A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-range-method/ below:

Python range() Method - GeeksforGeeks

Python range() Method

Last Updated : 11 Jul, 2025

range() function in Python is used to generate a sequence of numbers. It is widely used in loops or when creating sequences for iteration.

Let’s look at a simple example of the range() method.

Python
# Generate a sequence of numbers from 0 to 4
for i in range(5):
    print(i)

Explanation:

Syntax of range() Method

range(start, stop, step)

Parameters: Return Type: Examples of range() Method 1. Basic Usage with Default Parameters

When range() function called without arguments defaults to starting at 0 and ending at a specified value (exclusive). It generates a sequence of numbers from 0 to that value.

Python
# Generate numbers from 0 to 4
print(list(range(5)))

Explanation:

2. Specifying Start and Stop

We can specify both the starting and stopping points of the range. The range(start, stop) generates numbers starting from the start value up to (but not including) the stop value.

Python
# Generate numbers from 2 to 9
print(list(range(2, 10)))

Output
[2, 3, 4, 5, 6, 7, 8, 9]

Explanation:

3. Using a Step Value

range(start, stop, step) allows us to specify a step value, which controls the difference between consecutive numbers in the range. A positive step generates an increasing sequence, while a negative step generates a decreasing one.

Python
# Generate numbers from 1 to 9 with a step of 2
print(list(range(1, 10, 2)))

Explanation:

4. Using Negative Steps

By specifying a negative step value, the range() function creates a sequence that decrements from the starting point down to the stopping point, providing reverse order numbers.

Python
# Generate numbers from 10 to 1 in reverse
print(list(range(10, 0, -1)))

Output
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Explanation:

Key Characteristics of range()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4