Mini tasks — Loops (for and while)
In this lesson you can practise using for and while loops in practice.
The tasks are short, clear and meant for beginners. You can run each task in any Python environment.
The goal is to become confident using loops, conditions and working with lists.
1. Printing numbers with a for loop
Task: Print the numbers from 1 to 10.
for i in range(1, 11):
print(i)
2. The sum of the first N numbers (while)
Task:
The user enters a whole number n.
Calculate the sum of the numbers from 1 to n using a while loop.
Hint (short):
Use a helper variable that starts at 1 and increases step by step until it reaches n.
In each iteration add the current value to the sum.
3. Printing numbers and their squares as a table
Task:
Print each number from 1 to 5 and its square using a for loop.
Example output:
1 -> 1 2 -> 4 3 -> 9 4 -> 16 5 -> 25
4. Guess the number (while + break)
Task:
The program chooses a secret number. The user keeps guessing until they guess the right number.
Use while True and break.
Example output:
Guess the number: 4 Try again. Guess the number: 7 Well done! You guessed it!
5. Checking for even numbers in a list
Task:
From a given list of numbers, pick out and print only the even numbers.
Example output:
6 10 12
6. Adding until the user enters 0
Task:
The user enters numbers. Add them up until they enter 0.
Example input:
4 7 2 0
Example output:
The total sum is: 13
7. The factorial of a number (for)
Task:
Calculate the factorial of the number n using a for loop.
Example input:
5
Example output:
The factorial is: 120
These tasks help you master:
- for and while loops
- conditions (
if) - controlling the flow of a program (
break)
Mini tasks: while + break
The following tasks use a while loop together with
the break statement.
This approach is often used when a program should run until the user
decides to stop it.
Mini task 1: Input until a sentinel value arrives
Task:
Write a program that asks the user to enter numbers.
The program ends when the user enters 0.
At the end, print how many numbers were entered.
Example input:
5 3 8 0
Example output:
3 numbers were entered.
Mini task 2: Guessing a number
Task:
Let us imagine that the correct number is 7.
The program asks the user to guess the number until they get it right.
Example output:
Guess the number: 5 Try again. Guess the number: 9 Try again. Guess the number: 7 Well done! You guessed the number.
Mini task 3: A simple menu application
Task:
Make a simple menu with the following options:
- 1 – Greeting
- 2 – Show a message
- 0 – Exit the program
Example output:
1 - Greeting 2 - Message 0 - Exit Choice: 1 Hello!
Tasks for independent work and challenges
The following tasks do not have solutions given.
The goal is to apply what you know about loops,
nested loops, break and continue on your own.
Try it yourself first, and only then check your solutions.
Basic tasks
-
Write a program that prints the numbers from 1 to 10,
but skips the number 5 using
continue. -
Print a 5×5 square of asterisks (
*). -
Calculate the sum of the numbers from 1 to 100 using a
forloop. - Write a program that counts how many even numbers there are in the range from 1 to 20.
Intermediate tasks
-
Write a program that asks the user to enter numbers
until they enter
0, and then prints their sum. - Print a triangle of asterisks with a height of 6 using nested loops.
- For a list of numbers, print all pairs where the first number is smaller than the second.
- Make a simple menu application that runs until the user chooses to exit.
Challenges
- Make a program that finds the first prime number greater than 100 and stops as soon as it finds it.
- Print a diagonal pattern of asterisks in a 7×7 square.
- For two lists of numbers, find all pairs whose sum is 10.
- Simulate a simple number-guessing game where the program counts how many attempts were needed.
Tip: If you get stuck, split the problem into smaller steps and think about which loop (or combination of loops) you should use.
Interactive quiz – Find the mistake and complete the code
Click the answer you think is correct. Read the task text and the expected output carefully.
Task 1: Find the mistake in the for loop
The program should print the numbers from 1 to 4, each on a separate line.
for i in range(1, 5)
print(i)
Expected output:
1 2 3 4
Task 2: Complete the code
Complete the code so that the numbers are printed 4 times, in order from 0 to 3.
for i in __________:
print(i)
Expected output:
0 1 2 3
Task 3: Which code prints "Python" exactly 5 times?
Choose the code that prints the word Python exactly 5 times.
# Option A
for i in range(5):
print("Python")
# Option B
for i in range(1, 5):
print("Python")
# Option C
for i in range(1, 6):
print("Python")
Expected output:
Python Python Python Python Python
