Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dictionaries (1/2) | Other data types
Learn Python from Scratch
course content

Зміст курсу

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

Dictionaries (1/2)

In previous tasks, we store characteristics and names they belong to a bit equivalent. I think you agree that it would be great if we could reach information for something by its name, not finding indexes. This problem can be solved by using dictionaries - one more data type in Python.

Dictionary can be represented as key:value. It's important that keys in the dictionary can't repeat, while values can. To create dictionary use {key1: value1, key2: value2, ...} or dict(key1: value1, key2: value2, ...). Keys must be immutable (number, string, tuple).

For example, let's create dictionary with our first three countries.

CountryAreaPopulation
USA9629091331002651
Canada998467037742154
Germany35711483783942
123
# create dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} print(countries_dict)
copy

Please note, that I placed countries' characteristics inside the tuple, as we mentioned before it greatly fits when we have some object characteristics.

Now if I want to get, for example, information for Canada, I can simply use d[key] function.

123
countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # information about Canada print(countries_dict["Canada"])
copy

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 5. Розділ 7
toggle bottom row

Dictionaries (1/2)

In previous tasks, we store characteristics and names they belong to a bit equivalent. I think you agree that it would be great if we could reach information for something by its name, not finding indexes. This problem can be solved by using dictionaries - one more data type in Python.

Dictionary can be represented as key:value. It's important that keys in the dictionary can't repeat, while values can. To create dictionary use {key1: value1, key2: value2, ...} or dict(key1: value1, key2: value2, ...). Keys must be immutable (number, string, tuple).

For example, let's create dictionary with our first three countries.

CountryAreaPopulation
USA9629091331002651
Canada998467037742154
Germany35711483783942
123
# create dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} print(countries_dict)
copy

Please note, that I placed countries' characteristics inside the tuple, as we mentioned before it greatly fits when we have some object characteristics.

Now if I want to get, for example, information for Canada, I can simply use d[key] function.

123
countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # information about Canada print(countries_dict["Canada"])
copy

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 5. Розділ 7
toggle bottom row

Dictionaries (1/2)

In previous tasks, we store characteristics and names they belong to a bit equivalent. I think you agree that it would be great if we could reach information for something by its name, not finding indexes. This problem can be solved by using dictionaries - one more data type in Python.

Dictionary can be represented as key:value. It's important that keys in the dictionary can't repeat, while values can. To create dictionary use {key1: value1, key2: value2, ...} or dict(key1: value1, key2: value2, ...). Keys must be immutable (number, string, tuple).

For example, let's create dictionary with our first three countries.

CountryAreaPopulation
USA9629091331002651
Canada998467037742154
Germany35711483783942
123
# create dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} print(countries_dict)
copy

Please note, that I placed countries' characteristics inside the tuple, as we mentioned before it greatly fits when we have some object characteristics.

Now if I want to get, for example, information for Canada, I can simply use d[key] function.

123
countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # information about Canada print(countries_dict["Canada"])
copy

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

In previous tasks, we store characteristics and names they belong to a bit equivalent. I think you agree that it would be great if we could reach information for something by its name, not finding indexes. This problem can be solved by using dictionaries - one more data type in Python.

Dictionary can be represented as key:value. It's important that keys in the dictionary can't repeat, while values can. To create dictionary use {key1: value1, key2: value2, ...} or dict(key1: value1, key2: value2, ...). Keys must be immutable (number, string, tuple).

For example, let's create dictionary with our first three countries.

CountryAreaPopulation
USA9629091331002651
Canada998467037742154
Germany35711483783942
123
# create dictionary countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} print(countries_dict)
copy

Please note, that I placed countries' characteristics inside the tuple, as we mentioned before it greatly fits when we have some object characteristics.

Now if I want to get, for example, information for Canada, I can simply use d[key] function.

123
countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} # information about Canada print(countries_dict["Canada"])
copy

Завдання

Create a dictionary with keys - names of people and values - their respective age and height (as a tuple). Print information for Alex.

NameAgeHeight
Alex23178
Noah34189
Peter29175

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 5. Розділ 7
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt