๐Ÿ“ฑ Code Python on your phone with Python Coding - Learn PythonGet the free Android app โ†’
  1. Home
  2. Python exercises
  3. Loop exercises

Python exercises ยท 6 tasks

Python loop exercises (for and while)

Loops are where programs start to do real work. These six exercises move from a simple for over range() to nested loops and a while loop that runs until a condition is met.

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.

0 / 6 solved

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.

    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.

      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.

        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.

          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.

            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.

              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 โ†’

              Practise on your phone too

              Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.

              Get it on Google Play

              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.

              Keep practising

              Updated 2026-10-22 ยท every solution is tested on Python 3