🔁 Python Loops 

 

Overview

Loops in Python are used to repeat a block of code multiple times — until a specific condition becomes false.
They help reduce redundancy and automate repetitive tasks efficiently.

Example:

for i in range(5):
   print("Hello, Python!")

Output:

Hello, Python! (prints 5 times)
 

Types of Loops in Python

Python mainly provides two types of loops:

for loop

while loop

Let’s understand both in detail 👇

 

🧮 A. for Loop

The for loop is used when you know the number of iterations you want to perform.

Syntax:

for variable in sequence:
   # code block
 

Example 1: Print numbers from 1 to 5

for i in range(1, 6):
   print(i)
 

Example 2: Loop through a list

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
   print(fruit)
 

Example 3: Loop through a string

for ch in "Python":
   print(ch)
 

range() Function

The range() function generates a sequence of numbers, often used with for loops.

ExampleDescription
range(5)0 to 4
range(1, 6)1 to 5
range(1, 10, 2)1, 3, 5, 7, 9 (step = 2)

Example:

for i in range(1, 10, 2):
   print(i)
 

🔁 B. while Loop

The while loop is used when you don’t know in advance how many times to repeat — it runs until a condition becomes False.

Syntax:

while condition:
   # code block
 

Example 1: Count 1 to 5

i = 1 while i <= 5:    print(i)    i += 1

Example 2: Infinite loop (⚠️ avoid this!)

i = 1
while i <= 5:
   print(i)
   i += 1
 

⏹️ Loop Control Statements

Python provides control statements to manage loop execution — stop, skip, or hold temporarily.

StatementDescriptionExample
breakStops the loop entirelyif i == 5: break
continueSkips the current iterationif i == 3: continue
passDoes nothing (placeholder)Used for empty loops or future code

Example: break

for i in range(1, 10):
   if i == 5:
       break
   print(i)
 

➡️ Stops at 4.

Example: continue

for i in range(1, 6):
   if i == 3:
       continue
   print(i)
 

➡️ Skips 3, prints 1 2 4 5.

Example: pass

for i in range(3):
   pass  # Future code will go here
 

🔂 Nested Loops

A loop inside another loop is called a nested loop.

Example:

for i in range(1, 4):
   for j in range(1, 4):
       print(i, j)
 

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
 

🧠 Practical Examples

💡 Example 1: Sum of first n numbers

n = int(input("Enter n: "))
sum = 0
for i in range(1, n+1):
   sum += i
print("Sum =", sum)
 

💡 Example 2: Multiplication Table

num = int(input("Enter a number: "))
for i in range(1, 11):
   print(num, "x", i, "=", num*i)
 

💡 Example 3: Factorial using while loop

n = int(input("Enter a number: "))
fact = 1
while n > 0:
   fact *= n
   n -= 1
print("Factorial:", fact)
 

💡 Example 4: Reverse a string

word = input("Enter a word: ")
rev = ""
for ch in word:
   rev = ch + rev
print("Reversed:", rev)
 

🧩 Quick Practice Tasks

Print all even numbers from 1 to 50.

Find the sum of numbers divisible by 3 between 1–100.

Print the following star pattern:

*
* *
* * *
* * * *
* * * * *

Create a password system that keeps asking for input until the correct password is entered.

Generate a Fibonacci series up to n terms using a while loop.