Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If/Else in a for Loop | The for Loop
Python Loops Tutorial
course content

Conteúdo do Curso

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

bookIf/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 to False.

Look at the code below:

12345678
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')
copy

How does the code work?

Tarefa
test

Swipe to show code editor

To create a program that determines whether a number in the list is less than 10, follow these steps:

  1. Configure the for loop to operate on the numbers, using i as an element of the list.
  2. Set the condition i < 10 to check if the number is less than 10.
  3. Display the appropriate messages: 'is lower than 10' if an element is lower than 10 (inside the if block), and 'is greater or equal to 10' if an element is greater than or equal to 10 (inside the else block).

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
toggle bottom row

bookIf/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 to False.

Look at the code below:

12345678
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')
copy

How does the code work?

Tarefa
test

Swipe to show code editor

To create a program that determines whether a number in the list is less than 10, follow these steps:

  1. Configure the for loop to operate on the numbers, using i as an element of the list.
  2. Set the condition i < 10 to check if the number is less than 10.
  3. Display the appropriate messages: 'is lower than 10' if an element is lower than 10 (inside the if block), and 'is greater or equal to 10' if an element is greater than or equal to 10 (inside the else block).

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 5
toggle bottom row

bookIf/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 to False.

Look at the code below:

12345678
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')
copy

How does the code work?

Tarefa
test

Swipe to show code editor

To create a program that determines whether a number in the list is less than 10, follow these steps:

  1. Configure the for loop to operate on the numbers, using i as an element of the list.
  2. Set the condition i < 10 to check if the number is less than 10.
  3. Display the appropriate messages: 'is lower than 10' if an element is lower than 10 (inside the if block), and 'is greater or equal to 10' if an element is greater than or equal to 10 (inside the else block).

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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 to False.

Look at the code below:

12345678
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')
copy

How does the code work?

Tarefa
test

Swipe to show code editor

To create a program that determines whether a number in the list is less than 10, follow these steps:

  1. Configure the for loop to operate on the numbers, using i as an element of the list.
  2. Set the condition i < 10 to check if the number is less than 10.
  3. Display the appropriate messages: 'is lower than 10' if an element is lower than 10 (inside the if block), and 'is greater or equal to 10' if an element is greater than or equal to 10 (inside the else block).

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 5
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt