Loops In Python: For And While

Loops in Python: For and While

Python, a highly regarded programming language, is known for its readability and ease of use, making it an excellent language for both beginning programmers and experienced professionals. Loops, an essential part of any programming language, present no exception to this, and Python’s loop constructs maintain this high standard of quality. In this article, we will introduce you to two types of loops in Python – the for and while loops.


Loops In Python: For And While
Loops In Python: For And While

Throughout this article, we guarantee you an informative and engaging journey, showcasing practical examples for enhanced comprehension.

Understanding Loops in Python

Loops are one of the fundamental concepts in programming. They allow programmers to execute a group of statements multiple times based on certain conditions. In Python, we have two types of loops – for and while. We can utilize these loops in Python to reduce the amount of code and make it more readable.

1. For Loop in Python

The for loop in Python is used to iterate over a sequence (like a list, tuple, dictionary, set, or a string) or other iterable objects. Iterating over a sequence is called traversal.

Here is the basic syntax for the for loop in Python:

for value in sequence:
    # Body of for loop

Here, value is the variable that takes each value in the sequence for each iteration. The for loop continues until it has gone through each item in the sequence. The body of for loop is indented which marks the scope of the loop.

Let’s illustrate this with an example.

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

This script will output:

apple
banana
cherry

Using the range() function with for loop

In Python, the range() function is commonly used with for loop to execute a block of code a certain number of times.

for i in range(5):
    print(i)

This script will output:

0
1
2
3
4

2. While Loop in Python

The while loop in Python is used to iterate over a block of code or statements as long as the test expression (or condition) is true.

Here is the basic syntax for the while loop in Python:

while test_expression:
    # Body of while loop

In the while loop, the body will keep executing until the condition becomes false.

Let’s illustrate with an example.

i = 1
while i < 6:
    print(i)
    i += 1

This script will output:

1
2
3
4
5

In the above example, the while loop will keep executing until i is no longer less than 6.

Using the break statement with loops

In Python, the break statement provides us with an opportunity to exit out of a loop when a certain condition becomes true.

for val in "PythonTimes":
    if val == "i":
        break
    print(val)

This script will output:

P
y
t
h
o
n
T

In this example, the for loop breaks as soon as it encounters “i”.

Using the continue statement with loops

Unlike break, continue doesn’t terminate the loop entirely, instead, it skips the current iteration and proceeds to the next one.

for val in "PythonTimes":
    if val == "i":
        continue
    print(val)

This script will output:

P
y
t
h
o
n
T
m
e
s

In this example, the for loop skips “i” and continues with the next iteration.

Using the else clause with loops

In Python, the else clause can also be used with loops. The else part is executed if the condition in the loop evaluates to False. The else part is also executed if there is a break statement in the loop.

for i in range(5):
    print(i)
else:
    print("Loop exhausted. Exiting the loop.")

This script will output:

0
1
2
3
4
Loop exhausted. Exiting the loop.

In conclusion, loops in Python – the for and while loops – offer easy handling and readability in programs where certain statements need to be executed multiple times based on a condition. The flexibility and versatility provided by these constructs in Python are what make it a favorite among developers worldwide.

Loops are indeed powerful and when coupled with control statements like break and continue, you can develop complex sequence processing applications efficiently. However, use them with caution and mindfully to avoid infinite loops which can lead to memory leaks. Happy Coding!

Share this article:

Leave a Comment