Python FunctionsPython Functions1

Python Exception HandlingPython Exception Handling1

Python Dictionaries



In Python, a dictionary is a collection that allows you to store data in pairs. Each pair consists of a key and a value. Think of it as a real-world dictionary where you look up a word (key) and get its meaning (value).

What is a Python Dictionary?

A Python dictionary is an unordered collection that stores data as key-value pairs. Each key is unique, and each key maps to a value. You can use the key to access the associated value.

Example of a Dictionary

my_dict = {"name": "John", "age": 25, "city": "New York"}

In this dictionary, the keys are "name", "age", and "city". The corresponding values are "John", 25, and "New York".

Where Are Dictionaries Used?

Dictionaries are very useful when you need to store data that is associated with a unique identifier (the key). For example, storing student names with their grades, a person's contact information (name, phone number, etc.), or the countries and their capitals.

Basic Operations on Dictionaries

1. Create a Dictionary

To create a dictionary, you can use curly braces {} and separate keys and values with a colon :.

student = {"name": "Alice", "age": 22, "grade": "A"}

2. Read/Access Data

To get the value associated with a key, you can use square brackets [] with the key.

print(student["name"])  # Output: Alice

The above code prints the value associated with the key "name", which is Alice.

3. Update Data

If you want to change the value associated with a key, you can directly assign a new value to that key.

student["age"] = 23  # Now the age is updated

Now, if you print the dictionary, the age will be updated:

print(student)  # Output: {"name": "Alice", "age": 23, "grade": "A"}

4. Add New Key-Value Pair

You can add a new key-value pair to the dictionary by assigning a value to a new key.

student["city"] = "London"  # Adding a new key-value pair

After adding, the dictionary will look like this:

print(student)  # Output: {"name": "Alice", "age": 23, "grade": "A", "city": "London"}

5. Delete Data

If you want to remove a key-value pair from the dictionary, you can use the del keyword.

del student["grade"]  # Removing the grade key-value pair

After deleting, the dictionary will look like this:

print(student)  # Output: {"name": "Alice", "age": 23, "city": "London"}

Summary of CRUD Operations

Example: Contact Information Dictionary

Here’s an example of a dictionary for storing contact information:

contact_info = {
      "name": "John",
      "phone": "123-456-7890",
      "email": "john@example.com"
    }

We can update John's phone number or add his address using the same CRUD operations as shown earlier.

Conclusion

Dictionaries are a powerful and flexible way to store and manipulate data in Python. They allow you to easily access and modify information based on a unique key, which is great for many real-world use cases like storing user profiles, managing data, and much more. Once you're comfortable with dictionaries, you'll be able to organize your data efficiently and solve problems with ease!



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M