Loop in Python
A loop in Python is a programming structure that allows a block of code to be
executed again and again based on a condition or sequence.
In simple terms, a loop helps automate repetition.
Why do we need loops?
Loops are very useful in programming because they:
• Reduce code repetition
• Save time and effort
• Make programs shorter and readable
• Help in processing large data
Example without loop:
print("Welcome")
print("Welcome")
print("Welcome")
print("Welcome")
Same work using a loop:
for i in range(4):
print("Welcome")
Types of Loops in Python
Python provides the following main types of loops:
[Link] loop
[Link] loop
These two loops are enough to handle almost all repetition tasks in Python.
1. for Loop in Python
The for loop is used when we know in advance how many times a block of code
should run, or when we want to loop through a collection like:
• List
• Tuple
• String
• Dictionary
• Range
The for loop goes through each item of a sequence one by one and executes the code block
for each item.
Syntax of for Loop:
for variable in sequence:
statements
Explanation:
• variable gets one value at a time from the sequence.
• sequence is a collection or range.
• statements are executed in every loop cycle.
Example 1: Print numbers from 1 to 5
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Example 2: Printing items from a list
animals = ["Dog", "Cat", "Horse", "Elephant"]
for a in animals:
print(a)
Example 3: Looping through a string
for letter in "PYTHON":
print(letter)
Each character is printed one by one.
Example 4: Printing a multiplication table
num = 7
for i in range(1, 11):
print(num, "x", i, "=", num * I)
2. while Loop in Python
The while loop continues running as long as the condition remains True. When the
condition becomes False, the loop stops automatically.
This loop is mainly used when:
• The number of repetitions is not fixed
• We want to loop until something happens
• We depend on user input or system value
Syntax of while Loop:
while condition:
statements
Example 1: Print numbers from 1 to 10
i=1
while i <= 10:
print(i)
i += 1
Example 2: Print even numbers
i=2
while i <= 20:
print(i)
i += 2
Example 3: Countdown Timer
count = 5
while count > 0:
print(count)
count -= 1
Loop Control Statements
Python provides special keywords to control loop execution:
break: (Stop the loop completely)
for i in range(10):
if i == 5:
break
print(i)
continue: (Skip current iteration and continue)
for i in range(5):
if i == 2:
continue
print(i)
Nested loop programs
1. Print a Square Pattern
Program:
for i in range(5):
for j in range(5):
print("*", end=" ")
print()
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
2. Print a Right-Angle Triangle Pattern
Program:
for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()
Output:
*
* *
* * *
* * * *
* * * * *
3. Print a Number Triangle
Program:
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
4. Print Inverted Triangle
Program:
for i in range(5, 0, -1):
for j in range(i):
print("*", end=" ")
print()
Output:
* * * * *
* * * *
* * *
* *
*