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:

  1. we uses a for loop: to iterate through the numbers from 1 to 100 (inclusive)
  2. and print: each one on a new line using the print function.
  3. The range function: is used to generate a sequence of integers from 1 to 100.
  4. The loop variable i: takes on the values in this sequence,
  5. The print function: is called once for each value of i.




 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 4: python algorithm to print first 100 integers”

Leave a Reply