Loops in Python
Learning Objectives
After this lesson, students will be able to:
• Understand the concept of looping and iteration.
• Differentiate between for and while loops.
• Use range() effectively.
• Apply loop control statements (break, continue, pass).
• Solve real-life computational problems using loops.
Introduction: What is a Loop?
A loop is a programming construct that allows a set of statements to be executed repeatedly
until a certain condition is met.
Python supports two main types of loops:
1. for loop — used for definite iteration.
2. while loop — used for indefinite iteration.
Types of Loops in Python
1. for Loop
Used when the number of iterations is known.
Syntax:
for variable in sequence:
statements
Example:
for i in range(1, 6):
print(i)
This prints numbers 1 to 5.
2. while Loop
Used when the number of iterations is not known in advance.
Syntax:
while condition:
statements
Example:
i = 1
while i <= 5:
print(i)
i += 1
This also prints numbers 1 to 5.
Loop Control Statements
Statement Description
break Exits the loop immediately.
continue Skips the remaining statements in current iteration.
pass Placeholder that does nothing; used for future code.
20 Solved Python Programs on Loops
1. Print Numbers from 1 to 10
for i in range(1, 11):
print(i)
2. Print First 10 Even Numbers
for i in range(2, 21, 2):
print(i)
3. Sum of First 10 Natural Numbers
sum = 0
for i in range(1, 11):
sum += i
print("Sum =", sum)
4. Print Multiplication Table of a Number
n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)
5. Factorial of a Number
n = int(input("Enter a number: "))
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial =", fact)
6. Reverse the Digits of a Number
num = int(input("Enter a number: "))
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
print("Reversed Number =", rev)
7. Check Palindrome Number
num = int(input("Enter a number: "))
temp = num
rev = 0
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
if rev == num:
print("Palindrome Number")
else:
print("Not a Palindrome Number")
8. Print All Prime Numbers Between 1 and 50
for num in range(2, 51):
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
9. Sum of Digits of a Number
num = int(input("Enter a number: "))
sum = 0
while num > 0:
sum += num % 10
num //= 10
print("Sum of digits =", sum)
10. Find the Largest Digit in a Number
num = int(input("Enter a number: "))
largest = 0
while num > 0:
d = num % 10
if d > largest:
largest = d
num //= 10
print("Largest digit =", largest)
11. Print Fibonacci Series (First 10 Terms)
a, b = 0, 1
for i in range(10):
print(a)
a, b = b, a + b
12. Display all Odd Numbers up to N
n = int(input("Enter a number: "))
for i in range(1, n + 1, 2):
print(i)
13. Count Even and Odd Digits in a Number
num = int(input("Enter a number: "))
even = odd = 0
while num > 0:
d = num % 10
if d % 2 == 0:
even += 1
else:
odd += 1
num //= 10
print("Even digits:", even)
print("Odd digits:", odd)
14. Display Pattern
*
* *
* * *
* * * *
* * * * *
for i in range(1, 6):
print("* " * i)
15. Display Reverse Pattern
* * * * *
* * * *
* * *
* *
*
for i in range(5, 0, -1):
print("* " * i)
16. Display Series: 1, 4, 9, 16, 25... up to N
n = int(input("Enter the limit: "))
for i in range(1, n + 1):
print(i ** 2, end=" ")
17. Calculate the Power of a Number
base = int(input("Enter base: "))
exp = int(input("Enter exponent: "))
result = 1
for i in range(exp):
result *= base
print("Result =", result)
18. Display Numbers Divisible by 3 and 5 (1–100)
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print(i)
19. Sum of Series 1 + 2 + 3 + ... + n
n = int(input("Enter n: "))
sum = 0
for i in range(1, n + 1):
sum += i
print("Sum =", sum)
20. Use of Break and Continue
for i in range(1, 10):
if i == 5:
continue
if i == 8:
break
print(i)
Skips 5 and stops loop when i = 8.
10 Unsolved Practice Problems (For
Students)
1. Write a program to find the factorial of a number using a while loop.
2. Write a program to display the cube of numbers from 1 to N.
3. Print all numbers between 1 and 100 that are divisible by either 3 or 7.
4. Print the multiplication table of any number up to 20.
5. Display a pattern:
6. 1
7. 1 2
8. 1 2 3
9. 1 2 3 4
10. 1 2 3 4 5
11. Count the number of digits in an input number.
12. Find the smallest digit in a given number.
13. Display all Armstrong numbers between 100 and 999.
14. Print all numbers from 1 to 50 except those divisible by 7.
15. Write a program to check whether a number is prime or not using a while loop.
Summary
Concept Keywords Example
Definite Loop for, range() Counting 1 to 10
Indefinite Loop while Until condition met
Loop Controls break, continue, pass Exit or skip
Nested Loops for inside for Patterns
Real-life Application Banking, factorial, digit sum Iteration logic