Range Function In Python Python Range Function With Example

Understanding the Range Function in Python: Examples and Explanation

10 Examples of Using the Range Function in Python

Generating a Sequence of Numbers from 0 to 9

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

Generating a Sequence from 3 to 9

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

Generating a Sequence with a Step Size of 2

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

Generating a Sequence from -10 to -1

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

Generating a Sequence with a Step Size of 3

for i in range(-10, 0, 3):
  print(i)
Output:
-10
-7
-4
-1

Generating a Sequence from -5 to 5

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

Create a List Using Range Function

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

Create a List with a Step Size of 2

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

Generating a Sequence from 0 to 1 with a Step Size of 0.1

# Since range() only works with integers, we need to use a different approach
i = 0.0
while i <= 1:
    print(i)
    i += 0.1
        

Output:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

Generating a Sequence from 10 to 1 with a Step Size of -1

for i in range(10, 0, -1):
  print(i)
        

Output:

10
9
8
7
6
5
4
3
2
1

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

FAQ ON PYTHON RANGE FUNCTION

The range function is a built-in Python function used to generate a sequence of numbers within a specified range. It is often used in loops, particularly for loops, to iterate over a set of values.

The range function has three forms of syntax:

range(stop) – Generates numbers from 0 up to (but not including) stop.
range(start, stop) – Generates numbers from start up to (but not including) stop.
range(start, stop, step) – Generates numbers from start up to (but not including) stop, incrementing by step

for i in range(5):  # This will iterate from 0 to 4.
    print(i)
for i in range(5, -1, -1):  # This will iterate from 5 to 0 in reverse.
    print(i)
my_range = range(5)
my_list = list(my_range)
print(my_list)  # [0, 1, 2, 3, 4]

No, range does not create a list in memory. It generates numbers on the fly as you iterate through them, which is memory-efficient, especially for large ranges

No, the range function only works with integer values. If you need a sequence of floating-point numbers, you can use a loop with a specified step size.

 This behavior is intentional and follows the convention in Python, where ranges are typically used for zero-based indexing. By excluding the stop value, you ensure that the range goes up to, but does not include, that value.

yes, in Python 3, you can use the range function with non-integer values for start, stop, and step. However, it’s important to note that this behavior is different from Python 2, where range only accepted integers.