Conditional Constructs and Looping

For (Range Function)

range() is a built-in function of the Python language. We use it when we need to perform an action any specific number of times. range() in Python(3.x) is just a retitled version of a function known as xrange in Python(2.x). The range() function is useful for generating a sequence of some numbers. We will study range function in detail.

range function () has a common use for looping hence, knowledge of the same is the key aspect when dealing with any type of Python code. The most common and popular use of the range() function in Python is to iterate sequence type (List, string, etc.) with ‘for’ and ‘while’ loop.

range function

Python range() Basics:

In simple words, range() permits the user to create a series of numbers within a set range. Depending on the number of the arguments user is passing to that function, the user can decide where that series of numbers will start and end as well as how much the difference will be there between one number and the next one. range() takes mostly 3 arguments, which are as follows:

  • start: integer beginning from which the sequence of integers is to be reverted
  • stop: integer before which the arrangement of integers is to be reverted.
  • The range of integers closes at stop – 1.
  • step: integer value which defines the increment between each integer in the order
                    

# Python Program to

# show range() basics

# printing a number

for i in range(20):

print(i, end =" ")

print()

# using range for iteration

l = [20, 30, 40, 50]

for i in range(len(l)):

print(l[i], end =" ")

print()

# performing sum of natural

# number

sum = 0

for i in range(1, 11):

sum = sum + i

print("Sum of first 10 natural number :", sum)

Output :

                    

0 1 2 3 4 5 6 7 8 9

10 20 30 40

Sum of first 10 natural number : 55

There are three ways you can call range(), these are as follows:

range(stop) takes 1 argument.

range(start, stop) takes 2 arguments.

range(start, stop, step) takes 3 arguments.

 

range(stop)

When we call range() with 1 argument, we will get a sequence of numbers that starts at ‘0’ and comprises every whole number up to but not containing, the number that we have delivered as the stop.

range(start, stop)

When we call range() with 2 arguments, we get to choose not only where the sequence of the numbers stops but also where it will start from, so we don’t have to start at ‘0’ every single time. We can use range() to make a series of numbers from ‘X’ to ‘Y’ with the use of a range(X, Y).

range(start, stop, step)

When we call range() with 3 arguments, we can select not only where the sequence of the numbers will start from and stop at but also how vast the difference will be there between one number and the next one.

Using the

float Numbers

in the Python range():

Python range() function doesn’t support the float numbers. i.e. we cannot use the floating-point or the non-integer number in any of its arguments. We can only use integer numbers.

Browse more Topics Under Conditional Constructs and Looping

FAQ on For Range Function

Question 1: What actions do a range () function performs?

Answer 1: The range() function reverts a series of numbers, beginning from ‘0’ by default, and increases by ‘1’, and stops before a stated number.

Question 2: What is the range actually for?

Answer 2: The Range is actually the difference between the lowermost and uppermost values. For example: In {4, 5, 9, 3, 8} the lowest value is ‘3’, and the highest value is ‘9’. So the range will be 9 − 3 = ‘6’.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.