Python Tutorials 4: Dictionaries and File Operations: Storing and Using Data
Share
Organizing Data with Dictionaries
Hello friends! We talked about basic data types, variables, conditional statements, loops and functions in Python.
Now we will learn how to use dictionaries to store data more flexibly in Python . Dictionaries are data structures used in Python to store data in the form of key-value pairs.
Dictionaries are defined in curly brackets, and key-value pairs are separated by commas. Keys must be unique and can be of any immutable data type. Values can be of any data type.
Example: ```python person = { "name": "Ali" "age": 32 "occupation": "Software Developer" } ```
In this example, we created a dictionary named person . The dictionary contains three keys named "name", "age", and "occupation". Each key has a corresponding value.
Access to Dictionary Elements
We can access dictionary elements using keys.
Example: ```python person = { "name": "John" "age": 32 "occupation": "Software Developer" } print(person["name"]) # Outputs "John" print(person["age"]) # Outputs 32 ```
Dictionary Operations
We can perform operations such as adding, deleting, and changing elements in dictionaries.
Example: ```python person = { "name": "John" "age": 32 "occupation": "Software Developer" } person["city"] = "Istanbul" # adds "city" key and "Istanbul" value del person["age"] # deletes "age" key and value person["occupation"] = "Python Developer" # changes "occupation" value print(person) # outputs {"name": "John" "occupation": "Python Developer" "city": "Istanbul"} ```
Reading and Writing Data from Files
To open, read and write files in Python, we use the open() function.
The open() function takes the file name and file mode to open the file.
File modes are:
"r": For reading only
"w": To write (overwrites if file already exists)
"a": To append (if the file already exists, append to the end)
"x": To create only (will fail if the file already exists)
"r+": For reading and writing. After opening the file, we can read data from or write data to the file using functions such as read() and write() .
After completing the file operation, we should close the file using the close() function.
Example: ```python # Reading a file file = open("data.txt" "r") content = file.read() print(content) file.close() # Writing to a file file = open("data.txt" "w") file.write("Hello World!") file.close() ```
Practical Examples
Now let's put what we learned into practice:
1. Receiving Information from the User and Saving it to the Dictionary
```python person = {} name = input("Your name: ") age = int(input("Your age: ")) profession = input("Your profession: ") person["name"] = name person["age"] = age person["profession"] = profession print(person) ```
This program takes the name, age and occupation from the user and stores this information in a dictionary.
2. Reading Data from a File and Printing to the Screen
```python file = open("data.txt" "r") content = file.read() print(content) file.close() ```
This program opens the file named "data.txt", reads its content and prints it to the screen.
Summary
In this lesson, we learned about dictionaries and file operations in Python. We learned how to create dictionaries, access their elements, and perform dictionary operations. By learning how to open, read, write, and close files, we can manage data more effectively in our programs.
Dictionaries and file operations are fundamental tools for storing and organizing data in programming. The better you understand these topics, the better your programming skills will improve.
Remember, programming is a journey. I am here to guide you on this journey. Let's explore the magical world of Python together!
In the next lesson we will learn about modules and error handling. Get ready!
Try to approach the challenges you face while coding in a humorous way. This will both increase your motivation and make your programming journey more enjoyable. Having funny and entertaining moments in the programming world is very important. Because sometimes writing code can be boring and challenging. Sharing these memories allows us to motivate each other and increase our love of programming even more.
Remember, programming is an art of creativity and problem solving. Discovering new concepts allows us to further develop this art. In the next lesson, we will learn about modules and error handling, which is a very important topic in the programming world. Get ready!