Learn.Dedaandsons
Learn.Dedaandsons Python for Beginners
💻 For loop Python

For Loop in Python | Print a Number Series Using a For Loop in Python | how to print number series in python?

Course progress Lesson For loop Python
Lesson 3 / 5

For loop Python | Print number series for loop

Python Basics

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.

01

What is a for loop?

A for loop repeats a block of Python code for each item in a sequence.

02

For loop syntax

This is the basic structure of a for loop in Python:

Python
for variable_name in sequence:
    statements
03

Example: Print numbers from 0 to 9

Let's use range(10) to generate numbers from 0 through 9.

Python
for number in range(10):
    print(number)
Output
0 1 2 3 4 5 6 7 8 9
Python for loop example printing numbers from 0 to 9
A simple Python for loop using range(10).
04

How does this program work?

1
The for statement

The for statement starts the loop.

2
The variable

number stores the current value during each loop. You can use another valid variable name if you prefer.

3
range(10)

range(10) produces numbers starting at 0 and ending before 10.

4
print()

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.

05

Generate a multiplication table

Let's use a for loop to generate the first 10 multiples of a number.

Python
number = int(input("Enter a number: "))

for multiplier in range(1, 11):
    result = number * multiplier
    print(result)
Example

If the user enters:

20

The output is:

20 40 60 80 100 120 140 160 180 200
06

What happens in the exercise?

1
Get a number

input() asks the user to enter a number.

2
Convert the input

int() converts the entered value into an integer.

3
Repeat 10 times

range(1, 11) generates numbers from 1 through 10.

4
Calculate each multiple

The program multiplies the entered number by the current multiplier.

🧠 Try it yourself

Can you change the program?

Modify the loop so it prints numbers from 1 to 20.

Hint: change the value used with 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