Range Function In Python Python Range Function With Example

🔎 What is range() Function in Python?

The range() function in Python is used to generate a sequence of integers. It is most commonly used in for loops for iteration, but you can also convert it into lists or use it in array-style operations.

range() can take up to three parameters:

  • start (optional) → the beginning of the sequence (default = 0)
  • stop (required) → the end of the sequence (exclusive)
  • step (optional) → the difference between each number (default = 1)
This makes range() extremely powerful for generating number sequences, controlling loops, and working with arrays in Python.

Python range() Function Explained — Syntax & Easy Examples

Learn range() function in Python with 10 simple examples — from basic sequences to negative steps, lists, and reverse loops. Perfect for beginners preparing for interviews or practice.

10 Examples of Using the range() Function in Python

Generate 0 to 9

Print the numbers 0–9 (stop is exclusive)
# Print the numbers 0 to 9
for i in range(10):
    print(i)
Output
0
1
2
3
4
5
6
7
8
9

Generate 3 to 9

Start = 3, Stop = 10 (exclusive)
for i in range(3, 10):
    print(i)
Output
3
4
5
6
7
8
9

Step Size = 2

Even numbers from 0 to 8
for i in range(0, 10, 2):
    print(i)
Output
0
2
4
6
8

-10 to -1

Negative range
for i in range(-10, 0):
    print(i)
Output
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1

Step Size = 3

Counting by 3s in a negative range
for i in range(-10, 0, 3):
    print(i)
Output
-10
-7
-4
-1

-5 to 5 (list)

Range to list conversion
sequence = list(range(-5, 5))
print(sequence)
Output
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]

Create a List (0–9)

list(range(n)) pattern
numbers = list(range(10))
print(numbers)
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

List with Step = 2

Even list using step
numbers = list(range(0, 20, 2))
print(numbers)
Output
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

0.0 to 1.0 (step 0.1)

Tip: range() works with integers; generate 0–10 and divide for clean decimals (avoids float precision issues).
for i in range(0, 11):
    print(i / 10)
Output
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

Reverse: 10 → 1

Descending order using negative step
for i in range(10, 0, -1):
    print(i)
Output
10
9
8
7
6
5
4
3
2
1

These Python range() examples cover start, stop, and step usage, negative ranges, list conversion, and reverse iteration—matching common queries like “range function in python”, “python range examples”, “use of range function in python”.

10 More Python range() Examples with Output

Explore more advanced range() function examples in Python — including loops, arrays, tables, and reverse sequences. These examples target queries like “python range function examples”, “range of array”, “range integer”.

#11 Even Numbers with range()

for i in range(2, 21, 2):
    print(i)
Output:
2 4 6 8 10 12 14 16 18 20

#12 Odd Numbers with range()

for i in range(1, 20, 2):
    print(i)
Output:
1 3 5 7 9 11 13 15 17 19

#13 Multiplication Table

n = 5
for i in range(1, 11):
    print(n, "x", i, "=", n*i)
Output (n=5):
5x1=5 ... 5x10=50

#14 Iterate Array with range(len)

arr = [10, 20, 30, 40]
for i in range(len(arr)):
    print("Index:", i, "Value:", arr[i])
Output:
Index:0 Value:10 ... Index:3 Value:40

#15 Reverse Iteration

for i in range(10, 0, -1):
    print(i)
Output:
10 9 8 7 6 5 4 3 2 1

#16 Range with Zero Step (Error)

# This will raise an error
for i in range(0, 5, 0):
    print(i)
Output:
ValueError: step argument must not be zero

#17 Range to Generate Squares

for i in range(1, 6):
    print(i, "squared is", i**2)
Output:
1 squared is 1 ... 5 squared is 25

#18 Nested Loops with range()

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)
Output:
1 1 ... 3 3

#19 Range with Large Numbers

for i in range(1000, 10001, 2000):
    print(i)
Output:
1000 3000 5000 7000 9000

#20 Convert Range to List of Integers

nums = list(range(1, 6))
print(nums)
Output:
[1, 2, 3, 4, 5]

These extra range() function examples in Python show how to generate even/odd numbers, reverse sequences, arrays iteration, error handling, nested loops, and list creation — helping target long-tail queries like “python for range example”, “range integer”, and “create list python range”.

Python range() MCQ Quiz

10 questions on syntax, outputs, negative steps, and best practices. Click Check per question to see the answer, or Submit Quiz for your total score.

1) What does range(5) generate?




2) In range(start, stop, step), which argument is required?




3) Output of list(range(2, 10, 3)) is:




4) What happens with range(0, 5, 0)?




5) Which prints 10 down to 1?




6) Type of range(5) is:




7) Best way to print 0.0 to 1.0 step 0.1 using range() logic?




8) Output of len(range(2, 10, 2)) is:




9) Which iterates indices of a list arr correctly?




10) Why is range() memory-efficient?




❓ Python Range Function FAQs

What is range() function in Python?

The range() function in Python generates a sequence of numbers. It is widely used in for loops for iteration.

How do you use range() function in a for loop?

Example: for i in range(5): print(i) will print numbers from 0 to 4.

Can range() function generate a list?

Yes. Example: list(range(1,6)) outputs [1,2,3,4,5].

What if step = 0 in range()?

Python throws ValueError: step argument must not be zero.

Does range() support negative steps?

Yes, range(10,0,-1) will print numbers from 10 to 1 in reverse order.

Vista Academy – 316/336, Park Rd, Laxman Chowk, Dehradun – 248001
📞 +91 94117 78145 | 📧 thevistaacademy@gmail.com | 💬 WhatsApp
💬 Chat on WhatsApp: Ask About Our Courses