Python 3 – Loops
Loops are crucial in programming. They allow us to repeat a piece of code multiple times, saving us time and effort. Python has two types of loops, the for loop and the while loop. In this article, we’ll look at each of these loops and show you how to use them.
For Loop
The for loop is used to loop over a sequence (such as a list, tuple, or string) or other iterable object. Here’s the syntax:
for variable in iterable:
# code to be repeated
The variable
takes on the value of each element in the iterable
and the code inside the loop is executed for each value of the variable.
Here’s an example that prints out each element in a list:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
In this example, fruit
is the variable and fruits
is the iterable. The loop runs three times, once for each element in fruits
.
We can also use the range()
function to generate a sequence of numbers to loop over. The range()
function returns a sequence of numbers starting from 0 by default.
Here’s an example that prints out the numbers from 0 to 4:
for i in range(5):
print(i)
Output:
0
1
2
3
4
We can also specify a starting number, ending number, and step size for the range()
function.
for i in range(2, 11, 2):
print(i)
Output:
2
4
6
8
10
In this example, the loop starts at 2, ends at 11 (but doesn’t include 11), and increments by 2 each time.
While Loop
The while loop is used to repeat a block of code while a certain condition is true. Here’s the syntax:
while condition:
# code to be repeated
The condition
is checked at the beginning of each iteration. If it’s true, the code inside the loop is executed. If it’s false, the loop ends.
Here’s an example that prints out the numbers from 0 to 4 using a while loop:
i = 0
while i < 5:
print(i)
i += 1
Output:
0
1
2
3
4
In this example, i
is initially set to 0. The loop runs as long as i
is less than 5. Inside the loop, i
is incremented by 1 each time.
We can also use the break
statement to exit the loop early. Here’s an example that prints out the numbers from 0 to 4, but stops when it reaches 3:
i = 0
while i < 5:
print(i)
if i == 3:
break
i += 1
Output:
0
1
2
3
In this example, the break
statement is executed when i
is equal to 3, causing the loop to end early.
Nested Loops
We can also have loops inside of loops, known as nested loops. Here’s an example that prints out all possible combinations of two dice rolls:
for i in range(1, 7):
for j in range(1, 7):
print(i, j)
Output:
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
2 3
2 4
2 5
2 6
3 1
3 2
3 3
3 4
3 5
3 6
4 1
4 2
4 3
4 4
4 5
4 6
5 1
5 2
5 3
5 4
5 5
5 6
6 1
6 2
6 3
6 4
6 5
6 6
In this example, the outer loop runs 6 times (once for each possible value of the first die). For each value of the first die, the inner loop runs 6 times (once for each possible value of the second die), printing out the two values.
Conclusion
In this article, we’ve covered the two types of loops in Python, the for loop and the while loop. We’ve shown you how to use them to repeat a block of code multiple times, loop over sequences and other iterables, and create nested loops. Loops are a fundamental building block of programming, and mastering them is essential to becoming a proficient Python programmer.