









Python Loops
Loops
Loops help us repeat actions in Python. Imagine telling the computer: "Do this again and again." That’s what loops are for!
Why Use Loops?
Let’s say you want to print "Hi!" five times. Without a loop, you’d write:
print("Hi!")
print("Hi!")
print("Hi!")
print("Hi!")
print("Hi!")
That works… but what if you wanted to print it 100 times? That’s where loops save time.
Types of Loops in Python
- for loop – repeats a certain number of times
- while loop – repeats while a condition is true
1. for
Loop
A for
loop is useful when you know in advance how many times you want to repeat a block of code. It’s commonly used to iterate over sequences like lists, strings, or ranges of numbers.
Syntax
for variable in sequence:
# code to run for each item in the sequence
for
: The loop keyword that starts the iteration.variable
: A temporary variable that takes the value of each item in the sequence, one at a time.in
: A keyword that links the loop variable with the sequence to iterate over.sequence
: Any iterable object likerange()
, a list, or a string.- Indented block: The code under the loop that will execute once per iteration.
Example
for i in range(5):
print("Hi!")
Hi!
Hi!
Hi!
Hi!
Hi!
Explanation:
range(5)
generates a sequence of numbers from0
to4
(5 numbers in total).- The variable
i
takes on each of those values — first0
, then1
, and so on up to4
. - For each value of
i
, the lineprint("Hi!")
is executed once. - As a result,
"Hi!"
is printed 5 times.
2. while
Loop
A while
loop in Python is used to repeatedly execute a block of code as long as a specified condition remains True
.
Syntax
while condition:
# code to repeat
while
: The keyword that starts the loop.condition
: An expression that is evaluated before each iteration. The loop runs as long as this condition isTrue
.:
: Indicates the start of the loop block.- Indented block: The code inside the loop that gets executed repeatedly.
Example
count = 1
while count <= 3:
print("Welcome!")
count = count + 1
Explanation:
count
is initialized to1
.- The condition
count <= 3
is checked before each iteration. - If the condition is
True
, the loop body runs — printing"Welcome!"
and then increasingcount
by 1. - This continues for
count = 1
,2
, and3
. - When
count
becomes4
, the conditioncount <= 3
becomesFalse
, so the loop stops.
Looping Through a List
A for
loop in Python can be used to iterate over elements in a list — executing a block of code once for each item.
Example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
apple
banana
cherry
Explanation:
- A list named
fruits
is defined with three string elements:"apple"
,"banana"
, and"cherry"
. - The
for
loop goes through each element in the list one by one. - On each iteration, the variable
fruit
holds the current list item. - The statement prints the current value of
fruit
. - As a result, each fruit name is printed on a new line.
Using break in a Loop
break
stops the loop early.
for i in range(5):
if i == 3:
break
print(i)
Explanation:
- When
i
becomes 3, thebreak
line runs and stops the loop. - So 3 and 4 are never printed.
Using continue in a Loop
continue
skips the rest of the loop for that time only.
for i in range(5):
if i == 2:
continue
print(i)
Explanation:
- When
i
is 2, it skipsprint(i)
. - Everything else runs normally.