Which range function will create a list with all odd numbers between 5 and 77

>>> my_list = [num for num in range(0, 100) if num % 2 == 0]
>>> print(my_list)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

>>> import random
>>> my_dict = {num:random.randint(0, 100) for num in my_list}
>>> print(my_dict)
{0: 37, 2: 84, 4: 56, 6: 45, 8: 63, 10: 57, 12: 39, 14: 25, 16: 18, 18: 10, 20: 52, 22: 95, 24: 93, 26: 89, 28: 96, 30: 77, 32: 16, 34: 91, 36: 19, 38: 14, 40: 92, 42: 35, 44: 85, 46: 86, 48: 44, 50: 32, 52: 38, 54: 34, 56: 23, 58: 71, 60: 37, 62: 100, 64: 98, 66: 15, 68: 84, 70: 40, 72: 47, 74: 30, 76: 42, 78: 36, 80: 62, 82: 49, 84: 11, 86: 58, 88: 60, 90: 6, 92: 41, 94: 28, 96: 16, 98: 93}

>>> my_set = {num for num in my_dict.values()}
>>> print(my_set)
{6, 10, 11, 14, 15, 16, 18, 19, 23, 25, 28, 30, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 47, 49, 52, 56, 57, 58, 60, 62, 63, 71, 77, 84, 85, 86, 89, 91, 92, 93, 95, 96, 98, 100}

Python

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function generates the immutable sequence of numbers starting from the given start integer to the stop integer. The
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 is a built-in function that returns a range object that consists series of integer numbers, which we can iterate using a
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
4 loop.

In Python, Using a for loop with

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2, we can repeat an action a specific number of times. For example, let’s see how to use the
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function of Python 3 to produce the first six numbers.

Example

# Generate numbers between 0 to 6
for i in range(6):
    print(i)

Output

0
1
2
3
4
5

Which range function will create a list with all odd numbers between 5 and 77
Which range function will create a list with all odd numbers between 5 and 77
Python range() function

Note: As you can see in the output, We got six integers starting from 0 to 5. If you notice,

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 didn’t include 6 in its result because it generates numbers up to the stop number but never includes the stop number in its result.

Solve

  • Python range() and for loop exercise
  • Python for loop Quiz.
  • Summary of range() operations

The

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 works differently between Python 3 and Python 2.

See range() in Python 2

  • In Python 2, we have 
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 and 
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    0 functions to produce a sequence of numbers.
  • In Python 3 
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    0 is renamed to 
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 and original 
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 function was removed. We will discuss it in the later section of the article.

Table of contents

  • How to use range() function in Python
    • Syntax
    • Parameters
    • Return Value
    • Steps to use range() function
  • range() Examples
    • range(stop)
    • range(start, stop)
    • range(start, stop, step)
    • Points to remember about range() function
  • for loop with range()
    • Iterate a list using range() and for loop
    • Practice Problem
  • Reverse range
    • Using negative step
    • Using reversed() function
    • Use range() to reverse a list
  • Python range step
    • Decrementing range() using step
  • Negative range() in Python
  • Convert range() to list
  • Inclusive range
  • range() vs. xrange() in Python 2
  • Concatenating the result of two range()
  • range() indexing and slicing
  • range() over character or alphabet
  • Summary
  • FAQ

How to use range() function in Python

Syntax

Below is the syntax of the range() function.

range(start, stop[, step])

It takes three arguments. Out of the three, two are optional. The

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
4 and
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 are optional arguments and the
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
6 is the mandatory argument.

Parameters

  • # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    4: (Lower limit) It is the starting position of the sequence. The default value is 0 if not specified. For example,
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    8. Here,
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    9 and
    # Numbers from 10 to 15
    # start = 10
    # stop = 50
    # step = 5
    for i in range(10, 50, 5):
        print(i, end=' ')
    
    # Output 10 15 20 25 30 35 40 45
    0
  • # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    6: (Upper limit) generate numbers up to this number, i.e., An integer number specifying at which position to stop (upper limit). The
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 never includes the stop number in its result
  • # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    5: Specify the increment value. Each next number in the sequence is generated by adding the step value to a preceding number. The default value is 1 if not specified. It is nothing but a difference between each number in the result. For example,
    # Numbers from 10 to 15
    # start = 10
    # stop = 50
    # step = 5
    for i in range(10, 50, 5):
        print(i, end=' ')
    
    # Output 10 15 20 25 30 35 40 45
    4. Here,
    # Numbers from 10 to 15
    # start = 10
    # stop = 50
    # step = 5
    for i in range(10, 50, 5):
        print(i, end=' ')
    
    # Output 10 15 20 25 30 35 40 45
    5.

Return Value

It returns the object of class

# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
    print(i, end=' ')

# Output 10 15 20 25 30 35 40 45
6.

print(type(range(10)))
# Output 

Steps to use range() function

The

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function generates a sequence of integer numbers as per the argument passed. The below steps show how to use the range() function in Python.

  1. Pass start and stop values to range()

    For example,

    # Numbers from 10 to 15
    # start = 10
    # stop = 50
    # step = 5
    for i in range(10, 50, 5):
        print(i, end=' ')
    
    # Output 10 15 20 25 30 35 40 45
    8. Here,
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    9 and
    # start = 9
    # stop = 100
    # step = 3 (increment)
    for i in range(9, 100, 3):
        print(i)
    
    0. It will generate integers starting from the
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    4 number to
    # start = 9
    # stop = 100
    # step = 3 (increment)
    for i in range(9, 100, 3):
        print(i)
    
    2. i.e.,
    # start = 9
    # stop = 100
    # step = 3 (increment)
    for i in range(9, 100, 3):
        print(i)
    
    3

  2. Pass the step value to range()

    The

    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    5 Specify the increment. For example,
    # start = 9
    # stop = 100
    # step = 3 (increment)
    for i in range(9, 100, 3):
        print(i)
    
    5. Here,
    # start = 9
    # stop = 100
    # step = 3 (increment)
    for i in range(9, 100, 3):
        print(i)
    
    6. Result is
    # start = 9
    # stop = 100
    # step = 3 (increment)
    for i in range(9, 100, 3):
        print(i)
    
    7

  3. Use for loop to access each number

    Use for loop to iterate and access a sequence of numbers returned by a

    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2.

Which range function will create a list with all odd numbers between 5 and 77
Which range function will create a list with all odd numbers between 5 and 77
Steps to use range()

range() Examples

Now, let’s see all the possible scenarios. Below are the three variants of

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2.

for i in range(1, 10, 2): print("Current value of i is:", i)0

When you pass only one argument to the

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2, it will generate a sequence of integers starting from 0 to
# start = 9
# stop = 100
# step = 3 (increment)
for i in range(9, 100, 3):
    print(i)
2.

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9

Note:

  • Here,
    for i in range(1, 10, 2):
        print("Current value of i is:", i)
    3 and
    # Numbers from 10 to 15
    # start = 10
    # stop = 50
    # step = 5
    for i in range(10, 50, 5):
        print(i, end=' ')
    
    # Output 10 15 20 25 30 35 40 45
    5 as a default value.
  • If you set the
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    6 as a 0 or some negative value, then the range will return an empty sequence.
  • If you want to start the range at 1 use
    for i in range(1, 10, 2):
        print("Current value of i is:", i)
    6.

for i in range(1, 10, 2): print("Current value of i is:", i)7

When you pass two arguments to the

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2, it will generate integers starting from the
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
4 number to
# start = 9
# stop = 100
# step = 3 (increment)
for i in range(9, 100, 3):
    print(i)
2.

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15

Note

  • Here, the
    # Numbers from 10 to 15
    # start = 10
    # stop = 50
    # step = 5
    for i in range(10, 50, 5):
        print(i, end=' ')
    
    # Output 10 15 20 25 30 35 40 45
    5 as a default value.
  • The range will return an empty sequence if you set the
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    6 value lesser than the
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    4.

Current value of i is: 3 Current value of i is: 5 Current value of i is: 7 Current value of i is: 94

When you pass all three arguments to the range(), it will return a sequence of numbers, starting from the start number, increments by step number, and stops before a stop number.

Here you can specify a different increment by adding a

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 parameter.

# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
    print(i, end=' ')

# Output 10 15 20 25 30 35 40 45

Note:

  • Here, the
    Current value of i is: 3
    Current value of i is: 5
    Current value of i is: 7
    Current value of i is: 9
    6 as a default value.
  • Python will raise a
    Current value of i is: 3
    Current value of i is: 5
    Current value of i is: 7
    Current value of i is: 9
    7 exception if you set the
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    5 to 0.

Points to remember about range() function

  • The
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 function only works with the integers, So all arguments must be integers. You can not use float numbers or any other data type as a start, stop, and step value. Please refer to generate a range of float numbers in Python
  • All three arguments can be positive or negative.
  • The
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    5 value must not be zero. If a
    0
    1
    2
    3
    4
    5
    01, Python will raise a
    Current value of i is: 3
    Current value of i is: 5
    Current value of i is: 7
    Current value of i is: 9
    7 exception.

Practice Problem: –

Use 

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 to generate a sequence of numbers starting from 9 to 100 divisible by 3.

Show Solution

# start = 9
# stop = 100
# step = 3 (increment)
for i in range(9, 100, 3):
    print(i)

See: Python for loop and range() exercise

for loop with range()

Python for loop executes a block of code or statement repeatedly for a fixed number of times. We can iterate over a sequence of numbers produced by the range() function using for loop.

Let’s see how to use

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
4 loop with
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function to print the odd numbers between 1 and 10. Using this example, we can understand how the iterator variable
0
1
2
3
4
5
06 is getting value when we use range() with for loop.

for i in range(1, 10, 2):
    print("Current value of i is:", i)

Output

Current value of i is: 3
Current value of i is: 5
Current value of i is: 7
Current value of i is: 9

To understand what

0
1
2
3
4
5
07 means in Python, we need first to understand the working of the
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function.

The

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function uses the generator to produce numbers. It doesn’t generate all numbers at once.

As you know range() returns the

# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
    print(i, end=' ')

# Output 10 15 20 25 30 35 40 45
6 object. A range object uses the same (small) amount of memory, no matter the size of the range it represents. It only stores the start, stop and step values and calculates individual items and subranges as needed.

I.e., It generates the next value only when for loop iteration asked for it. In each loop iteration, It generates the next value and assigns it to the iterator variable i.

  • As you can see in the output, the variable
    0
    1
    2
    3
    4
    5
    06 is not getting the values 1, 3, 5, 7, and 9 simultaneously.
  • In the first iteration of the loop value of
    0
    1
    2
    3
    4
    5
    06 is the start number of a range.
  • Next, In every subsequent iteration of for loop, the value of
    0
    1
    2
    3
    4
    5
    06 is incremented by the step value. The value of
    0
    1
    2
    3
    4
    5
    06 is determined by the formula
    0
    1
    2
    3
    4
    5
    15.

So it means range() produces numbers one by one as the loop moves to the next iteration. It saves lots of memory, which makes range() faster and more efficient.

Which range function will create a list with all odd numbers between 5 and 77
Which range function will create a list with all odd numbers between 5 and 77
Working of Python range function with for loop

Iterate a list using # Print first 10 numbers # stop = 10 for i in range(10): print(i, end=' ') # Output 0 1 2 3 4 5 6 7 8 92 and # Print first 10 numbers # stop = 10 for i in range(10): print(i, end=' ') # Output 0 1 2 3 4 5 6 7 8 94 loop

You can iterate Python sequence types such as list and string using a

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 and for loop.

When you iterate the list only using a loop, you can access only items. When you iterate the list only using a loop, you can only access its items, but when you use range() along with the loop, you can access the index number of each item.

The advantage of using

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 to iterate a list is that it allows us to access each item’s index number. Using index numbers, we can access as well as modify list items if required.

Example

Pass the count of total list items to

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 using a
0
1
2
3
4
5
21 function. The
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 will use it as a
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
6 argument.

0
1
2
3
4
5
0

Output:

0
1
2
3
4
5
1

Practice Problem

Print the following number pattern using Python 

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 and a loop.

0
1
2
3
4
5
2

Show Solution

0
1
2
3
4
5
3

Read More:

  • Python for loop and range() Exercise

Reverse range

You can display the sequence of numbers produced by a

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function by descending order or reverse order.

You can use the following two ways to get the reverse range of numbers in Python.

  • Use a negative
    # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    5 value
  • Use a
    0
    1
    2
    3
    4
    5
    27 function

Which range function will create a list with all odd numbers between 5 and 77
Which range function will create a list with all odd numbers between 5 and 77

Using negative step

Use a negative step value in a

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function to generate the sequence of numbers in reverse order. For example,
0
1
2
3
4
5
29 will produce numbers like 5, 4, 3, 2, and 1.

I.e., you can reverse a loop by setting the step argument of a

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 to -1. It will cause the
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
4 loop to iterate in reverse order.

Let’s see how to loop in a reverse iteration or backward iteration to display a range of numbers from 5 to 0.

0
1
2
3
4
5
4

Output:

0
1
2
3
4
5
5

Using reversed() function

Using Python’s built-in

0
1
2
3
4
5
27 function, you can reverse any sequence such as list or range.

  • Pass the
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 as an input to the reversed() function, It returns a
    0
    1
    2
    3
    4
    5
    34 that accesses the sequence of numbers provided by
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 in the reverse order.
  • Next, iterate the result provided by
    0
    1
    2
    3
    4
    5
    27 function using for loop.

Example 2: reverse range starting from 20 to 10

0
1
2
3
4
5
6

Example 3: reverse range starting from 20 to 10 with step 2

0
1
2
3
4
5
7

Note: The

0
1
2
3
4
5
37 returns a
0
1
2
3
4
5
34 that accesses the sequence of numbers provided by
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 in the reverse order.

0
1
2
3
4
5
8

Also, If you need the list out of it, you need to convert the output of the

0
1
2
3
4
5
27 function to list. So you can get the reverse list of ranges.

Use range() to reverse a list

Use

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 to reverse a list by passing the count of list items as a
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
4 argument and
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 as a -1.

Let’s see the various ways to reverse a list of numbers using a

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2

0
1
2
3
4
5
9

Python range step

A step is an optional argument of a range(). It is an integer number that determines the increment between each number in the sequence. i.e., It specifies the incrementation.

You can also define it as a difference between each preceding and next number in the result sequence. For example, If the step is 2, then the difference between each preceding and following number is 2

The default value of the step is 1 if not specified explicitly.

Example: Increment using step

range(start, stop[, step])
0

You can also perform lots of operations by using step arguments such as reverse a sequence such as a list and string.

Decrementing range() using step

You can decrement range() by using negative

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 value.

When we set the negative value to step, In each iteration, the number will go down until it reaches to stop number.

range(start, stop[, step])
1

Note: To decrement

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 the
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
4 must be greater than
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
6. A range() return empty sequence if
0
1
2
3
4
5
49.

range(start, stop[, step])
2

Also, you can use

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 to generate sequence of numbers multiply of n.

range(start, stop[, step])
3

Also, you will get a

0
1
2
3
4
5
51 if you set
Current value of i is: 3
Current value of i is: 5
Current value of i is: 7
Current value of i is: 9
6.

range(start, stop[, step])
4

Also, you can’t use the decimal

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 value. If you want to use the float/decimal step in the
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2, please refer to generating a range of float numbers.

Negative range() in Python

You can use negative integers in range().

Most of the time, we use the negative step value to reverse a range. But apart from the step, we can use negative values in the other two arguments (start and stop) of a range() function.

Example: Negative range from -1 to -10

Let’s see the example to print the range of numbers from negative to positive.

range(start, stop[, step])
5

Let’s understand the above program, we set –

  • 0
    1
    2
    3
    4
    5
    55 (because we wanted to start producing number from -1)
  • 0
    1
    2
    3
    4
    5
    56 (We want to stop generating numbers when we reach -11)
  • 0
    1
    2
    3
    4
    5
    57

Execution:

  • In the 1st iteration of the loop,
    0
    1
    2
    3
    4
    5
    06 is -1
  • In the 2nd iteration of for loop,
    0
    1
    2
    3
    4
    5
    06 is -2 because
    0
    1
    2
    3
    4
    5
    60, and it will repeat this process till the stop number.

Example: Negative reverse range from -10 to -1

You can also print the negative reverse

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 using a positive
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 integer.

range(start, stop[, step])
6

Combination of negative and positive numbers

range(start, stop[, step])
7

Convert range() to list

Python

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function doesn’t return a
0
1
2
3
4
5
64 type. It returns an immutable sequence of integers.

We can convert

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 to list using a
0
1
2
3
4
5
66 constructor.

  • Pass the
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 function as an input to the list constructor.
  • The
    0
    1
    2
    3
    4
    5
    66 constructor automatically creates a list by enclosing the integers returned by the
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 inside the square brackets.
range(start, stop[, step])
8

Access and modify list item using

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2

Also, you can use

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 to access and modify
0
1
2
3
4
5
64 items.

  • Using a
    0
    1
    2
    3
    4
    5
    21 function, you can get a count of list items.
  • Next, use this count as a stop number in
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 and iterate for loop
    0
    1
    2
    3
    4
    5
    75 times.
  • In each iteration, you will get the index number of a current list item.
range(start, stop[, step])
9

Inclusive range

In this section, we will learn how to generate an inclusive range in Python. By default, The

0
1
2
3
4
5
76 is exclusive, so it doesn’t include the last number in the result. It creates the sequence of numbers from
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
4 to
# start = 9
# stop = 100
# step = 3 (increment)
for i in range(9, 100, 3):
    print(i)
2.

For example,

0
1
2
3
4
5
79 will produce
0
1
2
3
4
5
80. The result contains numbers from 0 to up to 5 but not five.

If you notice, the result contains 5 elements which equal to

0
1
2
3
4
5
81. Note, the index always starts from 0, not 1.

If you want to include the end number in the result, i.e., If you want to create an inclusive range, then set the stop argument value as

0
1
2
3
4
5
82.

Example

print(type(range(10)))
# Output 
0

Example 2: Even inclusive range()

print(type(range(10)))
# Output 
1

The

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 vs
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
0 comparison is relevant only if you are using Python 2 and Python 3. If you are not using Python 2 you can skip this comparison.

The range() function works differently between Python 3 and Python 2. If your application runs on both Python 2 and Python 3, you must use

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 instead of
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
0 for better code compatibility.

In Python 2, we have

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 and
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
0 functions to produce a sequence of numbers.

In Python 3

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
0 is renamed to
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 and original
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function was removed.

So in simple terms,

# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
0 is removed from Python 3, and we can use only the
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function to produce the numbers within a given range.

Use of

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 and
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
0

  • In Python 2,
    # Print first 10 numbers
    
    # stop = 10
    for i in range(10):
        print(i, end=' ')
    
    # Output 0 1 2 3 4 5 6 7 8 9
    2 returns the
    0
    1
    2
    3
    4
    5
    64 object, i.e., It does generate all numbers at once. The
    range(start, stop[, step])
    00 will generate a Python list of 499 integers in memory. So It consumes high memory and increases the execution time.
  • # Numbers from 10 to 15
    # start = 10
    # stop = 16
    for i in range(10, 16):
        print(i, end=' ')
    
    # Output 10 11 12 13 14 15
    0: The
    range(start, stop[, step])
    02 function doesn’t generate all numbers at once. It produces numbers one by one as the loop moves to the next number. So it consumes less memory and resources.

Example

print(type(range(10)))
# Output 
2

Output

print(type(range(10)))
# Output 
3

Concatenating the result of two range()

Let say you want to add

range(start, stop[, step])
03. And you want the concatenated range like 
range(start, stop[, step])
04.

For example, you want to add the result of two

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 functions to produce another sequence of numbers. You can add/merge the result of multiple
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 functions using
range(start, stop[, step])
07.

print(type(range(10)))
# Output 
4

range() indexing and slicing

Built-in function

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 is the constructor that returns a
# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
    print(i, end=' ')

# Output 10 15 20 25 30 35 40 45
6 object, this range object can also be accessed by its index number using indexing and slicing.

Access range() attributes

It is essential to know the

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 attributes when you receive it as input to your function, and you wanted to see the value of the
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
4,
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
6 and
# Numbers from 10 to 15
# start = 10
# stop = 16
for i in range(10, 16):
    print(i, end=' ')

# Output 10 11 12 13 14 15
5 argument.

print(type(range(10)))
# Output 
5

Indexing

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 supports both positive and negative indices. The below example demonstrates the same.

In the case of

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2, The index value starts from zero to (stop). For example, if you want to access the 3rd number, we need to use 2 as the index number.

print(type(range(10)))
# Output 
6

Negative indexing

The numbers can be accessed from right to left by using negative indexing.

print(type(range(10)))
# Output 
7

Slicing

Slicing a implies accessing a portion from

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2

print(type(range(10)))
# Output 
8

range() over character or alphabet

Is there a way to print a range of characters or alphabets? For example like this.

print(type(range(10)))
# Output 
9

Is there a way to print a range of characters or alphabets? For example like this. It is possible to create a range of characters using the custom generator. Let’s see how to generate the ‘a’ to ‘z’ alphabet using the custom

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function.

Note: We need to use the ASCII value and then convert the ASCII value to a letter using a

range(start, stop[, step])
18 function.

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
0

Output

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
1

Summary

I want to hear from you. What do you think of this guide on Python range()? Let me know by leaving a comment below.

Also, try to solve the Python loop Exercise and for loop Quiz.

Below is the summary of all operations that we learned in this lesson

OperationDescription
for i in range(1, 10, 2):
    print("Current value of i is:", i)
0Generate a sequence of integers from zero to stop-1
for i in range(1, 10, 2):
    print("Current value of i is:", i)
7Generate a sequence of integers from start to stop-1
Current value of i is: 3
Current value of i is: 5
Current value of i is: 7
Current value of i is: 9
4Generate a sequence of integers starting from the start number, increments by step, and stops before a stop number. I.e., Each next number is generated by adding the step value to a preceding number.
range(start, stop[, step])
22Reverse range
range(start, stop[, step])
23Reverse range using a
0
1
2
3
4
5
27 function
range(start, stop[, step])
25Negative range from -1 to -10
range(start, stop[, step])
26Convert range() to list
range(start, stop[, step])
27Generate an inclusive range
range(start, stop[, step])
28Access fifth number of a
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 directly
range(start, stop[, step])
30Slice a range to access numbers from index 3 to 8
range(start, stop[, step])
31Get the start value of a
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2
range(start, stop[, step])
33Get the stop value of a
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2
range(start, stop[, step])
35Get the step value of a
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2Usage of Python range()

FAQ

Does range() in Python start at 0?

The

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 by default starts at 0, not 1, if the start argument is not specified. For example,
0
1
2
3
4
5
79 will return 0, 1, 2, 3, 4.

What does range() return in Python?

The

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 function returns an object of class
# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
    print(i, end=' ')

# Output 10 15 20 25 30 35 40 45
6, which is nothing but a series of integer numbers.

Is range a list in Python?

No.

# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 is not a list, nor it returns a list type. A
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 return
# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
    print(i, end=' ')

# Output 10 15 20 25 30 35 40 45
6 object. You can verify the data type of
# Print first 10 numbers

# stop = 10
for i in range(10):
    print(i, end=' ')

# Output 0 1 2 3 4 5 6 7 8 9
2 using the
range(start, stop[, step])
45 function.