Editor settings

General settings

Save settings

Learn to program with Python

Python Dictionaries #

In Python, a dictionary is a collection of key-value pairs. Each key in a dictionary must be unique and can be of any hashable type (strings, numbers, tuples, etc.). The values can be of any type (strings, numbers, lists, dictionaries, etc.).

You can create a dictionary using curly braces {} and separating key-value pairs using a colon : and separate each pair by a comma ,. Here's an example:

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

In the example above, we created a variable my_dict with key-values as per the table below:

key value
name "John"
age 25
city "New York"

Exercise #

  • Create a variable student as a dictionary with:
    • name as a key with a string value
    • age as a key with an integer value
Output will be displayed here