Understanding Loops in Python
Loops are a fundamental concept in programming that allow you to repeat a specific block of code. In Python, there are two main types of loops: for
and while
.
while
Loop
The while
A loop in Python is used to repeatedly execute a block of code as long as the test expression (or condition) evaluates to True
.
Syntax:
while test_expression:
# body of while loop
In a while
In a loop, the condition is evaluated before each iteration. The body of the loop is executed only if the condition is True
. After each iteration, the condition is checked again. This continues until the condition evaluates to False
.
The body of the while
the loop is defined by indentation. The block of code inside the loop starts with an indented line, and the loop ends when the indentation is no longer present.
Example:
i = 1
while i < 6:
print(i)
i += 1
Output:
1
2
3
4
5
In this example, the loop continues as long as i
it is less than 6. The variable i
is incremented with each iteration. If we forget to increment i
, the loop will run indefinitely, creating an infinite loop.
while
Loop with else
A while
the loop can also have an optional else
block. This block is executed when the condition evaluates to False
.
Example:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Output:
1
2
3
4
5
i is no longer less than 6
Here, when i
At 6, the condition becomes False
, and the else
the block is executed.
for
Loop
A for
the loop is used to iterate over a sequence (like a list, tuple, or string) or any other iterable object.
Syntax:
for variable in sequence:
# body of for loop
The loop will continue until all items in the sequence are processed. As with the while
loop, the body of the for
the loop is determined by indentation.
Example:
for x in "programming":
print(x)
Output:
p
r
o
g
r
a
m
m
i
n
g
range()
Function
To loop through a block of code a specific number of times, the range()
function can be used. It generates a sequence of numbers, starting from 0 by default and incrementing by 1 (unless specified otherwise), ending before a given number.
Example:
for x in range(7):
print(x)
Output:
0
1
2
3
4
5
6
Note that range(7)
generates numbers from 0 to 6, not 0 to 7.
You can also specify a starting point and an increment. For example, range(3, 7)
will generate values from 3 to 6 (but not including 7).
Example:
for x in range(3, 7):
print(x)
Output:
3
4
5
6
To change the increment value, you can add a third parameter. For instance, range(1, 10, 2)
will increment by 2:
Example:
for x in range(1, 10, 2):
print(x)
Output:
1
3
5
7
9
else
in for
Loop
In a for
loop, the else
keyword specifies a block of code to be executed when the loop completes all iterations, without encountering a break
statement.
Example:
for x in range(5):
print(x)
else:
print("Finished Counting!")
Output:
0
1
2
3
4
Finished Counting!
In this example, the loop prints all the numbers from 0 to 4, and then the else
the block is executed, printing the message "Finished Counting!"
.
Leave a Reply