Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
for loop (5/5) | Loops
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

bookfor loop (5/5)

We haven't already discussed how can we use for loop to iterate over dictionaries, as this data structure completely differs from others. If you try to iterate just over a dictionary, it will iterate over its keys. So, it's the same that iterating over dict.keys().

In the previous section we saved countries as a dictionary too. And this time it will be much understandable how to print information. Let's see

1234567891011
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # for loop for i in countries_dict.keys(): print("Country name:", i) print("Area:", countries_dict[i][0]) print("Population:", countries_dict[i][1]) print("---------")
copy

There key was country name and respective value - is a tuple with area and population. You see, that this for loop is much "friendlier" than the previous ones.

Завдання

Using the same approach, print each person's name, age, and height from people_d dictionary.

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

bookfor loop (5/5)

We haven't already discussed how can we use for loop to iterate over dictionaries, as this data structure completely differs from others. If you try to iterate just over a dictionary, it will iterate over its keys. So, it's the same that iterating over dict.keys().

In the previous section we saved countries as a dictionary too. And this time it will be much understandable how to print information. Let's see

1234567891011
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # for loop for i in countries_dict.keys(): print("Country name:", i) print("Area:", countries_dict[i][0]) print("Population:", countries_dict[i][1]) print("---------")
copy

There key was country name and respective value - is a tuple with area and population. You see, that this for loop is much "friendlier" than the previous ones.

Завдання

Using the same approach, print each person's name, age, and height from people_d dictionary.

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

bookfor loop (5/5)

We haven't already discussed how can we use for loop to iterate over dictionaries, as this data structure completely differs from others. If you try to iterate just over a dictionary, it will iterate over its keys. So, it's the same that iterating over dict.keys().

In the previous section we saved countries as a dictionary too. And this time it will be much understandable how to print information. Let's see

1234567891011
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # for loop for i in countries_dict.keys(): print("Country name:", i) print("Area:", countries_dict[i][0]) print("Population:", countries_dict[i][1]) print("---------")
copy

There key was country name and respective value - is a tuple with area and population. You see, that this for loop is much "friendlier" than the previous ones.

Завдання

Using the same approach, print each person's name, age, and height from people_d dictionary.

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

We haven't already discussed how can we use for loop to iterate over dictionaries, as this data structure completely differs from others. If you try to iterate just over a dictionary, it will iterate over its keys. So, it's the same that iterating over dict.keys().

In the previous section we saved countries as a dictionary too. And this time it will be much understandable how to print information. Let's see

1234567891011
# data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942)} countries_dict["Brazil"] = (8515767, 212559417) countries_dict["India"] = (3166391, 1380004385) # for loop for i in countries_dict.keys(): print("Country name:", i) print("Area:", countries_dict[i][0]) print("Population:", countries_dict[i][1]) print("---------")
copy

There key was country name and respective value - is a tuple with area and population. You see, that this for loop is much "friendlier" than the previous ones.

Завдання

Using the same approach, print each person's name, age, and height from people_d dictionary.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 6. Розділ 7
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt