How can I use Python s range function

+4 votes
I have a set of numbers and I don't want to print all the numbers separateley.How does python's range function can help me?
Mar 31, 2018 in Python by hemant
• 5,790 points
2,126 views

8 answers to this question.

+2 votes

You can use a range function whenever you want to run a piece of code more than once. You can use "for" loop. Look at the below example:

fruits = ['Banana', 'Apple',  'Grapes']

for index in range(len(fruits)):

   print (fruits[index])

Notice here, we have specified the range, that means we know the number of times the code block will be executed.

Output:

Banana

Apple

Grapes
answered Mar 31, 2018 by DareDev
• 6,890 points
+2 votes

The range function is mostly used in for-loop. 

Ex:

for i in range(0,5):

    print(i)

The output will be:

0

1

2

3

4


Note: When you use the range() function, the 2nd parameter is excluded in the range.

answered Aug 21, 2018 by Omkar
• 69,220 points
+1 vote

range(x) returns a list of numbers from 0 to x - 1.

>>> range(1)
[0]
>>> range(2)
[0, 1]
>>> range(3)
[0, 1, 2]
>>> range(4)
[0, 1, 2, 3]

for i in range(x): executes the body (which is print i in your first example) once for each element in the list returned by range(). i is used inside the body to refer to the “current” item of the list. In that case, i refers to an integer, but it could be of any type, depending on the objet on which you loop.

answered Oct 12, 2018 by findingbugs
• 4,780 points
+1 vote
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
answered Oct 12, 2018 by abc
+1 vote
# You can use range() wherever you would use a list. 

a = range(1, 10) 
for i in a: 
    print i 

for a in range(21,-1,-2):
   print a

#output>> 21 19 17 15 13 11 9 7 5 3 1
answered Oct 12, 2018 by rani
+1 vote
# We can use any size of step (here 2)
>>> range(0,20,2)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

>>> range(20,0,-2)
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
answered Oct 12, 2018 by kalpesh
+1 vote
# The sequence will start at 0 by default. 
#If we only give one number for a range this replaces the end of range value.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
answered Oct 12, 2018 by Karunesh
+1 vote
# If we give floats these will first be reduced to integers. 
>>> range(-3.5,9.8)
[-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
answered Oct 12, 2018 by rajiv

Related Questions In Python

0 votes
1 answer

How can I use the power function in python 2.7?

print(pow(3,4)) #this will return the exponentiation of 3 ...READ MORE

answered May 21, 2019 in Python by Mohammad
• 3,230 points
1,420 views
0 votes
1 answer
0 votes
1 answer

How can I use the sleep function in a python program?

You can use sleep as below. import time print(" ...READ MORE

answered Jun 10, 2020 in Python by anonymous
1,002 views
0 votes
1 answer

How can I build a recursive function in python?

I'm wondering whether you meant "recursive". Here ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,147 views
+1 vote
3 answers

How can I use python to execute a curl command?

For sake of simplicity, maybe you should ...READ MORE

answered Oct 11, 2018 in Python by charlie_brown
• 7,720 points
94,429 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,890 points
20,016 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP