Quick answerThese Python loop exercises practise for loops with range() (times tables, FizzBuzz), nested loops (a star triangle), while loops (digit sum, Collatz sequence) and break (prime test). Every task is checked automatically against several inputs.
Loop refresher
for i in range(1, 6): runs with i = 1, 2, 3, 4, 5 โ the stop value is not included. A while loop repeats as long as its condition is true, so make sure something inside it changes the condition. break leaves a loop early; print(x, end=" ") stays on the same line.
Exercises
1. Times tableeasy
Read a number n and print its times table from 1 to 5 in the form n x i = result.
๐ก for i in range(1, 6): then print(f"{n} x {i} = {n * i}")
Show solution
n = int(input())
for i in range(1, 6):
print(f"{n} x {i} = {n * i}")
range(1, 6) stops before 6, which is why the stop value is one more than the last number you want.
2. FizzBuzzeasy
For each number from 1 to n, print Fizz if it's divisible by 3, Buzz if by 5, FizzBuzz if by both, otherwise the number.
๐ก Check divisibility by 15 first โ otherwise 15 would be caught by the 3 case.
Show solution
n = int(input())
for i in range(1, n + 1):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
Order matters in an if/elif chain: the first true branch wins.
3. Star triangleeasy
Read n and print a right triangle of stars with n rows: row 1 has *, row 2 has **, and so on.
๐ก String multiplication repeats text: "*" * 3 is ***.
Show solution
n = int(input())
for row in range(1, n + 1):
print("*" * row)
A nested loop also works (an inner loop printing one star at a time with end=""), but multiplying a string is the Pythonic way.
4. Sum of digitsmedium
Read a non-negative whole number and print the sum of its digits using a while loop and arithmetic (no converting to a string). 9045 โ 18.
๐ก n % 10 is the last digit and n // 10 removes it. Loop while n > 0.
Show solution
n = int(input())
total = 0
while n > 0:
total += n % 10
n //= 10
print(total)
Each pass peels off the last digit. For 0 the loop never runs and the total stays 0, which is correct.
5. Is it prime?medium
Read a whole number n (n โฅ 1) and print prime or not prime. 1 is not prime.
๐ก Try divisors from 2 up to the square root of n; if any divides n, it isn't prime โ break out early.
Show solution
n = int(input())
is_prime = n > 1
d = 2
while d * d <= n:
if n % d == 0:
is_prime = False
break
d += 1
print("prime" if is_prime else "not prime")
If n had a divisor bigger than โn, it would also have one smaller than โn, so checking up to โn is enough.
6. Collatz stepshard
Start from n. If it's even, halve it; if it's odd, make it 3n + 1. Count the steps until you reach 1 and print the count. 6 โ 6 โ 3 โ 10 โ 5 โ 16 โ 8 โ 4 โ 2 โ 1 = 8 steps.
๐ก while n != 1: then update n with an if/else and add one to steps.
Show solution
n = int(input())
steps = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
steps += 1
print(steps)
Nobody has proved this loop always ends โ it's the famous Collatz conjecture โ but it does for every number ever tried. Use // so n stays an int.
Next topic: Function exercises โ
Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.
Frequently asked questions
What's the difference between for and while loops in Python?
A for loop runs once per item in a sequence (or per number in a range). A while loop runs as long as a condition is true โ use it when you don't know in advance how many times to repeat.
Why does range(1, 5) stop at 4?
range's stop value is exclusive. That makes range(len(items)) produce exactly the valid indexes.
How do I stop an endless loop here?
Press Stop in the compiler. Exercise checks also time out automatically after a few seconds.