Python for Beginners begunpro

Python Learning Day 22 & 23: File Handling

Python

Welcome to the next leg of your Python journey! In the coming days, we’ll explore the versatility of sets and dive into the world of file operations, from reading to writing. Let’s embark on this exciting exploration.

Sets: Unordered Collections of Unique Elements

In Python, a set is an unordered collection of unique elements. Sets are defined using curly braces {}.

Creating Sets:

				
					# Example: Creating a set
fruits_set = {"apple", "banana", "cherry"}

				
			

Adding and Removing Elements:

				
					# Adding elements to a set
fruits_set.add("orange")

# Removing elements from a set
fruits_set.remove("banana")

				
			

Sets are efficient for tasks that require checking membership or finding the intersection or difference between collections.

Working with Files: Reading and Writing

Python provides powerful tools for working with files. Reading from and writing to files allows you to interact with external data and persist information.

Reading from a File:

				
					# Example: Reading from a file
with open("example.txt", "r") as file:
    content = file.read()
    print(content)

				
			

Writing to a File:


				
					# Example: Writing to a file
with open("example.txt", "w") as file:
    file.write("Hello, this is a sample content.")

				
			

Appending to a File:

 
				
					# Example: Appending to a file
with open("example.txt", "a") as file:
    file.write("\nAppending new content.")

				
			

Exercise: Set Operations and File Manipulation

Let’s combine the power of sets and file operations in a practical exercise.

  1. Set Operations:

    • Create two sets representing the likes of users A and B.
    • Find the common likes (intersection) and the unique likes of each user.
  2. File Manipulation:

    • Create a file named “user_likes.txt” and write the likes of each user to the file.
    • Read the contents of the file and print them.
				
					# Example: Set Operations and File Manipulation
# Set Operations
likes_user_a = {"apple", "banana", "cherry"}
likes_user_b = {"banana", "orange", "grape"}

common_likes = likes_user_a.intersection(likes_user_b)
unique_likes_user_a = likes_user_a.difference(likes_user_b)
unique_likes_user_b = likes_user_b.difference(likes_user_a)

# File Manipulation
with open("user_likes.txt", "w") as file:
    file.write("User A Likes: " + ", ".join(likes_user_a) + "\n")
    file.write("User B Likes: " + ", ".join(likes_user_b) + "\n")

with open("user_likes.txt", "r") as file:
    file_content = file.read()
    print(file_content)

# Print set operations results
print("Common Likes:", common_likes)
print("Unique Likes User A:", unique_likes_user_a)
print("Unique Likes User B:", unique_likes_user_b)

				
			

This exercise demonstrates how sets can be used for operations like intersection and difference, and how file operations can be used to store and retrieve data.

Conclusion: Expanding Horizons with Sets and Files

You’ve now harnessed the power of sets, a versatile tool for handling unique collections, and delved into the realm of file operations, opening up new possibilities for interacting with external data.

As you continue your Python exploration, experiment with different sets and file scenarios. The more you practice, the more proficient you’ll become. Stay enthusiastic, keep coding, and brace yourself for the upcoming adventures in Python!