For loop Python | Print number series for loop
Learn how to use a Python for loop to
generate a sequence of numbers with a simple
beginner-friendly example.
What you'll learn
By the end of this lesson, you'll understand how
a for loop repeats a block of code
and how range() can generate numbers
from 0 to 9.
What is a for loop?
A for loop repeats a block of
Python code for each item in a sequence.
For loop syntax
This is the basic structure of a
for loop in Python:
for variable_name in sequence:
statements
Example: Print numbers from 0 to 9
Let's use range(10) to generate
numbers from 0 through 9.
for number in range(10):
print(number)
0 1 2 3 4 5 6 7 8 9
How does this program work?
The for statement starts
the loop.
number stores the current
value during each loop.
You can use another valid variable name
if you prefer.
range(10) produces numbers
starting at 0 and ending before 10.
print(number) displays the
current number.
Remember
range(10) starts at 0
and stops before 10.
That's why the output contains 0 to 9.
Generate a multiplication table
Let's use a for loop to generate
the first 10 multiples of a number.
number = int(input("Enter a number: "))
for multiplier in range(1, 11):
result = number * multiplier
print(result)
If the user enters:
20The output is:
20 40 60 80 100 120 140 160 180 200
What happens in the exercise?
input() asks the user to
enter a number.
int() converts the entered
value into an integer.
range(1, 11) generates
numbers from 1 through 10.
The program multiplies the entered number by the current multiplier.
Can you change the program?
Modify the loop so it prints numbers from 1 to 20.
range().
Lesson complete
You learned how to use a Python
for loop with
range() to repeat code and
generate number sequences.
For loop Python | Print number series for loop
FOR LOOP IN PYTHON | how to print number series in python | how to print number series in python
Related
About this Python course
This Python course introduces beginners to programming through simple lessons, examples and step-by-step explanations.
The lessons gradually introduce Python concepts such as printing output, variables, data types, input, conditions, loops and other programming fundamentals.