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
course content

Course Content

Python Loops Tutorial

If/Else in a for LoopIf/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:

How does the code work?


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).

Everything was clear?

Section 1. Chapter 5
toggle bottom row