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!")
Hi!
Hi!
Hi!
Hi!
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 from0to4(5 numbers in total).- The variable
itakes 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
Welcome!
Welcome!
Welcome!
Explanation:
countis initialized to1.- The condition
count <= 3is checked before each iteration. - If the condition is
True, the loop body runs — printing"Welcome!"and then increasingcountby 1. - This continues for
count = 1,2, and3. - When
countbecomes4, the conditioncount <= 3becomesFalse, 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
fruitsis defined with three string elements:"apple","banana", and"cherry". - The
forloop goes through each element in the list one by one. - On each iteration, the variable
fruitholds 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)
0
1
2
Explanation:
- When
ibecomes 3, thebreakline 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)
0
1
3
4
Explanation:
- When
iis 2, it skipsprint(i). - Everything else runs normally.
Comments
Loading comments...