Python Loop Examples for Beginners
New to Python? This page will help you understand how to repeat actions using loops. Learn how for and while loops work through simple, hands-on examples. You'll also practice important concepts like break, continue, and nested loops.
1. Basic for loop with range
1. Basic for loop with range
python
# Example 1: Basic for loop
for i in range(5):
print("Iteration:", i)# Example 1: Basic for loop
for i in range(5):
print("Iteration:", i)2. Basic while loop
2. Basic while loop
python
# Example 2: Basic while loop
count = 0
while count < 5:
print("Count is:", count)
count += 1# Example 2: Basic while loop
count = 0
while count < 5:
print("Count is:", count)
count += 13. Using else with for loop
3. Using else with for loop
python
# Example 3: else with for loop
for i in range(3):
print("Inside loop:", i)
else:
print("Loop finished successfully")# Example 3: else with for loop
for i in range(3):
print("Inside loop:", i)
else:
print("Loop finished successfully")4. Using else with while loop
4. Using else with while loop
python
# Example 4: else with while loop
x = 0
while x < 3:
print("x =", x)
x += 1
else:
print("While loop ended")# Example 4: else with while loop
x = 0
while x < 3:
print("x =", x)
x += 1
else:
print("While loop ended")5. Break in a loop
5. Break in a loop
python
# Example 5: Using break
for i in range(10):
if i == 5:
break
print(i)# Example 5: Using break
for i in range(10):
if i == 5:
break
print(i)6. Continue in a loop
6. Continue in a loop
python
# Example 6: Using continue
for i in range(5):
if i == 2:
continue
print(i)# Example 6: Using continue
for i in range(5):
if i == 2:
continue
print(i)7. Infinite loop with break
7. Infinite loop with break
python
# Example 7: Infinite loop
while True:
print("This prints once")
break# Example 7: Infinite loop
while True:
print("This prints once")
break8. Nested for loops
8. Nested for loops
python
# Example 8: Nested loops
for i in range(2):
for j in range(3):
print(f"i={i}, j={j}")# Example 8: Nested loops
for i in range(2):
for j in range(3):
print(f"i={i}, j={j}")9. Countdown using while loop
9. Countdown using while loop
python
# Example 9: Countdown
n = 5
while n > 0:
print(n)
n -= 1
print("Liftoff!")# Example 9: Countdown
n = 5
while n > 0:
print(n)
n -= 1
print("Liftoff!")10. Sum of numbers using for loop
10. Sum of numbers using for loop
python
# Example 10: Sum of numbers
total = 0
for num in range(1, 6):
total += num
print("Total sum:", total)# Example 10: Sum of numbers
total = 0
for num in range(1, 6):
total += num
print("Total sum:", total)11. Factorial using while loop
11. Factorial using while loop
python
# Example 11: Factorial
n = 5
factorial = 1
while n > 0:
factorial *= n
n -= 1
print("Factorial is", factorial)# Example 11: Factorial
n = 5
factorial = 1
while n > 0:
factorial *= n
n -= 1
print("Factorial is", factorial)12. Multiplication table using for loop
12. Multiplication table using for loop
python
# Example 12: Multiplication table
num = 4
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")# Example 12: Multiplication table
num = 4
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")13. Print even numbers using for loop
13. Print even numbers using for loop
python
# Example 13: Print even numbers from 1 to 10
for i in range(1, 11):
if i % 2 == 0:
print(i)# Example 13: Print even numbers from 1 to 10
for i in range(1, 11):
if i % 2 == 0:
print(i)14. Print odd numbers using while loop
14. Print odd numbers using while loop
python
# Example 14: Print odd numbers from 1 to 10
n = 1
while n <= 10:
if n % 2 != 0:
print(n)
n += 1# Example 14: Print odd numbers from 1 to 10
n = 1
while n <= 10:
if n % 2 != 0:
print(n)
n += 115. Calculate square of numbers
15. Calculate square of numbers
python
# Example 15: Square of numbers from 1 to 5
for i in range(1, 6):
print(f"{i} squared is {i ** 2}")# Example 15: Square of numbers from 1 to 5
for i in range(1, 6):
print(f"{i} squared is {i ** 2}")16. Countdown with message
16. Countdown with message
python
# Example 16: Countdown timer
seconds = 3
while seconds > 0:
print(f"{seconds}...")
seconds -= 1
print("Time's up!")# Example 16: Countdown timer
seconds = 3
while seconds > 0:
print(f"{seconds}...")
seconds -= 1
print("Time's up!")17. Print a triangle pattern
17. Print a triangle pattern
python
# Example 17: Triangle pattern
rows = 5
for i in range(1, rows + 1):
print("*" * i)# Example 17: Triangle pattern
rows = 5
for i in range(1, rows + 1):
print("*" * i)18. Sum of even numbers using while loop
18. Sum of even numbers using while loop
python
# Example 18: Sum of even numbers from 1 to 10
total = 0
n = 1
while n <= 10:
if n % 2 == 0:
total += n
n += 1
print("Total even sum:", total)# Example 18: Sum of even numbers from 1 to 10
total = 0
n = 1
while n <= 10:
if n % 2 == 0:
total += n
n += 1
print("Total even sum:", total)19. Reverse loop with for
19. Reverse loop with for
python
# Example 19: Count backward using for
for i in range(10, 0, -1):
print(i)# Example 19: Count backward using for
for i in range(10, 0, -1):
print(i)20. Print multiples of a number
20. Print multiples of a number
python
# Example 20: Multiples of 3 from 1 to 30
for i in range(1, 31):
if i % 3 == 0:
print(i)# Example 20: Multiples of 3 from 1 to 30
for i in range(1, 31):
if i % 3 == 0:
print(i)21. Print numbers skipping last 2
21. Print numbers skipping last 2
python
# Example 21: Skip last two numbers in range
for i in range(1, 11):
if i > 8:
break
print(i)# Example 21: Skip last two numbers in range
for i in range(1, 11):
if i > 8:
break
print(i)22. Skip numbers divisible by 3
22. Skip numbers divisible by 3
python
# Example 22: Skip numbers divisible by 3
for i in range(1, 11):
if i % 3 == 0:
continue
print(i)# Example 22: Skip numbers divisible by 3
for i in range(1, 11):
if i % 3 == 0:
continue
print(i)23. Loop to find first number divisible by 7
23. Loop to find first number divisible by 7
python
# Example 23: Find first number divisible by 7
num = 1
while True:
if num % 7 == 0:
print("First divisible by 7:", num)
break
num += 1# Example 23: Find first number divisible by 7
num = 1
while True:
if num % 7 == 0:
print("First divisible by 7:", num)
break
num += 124. Simple login simulation
24. Simple login simulation
python
# Example 24: Login attempt simulation
password = "admin"
attempts = 0
while attempts < 3:
guess = input("Enter password: ")
if guess == password:
print("Access granted")
break
else:
print("Wrong password")
attempts += 1
else:
print("Account locked")# Example 24: Login attempt simulation
password = "admin"
attempts = 0
while attempts < 3:
guess = input("Enter password: ")
if guess == password:
print("Access granted")
break
else:
print("Wrong password")
attempts += 1
else:
print("Account locked")25. Print ASCII values of characters
25. Print ASCII values of characters
python
# Example 25: ASCII values
for ch in range(65, 71):
print(f"Character: {chr(ch)}, ASCII: {ch}")# Example 25: ASCII values
for ch in range(65, 71):
print(f"Character: {chr(ch)}, ASCII: {ch}")26. Sum of digits of a number
26. Sum of digits of a number
python
# Example 26: Sum of digits
number = 1234
sum_digits = 0
while number > 0:
sum_digits += number % 10
number //= 10
print("Sum of digits:", sum_digits)# Example 26: Sum of digits
number = 1234
sum_digits = 0
while number > 0:
sum_digits += number % 10
number //= 10
print("Sum of digits:", sum_digits)27. Check if a number is prime
27. Check if a number is prime
python
# Example 27: Prime number check
num = 7
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print("Prime number")
else:
print("Not a prime number")# Example 27: Prime number check
num = 7
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print("Prime number")
else:
print("Not a prime number")28. Print numbers in reverse using while loop
28. Print numbers in reverse using while loop
python
# Example 28: Reverse with while loop
n = 5
while n >= 1:
print(n)
n -= 1# Example 28: Reverse with while loop
n = 5
while n >= 1:
print(n)
n -= 129. Calculate cube of numbers
29. Calculate cube of numbers
python
# Example 29: Cube of numbers
for i in range(1, 6):
print(f"{i} cubed is {i ** 3}")# Example 29: Cube of numbers
for i in range(1, 6):
print(f"{i} cubed is {i ** 3}")30. Print numbers from 1 to 100 divisible by both 3 and 5
30. Print numbers from 1 to 100 divisible by both 3 and 5
python
# Example 30: Divisible by 3 and 5
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print(i)# Example 30: Divisible by 3 and 5
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print(i)31. Print digits in reverse order
31. Print digits in reverse order
python
# Example 31: Reverse digits
num = 123
while num > 0:
digit = num % 10
print(digit)
num //= 10# Example 31: Reverse digits
num = 123
while num > 0:
digit = num % 10
print(digit)
num //= 1032. Print a rectangle pattern
32. Print a rectangle pattern
python
# Example 32: Rectangle of stars
rows = 3
cols = 5
for i in range(rows):
print("*" * cols)# Example 32: Rectangle of stars
rows = 3
cols = 5
for i in range(rows):
print("*" * cols)33. Calculate power without using ** operator
33. Calculate power without using ** operator
python
# Example 33: Power using loop
base = 2
exp = 3
result = 1
for _ in range(exp):
result *= base
print("Result:", result)# Example 33: Power using loop
base = 2
exp = 3
result = 1
for _ in range(exp):
result *= base
print("Result:", result)34. Count digits in a number
34. Count digits in a number
python
# Example 34: Count digits
num = 7890
count = 0
while num != 0:
num //= 10
count += 1
print("Number of digits:", count)# Example 34: Count digits
num = 7890
count = 0
while num != 0:
num //= 10
count += 1
print("Number of digits:", count)35. Print multiplication table vertically
35. Print multiplication table vertically
python
# Example 35: Vertical multiplication
for i in range(1, 6):
for j in range(1, 6):
print(f"{i*j}", end="\t")
print()# Example 35: Vertical multiplication
for i in range(1, 6):
for j in range(1, 6):
print(f"{i*j}", end="\t")
print()36. Sum of first N natural numbers
36. Sum of first N natural numbers
python
# Example 36: Sum of N numbers
n = 10
total = 0
for i in range(1, n + 1):
total += i
print("Sum:", total)# Example 36: Sum of N numbers
n = 10
total = 0
for i in range(1, n + 1):
total += i
print("Sum:", total)What’s Next?
Great job learning how loops work in Python! Next, let’s take a look at how to use loops with different data structures like lists, Tuples, and more. This will help you write more powerful and flexible code using loops and collections together.