Contenido del Curso
Python Loops Tutorial
Python Loops Tutorial
If/Else in a for Loop
In the future, you'll frequently encounter and use the if/else
construct within loops. Here's a quick reminder of how it works:
- if - In this part, you specify the condition to check;
- else - The actions to execute if the condition in the
if
statement evaluates toFalse
.
Look at the code below:
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing special message, if 8 is in the list for i in numbers: if i == 8: print('Here is 8!') else: print('This num is not 8')
How does the code work?
Tarea
To create a program that determines whether a number in the list is less than 10, follow these steps:
- Configure the
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition
i < 10
to check if the number is less than 10. - Display the appropriate messages:
'is lower than 10'
if an element is lower than 10 (inside theif
block), and'is greater or equal to 10'
if an element is greater than or equal to 10 (inside theelse
block).
¡Gracias por tus comentarios!
If/Else in a for Loop
In the future, you'll frequently encounter and use the if/else
construct within loops. Here's a quick reminder of how it works:
- if - In this part, you specify the condition to check;
- else - The actions to execute if the condition in the
if
statement evaluates toFalse
.
Look at the code below:
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing special message, if 8 is in the list for i in numbers: if i == 8: print('Here is 8!') else: print('This num is not 8')
How does the code work?
Tarea
To create a program that determines whether a number in the list is less than 10, follow these steps:
- Configure the
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition
i < 10
to check if the number is less than 10. - Display the appropriate messages:
'is lower than 10'
if an element is lower than 10 (inside theif
block), and'is greater or equal to 10'
if an element is greater than or equal to 10 (inside theelse
block).
¡Gracias por tus comentarios!
If/Else in a for Loop
In the future, you'll frequently encounter and use the if/else
construct within loops. Here's a quick reminder of how it works:
- if - In this part, you specify the condition to check;
- else - The actions to execute if the condition in the
if
statement evaluates toFalse
.
Look at the code below:
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing special message, if 8 is in the list for i in numbers: if i == 8: print('Here is 8!') else: print('This num is not 8')
How does the code work?
Tarea
To create a program that determines whether a number in the list is less than 10, follow these steps:
- Configure the
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition
i < 10
to check if the number is less than 10. - Display the appropriate messages:
'is lower than 10'
if an element is lower than 10 (inside theif
block), and'is greater or equal to 10'
if an element is greater than or equal to 10 (inside theelse
block).
¡Gracias por tus comentarios!
In the future, you'll frequently encounter and use the if/else
construct within loops. Here's a quick reminder of how it works:
- if - In this part, you specify the condition to check;
- else - The actions to execute if the condition in the
if
statement evaluates toFalse
.
Look at the code below:
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing special message, if 8 is in the list for i in numbers: if i == 8: print('Here is 8!') else: print('This num is not 8')
How does the code work?
Tarea
To create a program that determines whether a number in the list is less than 10, follow these steps:
- Configure the
for
loop to operate on thenumbers
, usingi
as an element of the list. - Set the condition
i < 10
to check if the number is less than 10. - Display the appropriate messages:
'is lower than 10'
if an element is lower than 10 (inside theif
block), and'is greater or equal to 10'
if an element is greater than or equal to 10 (inside theelse
block).