Loops are an essential concept in programming, allowing you to repeat a block of code multiple times. In Python, there are three primary types of loops: for loops, while loops, and nested loops. Each has its unique use case and helps streamline repetitive tasks in your code. Let’s dive deeper into each type of loop and understand when and how to use them.
1. For Loops
A for loop in Python is used to iterate over a sequence (like a list, tuple, string, or range) and execute a block of code for each item in the sequence. This is particularly useful when you know in advance how many times you need to iterate.
Syntax:
python
Copy code
for item in sequence:
# code block
Example:
python
Copy code
fruits = [‘apple’, ‘banana’, ‘cherry’]
for fruit in fruits:
print(fruit)
Output:
Copy code
apple
banana
cherry
In this example, the for loop iterates over each item in the fruits list and prints it.
2. While Loops
A while loop in Python runs as long as a specified condition is True. It is ideal when you don’t know in advance how many iterations are needed, but you want the loop to continue until a certain condition is met.
Syntax:
python
Copy code
while condition:
# code block
Example:
python
Copy code
count = 0
while count < 3:
print("Count is:", count)
count += 1
Output:
csharp
Copy code
Count is: 0
Count is: 1
Count is: 2
In this example, the loop continues until the count reaches 3. The loop checks the condition before each iteration.
3. Nested Loops
A nested loop is simply a loop inside another loop. Nested loops are often used when you need to iterate over multi-dimensional data structures, like lists of lists or matrices.
Syntax:
python
Copy code
for item in outer_sequence:
for inner_item in inner_sequence:
# code block
Example:
python
Copy code
for i in range(3):
for j in range(2):
print(f"i = {i}, j = {j}")
Output:
css
Copy code
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 runs three times (for i), and for each iteration of the outer loop, the inner loop runs twice (for j). The result is a combination of all possible pairs.
Control Statements Inside Loops
You can also control the flow of loops using the following statements:
break: Exits the loop completely.
continue: Skips the current iteration and moves to the next one.
else: Executes a block of code when the loop finishes normally (without hitting a break).
Example with break and continue:
python
Copy code
for i in range(5):
if i == 3:
break # Exits the loop
if i == 1:
continue # Skips to the next iteration
print(i)
Output:
Copy code
0
2
In this example, the loop skips the iteration when i is 1 and exits when i reaches 3.
When to Use Which Loop?
Use a for loop when you know the number of iterations or are iterating over a sequence.
Use a while loop when the number of iterations is unknown and the loop depends on a condition.
Use nested loops when you need to work with multi-dimensional data or combinations of items.
Conclusion
Python loops are incredibly powerful tools for automating repetitive tasks. The choice between a for loop, while loop, or nested loop depends on your specific use case. Understanding when and how to use these loops will make your Python programming more efficient and effective. By mastering loops, you’ll be able to write cleaner, more concise code that handles complex tasks with ease. Happy coding!
Visit –
Python classes in Pune
Leave a comment