Working with Python Dictionaries

Working with Python Dictionaries

Working with Python Dictionaries

Dictionaries are unordered, mutable collections in Python that store key-value pairs. Let's explore how to work with dictionaries:

1. Creating Dictionaries

Dictionaries are created using curly braces `{}` with key-value pairs.


# Example
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
        

2. Accessing Values

Values in a dictionary are accessed using keys.


# Example
name_value = my_dict['name']
        

3. Modifying and Adding Elements

Dictionaries are mutable, allowing modification of values and addition of new key-value pairs.


# Example
my_dict['age'] = 31
my_dict['gender'] = 'Male'
        
Working with Python Dictionaries Working with Python Dictionaries Reviewed by Naveen Gupta on 1:11 AM Rating: 5

No comments:

Theme images by mammamaart. Powered by Blogger.