Introduction to Python Loops
In Python, loops are used to execute a block of code repeatedly until a certain condition is met. They allow you to automate repetitive tasks and iterate over a collection of items. Python provides two types of loops: for and while.
1. The for Loop
The for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects. It allows you to perform a set of statements for each item in the sequence.
Here’s the basic syntax of a for loop:
for item in sequence:
# code to be executed
Let’s look at an example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
This will output:
apple
banana
cherry
In this example, the for loop iterates over each item in the fruits
list and prints it.
You can also use the range() function to generate a sequence of numbers to iterate over:
for i in range(1, 5):
print(i)
This will output:
1
2
3
4
Here, the for loop iterates over the numbers from 1 to 4 (excluding 5) and prints each number.
2. The while Loop
The while loop is used to repeatedly execute a block of code as long as a certain condition is true. It allows you to perform a set of statements until the condition becomes false.
Here’s the basic syntax of a while loop:
while condition:
# code to be executed
Let’s see an example:
i = 1
while i <= 5:
print(i)
i += 1
This will output:
1
2
3
4
5
In this example, the while loop continues to execute as long as the condition i <= 5
is true. The variable i
is incremented by 1 in each iteration.
It’s important to ensure that the condition inside a while loop will eventually become false, otherwise, the loop will run indefinitely, resulting in an infinite loop.
Loop Control Statements
Python provides several loop control statements that allow you to alter the flow of a loop. Here are some commonly used ones:
- break: Terminates the loop and transfers control to the next statement outside the loop.
- continue: Skips the rest of the current iteration and moves to the next iteration.
Let’s see examples of these control statements:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
if fruit == "banana":
break
print(fruit)
This will output:
apple
In this example, the loop terminates when the fruit
variable becomes “banana” due to the break statement.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
if fruit == "banana":
continue
print(fruit)
This will output:
apple
cherry
In this example, the continue statement skips the iteration when the fruit
variable is “banana” and moves to the next iteration.
Nested Loops
In Python, you can also have loops within loops, known as nested loops. This allows you to perform more complex iterations.
Here’s an example:
for i in range(1, 4):
for j in range(1, 4):
print(i, j)
This will output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
In this example, the outer for loop iterates over the numbers 1 to 3, and for each iteration, the inner for loop iterates over the same range. The print()
function displays the values of both variables i
and j
.
Conclusion
Loops are fundamental in programming and Python provides powerful mechanisms for iteration. The for loop is used to iterate over a sequence, while the while loop is used to repeatedly execute a block of code. Understanding how to use loops and loop control statements will greatly enhance your ability to write efficient and flexible code.
Remember to use loops wisely and ensure that the conditions and control statements are appropriately placed to avoid infinite loops or unexpected results.