It is essential to exert maximum effort when dealing with repetitive tasks to avoid mistakes. There are several ways to program such repetitive tasks.
In programming, we refer to loops as control structures that repeatedly execute a specific task a certain number of times (or indefinitely). Python provides two types of loops:
while loopsfor… in… loops, which are very powerfulwhile Statementa = 0 while (a < 7): # (Don't forget the colon!) a = a + 1 # (Don't forget the indentation!) print(a)
The word “while” in English means “when”. The while statement used in the second line tells Python to continuously repeat the following block of code as long as the variable a is less than 7.
Similar to if statements, the while statement begins with a condition. The colon (:) at the end of the line tells Python that a code block follows, which must be indented to the exact same level (using the same number of spaces).
We have now built our first loop, which repeats the indented block of code multiple times. But how does it work?
while Loop Workswhile loop, Python first evaluates the condition inside the parentheses () (the parentheses are optional and were used here for clarity).a = a + 1 increases the value of a by 1 (i.e., reassigns a with its previous value + 1).print(a) displays the current value of a.while statement and checks the condition again:
In our example, as long as the condition a < 7 remains true, the loop will continue executing.
a = 1).n = 3
while n < 5:
print("hello!")The above loop never ends because the value of n does not change inside the loop.
Now, let’s slightly modify the first exercise:
a = 0
while a < 12:
a = a + 1
print(a, a**2, a**3)This program prints a list of squares and cubes for numbers from 1 to 12. The output of this program will look like:
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
11 121 1331
12 144 1728
Note: In the example above, you can see, we can pass multiple arguments to the print() function to display different values on the same line, separated by spaces. Python automatically inserts a space between the displayed elements.
The small program below prints the first ten numbers of the Fibonacci sequence. In this sequence, each number is the sum of the two preceding numbers. Try analyzing the program and describe the function of each line.
a, b, c = 1, 1, 1
while c < 11:
print(b, end=" ")
a, b, c = b, a + b, c + 11 2 3 5 8 13 21 34 55 89
The Fibonacci sequence is displayed on the same line due to the parameter end=" " in the print() function.
By default, print() automatically moves to the next line after displaying a value. However, by specifying end=" ", we replace the newline with a space, keeping all values on the same line. If you remove end=" ", each number will be printed on a new line instead.
To visualize the loop’s execution, we can write down a table manually, like the one below for the Fibonacci program:
| Iteration | a | b | c | Formula Used |
|---|---|---|---|---|
| Start | 1 | 1 | 1 | Initial values |
| 1st | 1 | 2 | 2 | b = a + b |
| 2nd | 2 | 3 | 3 | b = a + b |
| 3rd | 3 | 5 | 4 | b = a + b |
| 4th | 5 | 8 | 5 | b = a + b |
| … | … | … | … | … |
This table tracks how the values of a, b, and c change in each iteration, helping you understand and debug your program in case of a error.
In the next article, we will read about Predefined functions in Python.
No Comments