Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
for loop (3/5) | Loops
Learn Python from Scratch
course content

Contenido del Curso

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 (3/5)

After the last task, you can see that it looks good, but it would be much better if we could also get both numbers separately. This is possible, as you can put one loop in another.

For example, you surely remember our list with 5 countries and their areas and populations. Firstly, we can build for loop to print each element. Then, we can check, if this element is a tuple. If True, we need to iterate over its elements. To check if the object is, for example, tuple, use

object is tuple

Let's see how it works:

1234567891011
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] for i in countries: # iterate over list if type(i) is tuple: # check if it is tuple for j in i: # if tuple, then iterate over it print(j) else: # if not, just print item print(i)
copy

Tarea

Using list people print all the names, ages, and heights in that order.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 5
toggle bottom row

bookfor loop (3/5)

After the last task, you can see that it looks good, but it would be much better if we could also get both numbers separately. This is possible, as you can put one loop in another.

For example, you surely remember our list with 5 countries and their areas and populations. Firstly, we can build for loop to print each element. Then, we can check, if this element is a tuple. If True, we need to iterate over its elements. To check if the object is, for example, tuple, use

object is tuple

Let's see how it works:

1234567891011
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] for i in countries: # iterate over list if type(i) is tuple: # check if it is tuple for j in i: # if tuple, then iterate over it print(j) else: # if not, just print item print(i)
copy

Tarea

Using list people print all the names, ages, and heights in that order.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 5
toggle bottom row

bookfor loop (3/5)

After the last task, you can see that it looks good, but it would be much better if we could also get both numbers separately. This is possible, as you can put one loop in another.

For example, you surely remember our list with 5 countries and their areas and populations. Firstly, we can build for loop to print each element. Then, we can check, if this element is a tuple. If True, we need to iterate over its elements. To check if the object is, for example, tuple, use

object is tuple

Let's see how it works:

1234567891011
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] for i in countries: # iterate over list if type(i) is tuple: # check if it is tuple for j in i: # if tuple, then iterate over it print(j) else: # if not, just print item print(i)
copy

Tarea

Using list people print all the names, ages, and heights in that order.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

After the last task, you can see that it looks good, but it would be much better if we could also get both numbers separately. This is possible, as you can put one loop in another.

For example, you surely remember our list with 5 countries and their areas and populations. Firstly, we can build for loop to print each element. Then, we can check, if this element is a tuple. If True, we need to iterate over its elements. To check if the object is, for example, tuple, use

object is tuple

Let's see how it works:

1234567891011
# countries data countries = ['USA', (9629091, 331002651), 'Canada', (9984670, 37742154), 'Germany', (357114, 83783942), 'Brazil', (8515767, 212559417), 'India', (3166391, 1380004385)] for i in countries: # iterate over list if type(i) is tuple: # check if it is tuple for j in i: # if tuple, then iterate over it print(j) else: # if not, just print item print(i)
copy

Tarea

Using list people print all the names, ages, and heights in that order.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 6. Capítulo 5
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt