Learn Python
Lesson 1 - Introduction To Python
Lesson 2 - Basic Python Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 6 - Exception Handling
Lesson 7 - Modules And Packages
Lesson 8 - Basic String Operations
Lesson 9 - Object-Oriented Programming (OOP)
Dictionaries in Python are an unordered collection of key-value pairs, enclosed by {}
. Each key in a dictionary is unique and associated with a corresponding value.
For example, consider a mark sheet where subjects are listed along with their respective marks. This can be represented as a dictionary where the subject names serve as keys and the marks as values.
Here's an example of a simple dictionary:
my_dict = {'name': 'Alex', 'age': 25, 'city': 'New York'}
Unlike lists and tuples, dictionaries require you to use the key to retrieve specific items. Take a look at the following example:
my_dict = {'name': 'Alex', 'age': 25, 'city': 'New York'} print(my_dict['age'])
In this example, we used the key 'age'
to access the corresponding value '25'
. Try the same example using the key 'name'
to access the value 'Alex'
.