Loops are one of the most important concepts in Python programming. They allow you to repeat tasks efficiently without writing the same code multiple times. Whether you’re preparing for exams, coding interviews, or simply improving your skills, practicing loop questions in Python with answers is a great way to strengthen your logic-building abilities. In this guide, we’ve compiled a set of looping questions in Python that cover both for and while loops, including simple to slightly challenging problems. Each question is designed to test your understanding of iteration, nested loops, and loop control statements like break and continue. By solving these, you’ll gain confidence in writing cleaner and more efficient Python programs.
Things Covered in This Article
- Basics of loops in Python (for and while loops)
- Simple loop questions with answers
- Nested loop problems for better understanding
- Loop control statements (break, continue, pass)
- Real-world examples using loops
- Common mistakes in looping and how to fix them
1. Print numbers from 1 to 10 using a for loop
for i in range(1, 11):
print(i)
# Output: 1 2 3 4 5 6 7 8 9 10
2. Print even numbers between 1 and 20 using a while loop
n = 2
while n <= 20:
print(n)
n += 2
# Output: 2 4 6 8 10 12 14 16 18 20
3. Find the sum of numbers from 1 to 100
total = 0
for i in range(1, 101):
total += i
print(total)
# Output: 5050
4. Print the multiplication table of 7
for i in range(1, 11):
print(f"7 x {i} = {7*i}")
# Output: 7×1=7 ... 7×10=70
5. Reverse a string using a loop
s = "Python"
rev = ""
for char in s:
rev = char + rev
print(rev)
# Output: nohtyP
6. Print first 10 odd numbers
for i in range(1, 20, 2):
print(i)
# Output: 1 3 5 7 9 11 13 15 17 19
7. Find factorial of a number using a for loop
n = 5
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)
# Output: 120
8. Print elements of a list using a for loop
fruits = ["apple", "banana", "cherry"]
for f in fruits:
print(f)
# Output: apple banana cherry
9. Print numbers until 5 using break
for i in range(1, 11):
if i == 6:
break
print(i)
# Output: 1 2 3 4 5
10. Skip even numbers using continue
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
# Output: 1 3 5 7 9
11. Nested loop: print a 3×3 star pattern
for i in range(3):
for j in range(3):
print("*", end=" ")
print()
# Output:
# * * *
# * * *
# * * *
12. Nested loop: print a right triangle of stars
for i in range(1, 6):
for j in range(i):
print("*", end="")
print()
# Output:
# *
# **
# ***
# ****
# *****
13. Calculate the sum of digits of a number
num = 1234
sum_digits = 0
while num > 0:
sum_digits += num % 10
num //= 10
print(sum_digits)
# Output: 10
14. Print all characters except vowels
text = "programming"
for ch in text:
if ch in "aeiou":
continue
print(ch, end="")
# Output: prgrmmng
15. Find first number divisible by both 3 and 7
n = 1
while True:
if n % 3 == 0 and n % 7 == 0:
print(n)
break
n += 1
# Output: 21
16. Print squares of numbers from 1 to 5
for i in range(1, 6):
print(i**2)
# Output: 1 4 9 16 25
17. Countdown from 5 to 1
n = 5
while n > 0:
print(n)
n -= 1
# Output: 5 4 3 2 1
18. Check if a number is prime using a loop
num = 7
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
print(is_prime)
# Output: True
19. Reverse a list using a loop
lst = [1, 2, 3, 4]
rev = []
for i in range(len(lst)-1, -1, -1):
rev.append(lst[i])
print(rev)
# Output: [4, 3, 2, 1]
20. Count vowels in a string
s = "Hello World"
count = 0
for ch in s.lower():
if ch in "aeiou":
count += 1
print(count)
# Output: 3
21. Multiplication table using nested loops
for i in range(1, 4):
for j in range(1, 4):
print(f"{i}x{j}={i*j}", end=" ")
print()
# Output:
# 1x1=1 1x2=2 1x3=3
# 2x1=2 2x2=4 2x3=6
# 3x1=3 3x2=6 3x3=9
22. Skip negative numbers in a list
nums = [3, -1, 4, -2, 5]
for n in nums:
if n < 0:
continue
print(n)
# Output: 3 4 5
23. Use pass in a loop
for i in range(5):
if i == 2:
pass
print(i)
# Output: 0 1 2 3 4
24. Print Fibonacci series up to 10 terms
a, b = 0, 1
for _ in range(10):
print(a, end=" ")
a, b = b, a + b
# Output: 0 1 1 2 3 5 8 13 21 34
Conclusion
1. Print numbers from 1 to 10 using a for loop
for i in range(1, 11): print(i) # Output: 1 2 3 4 5 6 7 8 9 10
2. Print even numbers between 1 and 20 using a while loop
n = 2 while n <= 20: print(n) n += 2 # Output: 2 4 6 8 10 12 14 16 18 20
3. Find the sum of numbers from 1 to 100
total = 0 for i in range(1, 101): total += i print(total) # Output: 5050
4. Print the multiplication table of 7
for i in range(1, 11): print(f"7 x {i} = {7*i}") # Output: 7×1=7 ... 7×10=70
5. Reverse a string using a loop
s = "Python" rev = "" for char in s: rev = char + rev print(rev) # Output: nohtyP
6. Print first 10 odd numbers
for i in range(1, 20, 2): print(i) # Output: 1 3 5 7 9 11 13 15 17 19
7. Find factorial of a number using a for loop
n = 5 fact = 1 for i in range(1, n+1): fact *= i print(fact) # Output: 120
8. Print elements of a list using a for loop
fruits = ["apple", "banana", "cherry"] for f in fruits: print(f) # Output: apple banana cherry
9. Print numbers until 5 using break
for i in range(1, 11): if i == 6: break print(i) # Output: 1 2 3 4 5
10. Skip even numbers using continue
for i in range(1, 11): if i % 2 == 0: continue print(i) # Output: 1 3 5 7 9
11. Nested loop: print a 3×3 star pattern
for i in range(3): for j in range(3): print("*", end=" ") print() # Output: # * * * # * * * # * * *
12. Nested loop: print a right triangle of stars
for i in range(1, 6): for j in range(i): print("*", end="") print() # Output: # * # ** # *** # **** # *****
13. Calculate the sum of digits of a number
num = 1234 sum_digits = 0 while num > 0: sum_digits += num % 10 num //= 10 print(sum_digits) # Output: 10
14. Print all characters except vowels
text = "programming" for ch in text: if ch in "aeiou": continue print(ch, end="") # Output: prgrmmng
15. Find first number divisible by both 3 and 7
n = 1 while True: if n % 3 == 0 and n % 7 == 0: print(n) break n += 1 # Output: 21
16. Print squares of numbers from 1 to 5
for i in range(1, 6): print(i**2) # Output: 1 4 9 16 25
17. Countdown from 5 to 1
n = 5 while n > 0: print(n) n -= 1 # Output: 5 4 3 2 1
18. Check if a number is prime using a loop
num = 7 is_prime = True for i in range(2, num): if num % i == 0: is_prime = False break print(is_prime) # Output: True
19. Reverse a list using a loop
lst = [1, 2, 3, 4] rev = [] for i in range(len(lst)-1, -1, -1): rev.append(lst[i]) print(rev) # Output: [4, 3, 2, 1]
20. Count vowels in a string
s = "Hello World" count = 0 for ch in s.lower(): if ch in "aeiou": count += 1 print(count) # Output: 3
21. Multiplication table using nested loops
for i in range(1, 4): for j in range(1, 4): print(f"{i}x{j}={i*j}", end=" ") print() # Output: # 1x1=1 1x2=2 1x3=3 # 2x1=2 2x2=4 2x3=6 # 3x1=3 3x2=6 3x3=9
22. Skip negative numbers in a list
nums = [3, -1, 4, -2, 5] for n in nums: if n < 0: continue print(n) # Output: 3 4 5
23. Use pass in a loop
for i in range(5): if i == 2: pass print(i) # Output: 0 1 2 3 4
24. Print Fibonacci series up to 10 terms
a, b = 0, 1 for _ in range(10): print(a, end=" ") a, b = b, a + b # Output: 0 1 1 2 3 5 8 13 21 34
Mastering loops is essential for becoming a proficient Python programmer. These loop questions in Python with answers give you a mix of theory and practice, helping you not only understand syntax but also apply loops in problem-solving. The more you work on looping questions in Python, the faster and more efficient your coding will become. Keep practicing, experiment with variations, and soon, loops will become second nature in your programming journey.