Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python range Function

by suresh

The Python range function is one of the Built-in function, which helps to generates sequence of number, or to iterate items in an iterables such as Lists, Tuples etc.

In this section, we discuss how to use this Python range function to generate numbers from a start point to end point with multiple examples.

Python range syntax

The syntax of the range function in Python

range(start, end, step)
  • Start(optional) – This is the starting number of the range. If you omit this value, Python range function will start from 0. 
  • Stop – A value before this number will be the range end value. For example, range(1, 10) print values from 1 to 9.
  • Step – This is an optional argument that decides the sequence of numbers generated by Python range function. It determines the space or difference between each integer values. For example, range(1, 10, 2) returns 1, 3, 5, 7, 9. You can notice that the difference between each integer is 2.

All the arguments in the Python range function accepts integer values. Either it can be positive or Negative integers.

Python range examples

The following list of Python range function examples generate a sequence of numbers from the given starting point to end point using the specified steps. To display the range of items are more number of items in a single statement can be possible by assigning the output to a iterable.

Python List range function

We use a single argument, which will be the stop value. The first statement starts at 0 and ends at 0. The second one starts at 0 and stops at 5. Similarly, the last statement starts at 0 and ends at 9.

Here, we are using the list function to bind and display the numbers generated by the range function.

TIP: Refer the Python List article in Python.

# Python range function example
print(list(range(0)))
 
print(list(range(5)))
 
print(list(range(10)))
Python Range Function 1

Python Tuple range function

In this example also, we used a single argument. However, this time we are using the tuple function to display the items generated by the range function.

TIP: Refer the Python Tuple article.

print(tuple(range(0)))
 
print(tuple(range(5)))
 
print(tuple(range(10)))
Python Range Function 2

Python range function with two arguments

This examples uses range function with two arguments, and they are starting point and end point. For example, print(list(range(5, 15))) print list of values from 5 to 14 and print(list(range(150, 165))) returns list of values from 150 to 164.

print(list(range(0, 5)))
 
print(list(range(5, 15)))
 
print(list(range(10, 15)))
 
print(list(range(25, 33)))
 
print(list(range(150, 165)))
Python Range Function 3

In Python, you can use the negative values as the argument values of the range function. Here, we used negative values as the starting point, ending point, and both start and end points. The first statement starts at -10 and ends at 0, and the second one starts at -15 and ends at -4.

print(list(range(-10, 1)))
 
print(list(range(-15, -5)))
 
print(list(range(-19, -4)))
 
print(list(range(-5, 5)))
 
print(list(range(-122, -110)))
 
print(list(range(0, -5)))
Python Range Function 4

Python range function with three arguments

Using Python range function with three arguments, and they are starting point, ending point and steps. Here, print(list(range(0, 10, 2))) print list of numbers from 0 to 9 and the interval difference between consecutive numbers should be 2. The last statement, print(list(range(150, 500, 50))) return numbers from 150 to 499 with interval 50.

print(list(range(0, 10, 2)))
 
print(list(range(5, 25, 5)))
 
print(list(range(2, 40, 4)))
 
print(list(range(25, 100, 5)))
 
print(list(range(150, 500, 25)))
 
print(list(range(150, 500, 50)))
Python Range Function 5

Python range reverse

This is an example of Python range reserve. It means, you can use the range function to print items in reverse order also.

Here, we are using the negative values as the start point, end point and steps. If you use the negative values as the step argument, the range function returns values in reverse order.

For example, print(list(range(300, 1, -25))) returns numbers from 300 to 1 with an interval difference of 25 (300, 275, 250….).

print(list(range(-10, 1, 2)))
 
print(list(range(-45, -5, 5)))
 
print(list(range(300, 1, -25)))
 
print(list(range(-5, 15, 3)))
 
print(list(range(450, -200, -50)))
 
print(list(range(1000, 1, -75)))
Python Range Function 6

Python for loop range function

Until now, we are using the lists or tuples to hold the range items, and print that list as an output. Generally, we use this range function along with For Loop to iterate any iterables.

The first for loop prints values from 0 to 4, and the next for loop generate values form 0 to 9. However, within the second for loop, we are multiplying each number by 2. So, the output will be 0, 2, 4, 6 etc.

for i in range(5):
    print(i)
 
print('-----------')
for i in range(10):
    print(i * 2)
Python Range Function 7

Using for loop range function with two arguments. To keep the output short, we used the end argument inside the print statement.

for i in range(10, 20):
    print(i, end = ', ')
 
print('\n')
for i in range(150, 160):
    print(i, end = ', ')
 
print('\n')
for i in range(1, 16):
    print(i, end = ', ')
Python Range Function 8

Python for loop range function with three arguments. The first for loop starts at 12 and ends at 119. Here the interval difference between two numbers is 10. 

for i in range(15, 110, 10):
    print(i, end = ', ')
 
print('\n')
for i in range(150, 1, -10):
    print(i, end = ', ')
 
print('\n')
for i in range(-150, -300, -25):
    print(i, end = ', ')
 
print('\n')
for i in range(-300, -150, 25):
    print(i, end = ', ')
 
print('\n')
for i in range(-100, 500, 50):
    print(i, end = ', ')
 
print('\n')
for i in range(400, -200, -50):
    print(i, end = ', ')
Python Range Function 9

Python range string example

Use the range function to print the same statement or same string multiple times. The second statement prints Python from 1 to 4 (four times) 

for i in range(5):
    print('Python')
 
print('-----------')
for i in range(1, 5):
    print('Python')
 
print('-----------')    
for i in range(2**3):
    print('Tutorial Gateway')
Python Range Function 10

In this Python range string example, we declared a string. Next, we used for loop to iterate each character in a string using the range function.

Within the range function, we used len function to find the length of a string and used it as the end point of the range function. It means, range starts at 0 and ends at len(company). Within the print statement, we used the index values to print the characters.

company = 'Tutorial Gateway'
 
for i in range(len(company)):
    print(company[i], end = ', ')
 
print('\n-----------')
for i in range(0, len(company)):
    print(company[i])
Python Range Function 11

Python range iterator example

Let me use this range function to iterate the iterables like list and tuple. The first for loop with range is looping over list of fruits and printing them. Next for loop with range function iterates the tuple and prints the tuple items.

Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
for i in range(len(Fruits)):
    print(Fruits[i])
 
print('--------------')
x = (10, 20, 30, 40, 50, 60, 70, 80, 90)
for i in range(len(x)):
    print(x[i])
Python Range Function 12

Another example of range iterator on string, lists, and tuples.

string = 'Tutorial Gateway'
 
for i in range(len(string)):
    print(string[i], end = ', ')
 
print('\n')
numbers_list = [10, 20, 30, 40, 50]
 
for i in range(len(numbers_list)):
    print(numbers_list[i], end = ', ')
 
print('\n')
fruits_tuple = ('apple', 'cherry', 'kiwi', 'mango')
 
for i in range(len(fruits_tuple)):
    print(fruits_tuple[i], end = ', ')
Python Range Function 13

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved.
About | Contact | Privacy Policy