26
Lesson 5 of 44

Flow controls: Loops

Immerse yourself in the world of loop control structures in this lesson. Loops are a fundamental part of programming, allowing you to perform repetitive tasks and automate processes. Throughout this lesson, you will explore a variety of loops and learn how to use them effectively in your programs.

  • 7 theory articles
  • 19 practice exercises
Start lesson

Fundamentals concepts 7

The while loop

A loop repeats a block of code. while repeats it as long as a condition is true.

Theory
Read more

The for loop and range

When you know how many times something must be repeated, for is more convenient and safer than while : the control variable is…

Theory
Read more

Looping over strings with for

for does not work only with numbers: it can go through any sequence, such as a text string, character by character.

Theory
Read more

Counters and accumulators

Almost every useful loop keeps something while it runs. There are three patterns you will see again and again.

Theory
Read more

break, continue and while True

Sometimes you have to leave a loop before it ends, or skip a turn. That is what break and continue are for.

Theory
Read more

Nested loops

Inside a loop you can write another loop. For each turn of the outer loop, the inner one does all of its turns.

Theory
Read more

Common mistakes with loops

Loops are a classic source of mistakes. Recognising them will save you a lot of time.

Theory
Read more

Practice exercises 19

Char alphabet

Create a Python program that uses a `for` loop and `char` variables to display uppercase letters of the alphabet.

Exercise Medium
Solve it

Product of a number

Create a Python program that prompts the user for two integer numbers and displays their multiplication using consecutive…

Exercise Easy
Solve it

Multiplication table

Write a program in Python that asks the user for a number x and shows the result of his multiplication table from 1 to 10.

Exercise Easy
Solve it

Prime number

Develop a Python program that prompts the user to enter an integer and determine whether it is a prime number or not.

Exercise Easy
Solve it

Descending odd numbers

Write a Python program that displays odd numbers from x to y in descending order.

Exercise Easy
Solve it

Return change

Create a Python program that simulates a supermarket cash register to return change for a purchase. Request the total amount of…

Exercise Medium
Solve it

Product of prime factors

Create a Python program that asks the user for an integer (x) and displays the product of its prime factors. Prime factors are…

Exercise Medium
Solve it

Using the while statement

Create a Python program that requests a number (x) and displays the result of multiplying that number by 10. The program should…

Exercise Easy
Solve it

While loop with validated input

Create a Python program that requests a number (x) and displays the result of multiplying that number by 10. The program should…

Exercise Easy
Solve it

Repeated number

Create a Python program that requests a number (x) and a quantity (y) from the user. Then, display that number as many times as…

Exercise Easy
Solve it

Convert integer to binary and hexadecimal

Create a Python program that asks the user for an integer (x) and displays it in hexadecimal and binary. The program should…

Exercise Medium
Solve it

Range of numbers

Create a Python program that requests a range of numbers (x, y) from the user and displays those numbers on the screen, from…

Exercise Easy
Solve it

Multiples numbers

Create a Python program that displays numbers from 1 to 500 that are multiples of both 3 and 5.

Exercise Medium
Solve it

Hexadecimal table from 0 to 255

Create a Python program that displays hexadecimal numbers from 0 to 255 in a table with 16 rows, each containing 16 numbers.

Exercise Medium
Solve it

Access control

Write a Python program that requests the user's username and password.

Exercise Medium
Solve it

Infinite number sum

Create a Python program that requests numbers from the user and displays their sum. The program will continue requesting numbers…

Exercise Easy
Solve it

Infinite divisions

Create a Python program that allows the user to input two numbers. The program will perform the division of these numbers and…

Exercise Medium
Solve it

Infinite multiplication tables

Develop a Python program that allows the user to specify a range of numbers (x, y) and, subsequently, generates and displays the…

Exercise Easy
Solve it

Digits in a number

Write a Python program that calculates and displays the number of digits in a positive integer. If the user enters a negative…

Exercise Easy
Solve it