Generating Numbers with for in Python
To sequentially print numbers from a given range, use the range
function in a for
loop.
Let's print numbers from 1
to 9
. To do this, we pass one to the first parameter of the range
function. In the second parameter, we specify 10
, since the function selects numbers not including the value of the second parameter:
for num in range(1, 10):
print(num) # 1, 2... 9
Print numbers from 1
to 10
to the console.
Print numbers from 20
to 10
to the console.
Form a list of numbers from 1
to 5
.
Find the sum of integers from 1
to 100
.