Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Learn What is Range in Python With Examples

Last updated on Aug 08,2023 13.2K Views

39 / 62 Blog from Python Fundamentals

Python programming language comes with in-built data types like list, dictionary, set, tuple, etc. Range in python is another in-built python datatype which is mainly used with loops in python. It returns a sequence of numbers specified in the function arguments. In this article, we will learn about the range in python in detail with various examples. Following are the topics covered in this blog:

🐍 Ready to Unleash the Power of Python? Sign Up for Edureka’s Comprehensive Python Programming Course Online with access to hundreds of Python learning Modules and 24/7 technical support.

What Is Range In Python?

It is an in-built function in Python which returns a sequence of numbers starting from 0 and increments to 1 until it reaches a specified number. The most common use of range function is to iterate sequence type. It is most commonly used in for and while loops.

Range Parameters

Following are the range function parameters that we use in python:

  • Start – This is the starting parameter, it specifies the start of the sequence of numbers in a range function.
  • Stop – It is the ending point of the sequence, the number will stop as soon as it reaches the stop parameter.
  • Step – The steps or the number of increments before each number in the sequence is decided by step parameter.
range(start, stop, step)

Range With For Loop

Below is an example of how we can use range function in a for loop. This program will print the even numbers starting from 2 until 20.

for i in range(2,20,2):
     print(i)
Output: 2
        4
        6
        8
        10
        12
        14
        16
        18

Increment With Positive And Negative Step

We can use range in python to increment and decrement step values using positive and negative integers, following program shows how we can get the sequence of numbers in both the orders using positive and negative steps values.

for i in range(2, 20, 5):
     print(i, end=", ")
for j in range(25, 0 , -5):
     print(j , end=", ")
Output: 2, 7, 12, 17, 25, 20, 15, 10, 5

Float Numbers In Range

The range function does not support float or non-integer numbers in the function but there are ways to get around this and still get a sequence with floating-point values. The following program shows an approach that we can follow to use float in range.

def frange(start , stop, step):
     i = start
     while i < stop:
             yield i
             i += step

for i in frange(0.6, 1.0, 0.1):
     print(i , end=",")
Output: 0.6, 0.7, 0.8, 0.9

Reverse Range In Python

The following program shows how we can reverse range in python. It will return the list of first 5 natural numbers in reverse.

for i in range(5, 0, -1):
    print(i, end=", ")
Output: 5, 4, 3, 2, 1, 0

Range vs XRange

  • The major difference between range and xrange is that range returns a python list object and xrange returns a xrange object.
  • For the most part, range and xrange basically do the same functionality of providing a sequence of numbers in order however a user pleases.
  • xrange does not generate a static list like range does at run-time. It uses a special technique known as yielding to create values that we need, this technique is used by the object known as generators.
  • If you require to iterate over a sequence multiple times, it’s better to use range instead of xrange.
  • In python 3, xrange does not exist anymore, so it is ideal to use range instead. Any way we can use the 2to3 tool that python provides to convert your code.

Concatenating Two Range Functions

In the program below, there is a concatenation between two range functions.

from itertools import chain 

res = chain(range(10) , range(10, 15))
for i in res:
    print(i , end=", ")
Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

Accessing Range Using Index Values

The following program shows how we can access range using indexes.

a = range(0,10)[3]
b = range(0,10)[5]
print(a)
print(b)
Output: 3
        5

Converting Range To List

The following program shows how we can simply convert the range to list using type conversion.

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

Points To Remember

  • The range function in python only works with integers or whole numbers.
  • Arguments passed in the range function cannot be any other data type other than an integer data type.
  • All three arguments passed can be either positive or negative integers.
  • Step argument value cannot be zero otherwise it will throw a ValueError exception.
  • The range function in python is also one of the data types.
  • You can access the elements in a range function using index values, just like a list data type.

This brings us to the end of this article where we have learned how we can use range in python with several examples including a for loop in python and difference between range and xrange in python. I hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “Range In Python” relevant, check out the Edureka Python Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Python developer. The course is designed to give you a head start into Python programming and train you for both core and advanced Python concepts along with various Python frameworks like Django.

If you come across any questions, feel free to ask all your questions in the comments section of “Range In Python” and our team will be glad to answer.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 30th November,2024

30th November

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Learn What is Range in Python With Examples

edureka.co