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

Course Content

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?

Task
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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 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?

Task
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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 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?

Task
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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your 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?

Task
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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt