Python Range Function

To generate a series of numbers in Python, you can use the range() function. The range() function accepts up to three integer arguments: start, stop, and step. The start parameter tells where the sequence begins and defaults to 0 if not provided. The stop parameter is required and denotes the end of the sequence (this value is not included in the sequence). The optional 'step' parameter specifies the increment step between each number in the series and defaults to 1 if not specified. The range() function returns an immutable sequence that you can iterate over using loops like "for" or "while". In this Python Range Function Example, we generate a sequence of numbers from 0 to 10 with a step of 2. Click Execute to run the Python Range Function Example online and see the result.
Python Range Function Execute
x = range(0,10,2)
for n in x:
  print(n) 
Updated: Viewed: 629 times

What is Python?

Python is a powerful, high-level, dynamically interpreted programming language known for its simplicity and readability of code. Python is widely used in web development to develop robust dynamic websites and applications. Python supports several programming paradigms, including object-oriented, imperative, functional, and procedural. Being a cross-platform language, Python runs on various operating systems, including Windows, Linux, and macOS.

What is the Python Range function?

The Python range() function is a built-in function that generates a sequence of numbers. Typically used in looping structures, it allows developers to control the flow of iterations precisely. The Python range() function can be helpful when you need to repeat a block of code a certain number of times. Also, the Python range() function can create lists or manage indexes in data manipulation tasks.

Python Range Function Examples

The following are examples of using the range() function in Python:

Python Basic Range Function

The most common use of the range() function is to generate a sequence of numbers in a "for" loop.:

Python Basic Range Function Example
for i in range(5):
  print(i)

# output: 
# 0
# 1
# 2
# 3
# 4

Python Range Function with Start and Stop Parameters

The following is an example of a range() function with "start" and "stop" parameters:

Python Range Function with Start and Stop Parameters Example
for i in range(2, 9):
  print(i)

# output: 
# 2
# 3
# 4
# 5
# 6
# 7
# 8

Python Range Function with Step Parameter

The following is an example of a range() function with a third parameter, "step", which specifies the step between each number in the sequence:

Python Range Function with Step Parameter Example
for i in range(0, 10, 2):
  print(i)

# output: 
# 0
# 2
# 4
# 6
# 8

Python Range Function with Negative Step Value

The "step" parameter can also take negative values, which is useful when creating a sequence in reverse order. Below is an example of a range() function with a negative step:

Python Range Function with Negative Step Example
for i in range(10, 0, -2):
  print(i)

# output: 
# 10
# 8
# 6
# 4
# 2

Python Range Function with List

The following is an example of creating lists briefly using the range() function:

Python Range Function with List Example
squares = [i**2 for i in range(1, 5)]
print(squares)

# output: [1, 4, 9, 16]

See also