Python – Loops
Loops in Python allow you to execute a block of code repeatedly. There are two main types of loops in Python: for
loops and while
loops.
1. for Loop
A for
loop is used to iterate over a sequence (such as a list, tuple, dictionary, set, or string) or other iterable objects.
Syntax:
for item in sequence:
# code to execute for each item
Example: Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
In this example, the loop iterates over each item in the fruits
list and prints each fruit.
Example: Using range() The range()
function generates a sequence of numbers, which is useful for iterating a specific number of times.
for i in range(5):
print(i)
Output:
0
1
2
3
4
In this example, the loop iterates five times, printing the numbers from 0 to 4.
2. while Loop
A while
loop is used to execute a block of code as long as a specified condition is true.
Syntax:
while condition:
# code to execute as long as condition is true
Example: Basic while loop
count = 0
while count < 5:
print("Count is:", count)
count += 1
Output:
Count is: 0
Count is: 1
Count is: 2
Count is: 3
Count is: 4
In this example, the loop continues to execute as long as count
is less than 5, incrementing count
with each iteration.
Loop Control Statements
Python provides control statements that can alter the flow of a loop.
break Statement
The break
statement is used to exit a loop prematurely when a certain condition is met.
Example: Using break in a for loop
for number in range(10):
if number == 5:
break
print(number)
Output:
0
1
2
3
4
In this example, the loop stops when number
equals 5.
continue Statement
The continue
statement skips the rest of the code inside the loop for the current iteration and jumps to the next iteration.
Example: Using continue in a for loop
for number in range(10):
if number % 2 == 0:
continue
print(number)
Output:
1
3
5
7
9
In this example, the loop skips printing even numbers and only prints odd numbers.
pass Statement
The pass
statement is a null operation; it is used as a placeholder for future code.
Example: Using pass in a loop
for number in range(5):
if number == 3:
pass # TODO: Implement logic later
print("Number is:", number)
Output:
Number is: 0
Number is: 1
Number is: 2
Number is: 3
Number is: 4
In this example, when number
equals 3, the pass
statement does nothing, and the loop continues.
Nested Loops
You can nest one loop inside another loop to create more complex iterations.
Example: Nested for loop
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
Output:
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1
In this example, the outer loop iterates 3 times, and for each iteration of the outer loop, the inner loop iterates 2 times.
Summary
- for loop: Iterates over a sequence or iterable object.
- while loop: Continues to execute as long as a specified condition is true.
- break statement: Exits the loop prematurely.
- continue statement: Skips the rest of the code for the current iteration and moves to the next iteration.
- pass statement: Does nothing and can be used as a placeholder.
- Nested loops: Allows for more complex iterations by placing one loop inside another.
These loops and control statements are fundamental for repetitive tasks and iterating over data structures in Python.