write a python program that uses a for loop to print the numbers 8,11,14,17,20.....86,89
Answers
The syntax of a for loop is: for 'variable' in range():
The range() function has a syntax of it's own.
range(start, stop, step)
- Start indicates the value the series has to start from.
- Stop indicates the value the series has to stop at.
- Step indicates by how much the series must skip values each time.
Out of the three, the stop value is a must. If the start and step values aren't entered, by default, values will start from 0 and skip each time by 1.
What we need to obtain from the for loop is this series:
8, 11, 14, 17, 20, ... 86, 89.
Required code:
>>> for i in range(8, 90, 3):
print(i)
This will print the whole series from 8 to 89.
You might ask why we've written the stop value as 90 instead of 89. This is because the series always stops a number before the given stop value. If you'd have given the stop value as 89, it would've stopped at 88.
[Note that any variable (not just 'i') works in the for loop.]
Answer:
for i in range(8, 90,3)
print(i)