Python Basics

Python Dictionaries: Beginner Python tutorials 9 | Better4Code

Python Dictionaries: Python is a powerful programming language that provides a wide range of data structures to store and manipulate data. One such data structure is dictionaries. In this article, we will discuss how to use dictionaries in Python with examples.

Python Dictionaries: Beginner python tutorials 9 | Better4Code

Creating Dictionaries

You can create a dictionary in Python by enclosing key-value pairs in curly braces {} separated by commas. Here’s an example:

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

In this example, we have created a dictionary with three key-value pairs where the keys are ‘name’, ‘age’, and ‘city’ and the corresponding values are ‘John’, 30, and ‘New York’.


Alternatively, you can also create a dictionary by using the dict() function and passing key-value pairs as arguments. Here’s an example:

my_dict = dict(name='John', age=30, city='New York')

In this example, we have created a dictionary using the dict() function.

Accessing and Modifying Elements

You can access an element in a dictionary by using its key. Here’s an example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict['name'])

Output:

John

In this example, we have accessed the value of the key ‘name’ in the dictionary using indexing.


You can also modify an element in a dictionary by assigning a new value to its key. Here’s an example:

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['age'] = 35
print(my_dict)

 

Output:

{'name': 'John', 'age': 35, 'city': 'New York'}

In this example, we have modified the value of the key ‘age’ in the dictionary.

 

Dictionary Operations

Dictionaries support various operations such as adding, removing and checking for the existence of a key. Here’s an example of each operation:

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

# Adding a Key-Value Pair
my_dict['country'] = 'USA'
print(my_dict)

# Removing a Key-Value Pair
del my_dict['city']
print(my_dict)

# Checking for the Existence of a Key
if 'age' in my_dict:
print('Age:', my_dict['age'])

Output:

{'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}
{'name': 'John', 'age': 30, 'country': 'USA'}
Age: 30

In this example, we have performed each dictionary operation on the dictionary my_dict.

 

Dictionary Comprehension

Like lists and sets, dictionaries also support comprehension. Here’s an example:

my_dict = {x: x**2 for x in range(1, 6)}
print(my_dict)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

In this example, we have used dictionary comprehension to create a dictionary of squares of numbers from 1 to 5 where the key is the number and the value is its square.

 

Conclusion

Dictionaries are an important data structure in Python that allows you to store key-value pairs and perform various dictionary operations. In this article, we have discussed how to create dictionaries, access and modify elements, perform dictionary operations, and use dictionary comprehension.

gp

Are you looking to learn a programming language but feeling overwhelmed by the complexity? Our programming language guide provides an easy-to-understand, step-by-step approach to mastering programming.

Leave a Reply

Your email address will not be published. Required fields are marked *