Exercise 5
Write a Python algorithm which displays the first 100 integers: 1 , 2 , 3 , ... , 100.
Solution
#iterate through the numbers from 1 to 100 for i in range(1, 101): # display number i print(i)
In this program:
- we uses a for loop: to iterate through the numbers from 1 to 100 (inclusive)
- and print: each one on a new line using the print function.
- The range function: is used to generate a sequence of integers from 1 to 100.
- The loop variable i: takes on the values in this sequence,
- The print function: is called once for each value of i.
Younes Derfoufi
my-courses.net
[…] Exercise 4 || Solution […]