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

for loop (4/5)

Let's make our output a bit user-friendlier. Last time we iterated over all elements. Now let's iterate over indexes, and get elements by their indexes. For example, for countries, considering we know that each country is represented by two numbers, it will look like:

12345678910111213
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] # construct user-friendlier for loop for i in range(len(countries)): # it will iterate over all indexes if type(countries[i]) is tuple: # check if our element is tuple print('Area:', countries[i][0], 'sq km') print('Population:', countries[i][1]) print('--------------') else: print('Country name:', countries[i])
copy

There we used knowledge about our data, that each tuple is the length of 2. Also, we added a dotted line after each tuple to divide countries.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

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

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

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

for loop (4/5)

Let's make our output a bit user-friendlier. Last time we iterated over all elements. Now let's iterate over indexes, and get elements by their indexes. For example, for countries, considering we know that each country is represented by two numbers, it will look like:

12345678910111213
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] # construct user-friendlier for loop for i in range(len(countries)): # it will iterate over all indexes if type(countries[i]) is tuple: # check if our element is tuple print('Area:', countries[i][0], 'sq km') print('Population:', countries[i][1]) print('--------------') else: print('Country name:', countries[i])
copy

There we used knowledge about our data, that each tuple is the length of 2. Also, we added a dotted line after each tuple to divide countries.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

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

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

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

for loop (4/5)

Let's make our output a bit user-friendlier. Last time we iterated over all elements. Now let's iterate over indexes, and get elements by their indexes. For example, for countries, considering we know that each country is represented by two numbers, it will look like:

12345678910111213
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] # construct user-friendlier for loop for i in range(len(countries)): # it will iterate over all indexes if type(countries[i]) is tuple: # check if our element is tuple print('Area:', countries[i][0], 'sq km') print('Population:', countries[i][1]) print('--------------') else: print('Country name:', countries[i])
copy

There we used knowledge about our data, that each tuple is the length of 2. Also, we added a dotted line after each tuple to divide countries.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

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

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

Let's make our output a bit user-friendlier. Last time we iterated over all elements. Now let's iterate over indexes, and get elements by their indexes. For example, for countries, considering we know that each country is represented by two numbers, it will look like:

12345678910111213
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] # construct user-friendlier for loop for i in range(len(countries)): # it will iterate over all indexes if type(countries[i]) is tuple: # check if our element is tuple print('Area:', countries[i][0], 'sq km') print('Population:', countries[i][1]) print('--------------') else: print('Country name:', countries[i])
copy

There we used knowledge about our data, that each tuple is the length of 2. Also, we added a dotted line after each tuple to divide countries.

Завдання

Using the same approach as in the example, print the information about all people from the people list.

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