Articles → Python → Range Function In Python

Range Function In Python






Purpose





Syntax


range(start, end, step)




  1. Start – This number specifies the starting number from which sequence is going to start. This is optional if we specify end parameter.
  2. End – This number specifies the number where sequence will end. This number is excluded from the sequence.
  3. Step – This is the number by which sequence gets incremented. This is optional. If this parameter is not specified, then sequence gets incremented by 1.

Example


print("Specifying start, end and step")
for x in range(2,21,2): # start = 3, end = 21, step 2
    print (x)
    
print("Mention only start and end")
for x in range(0,6): # start = 0, end = 6
    print (x)

print("Mention only end")
for x in range(10): # end = 10
    print (x)




Picture showing the output of range function in python

Click to Enlarge


Convert Range To List




print(list(range(0,5)))




Picture showing the output of converting range into list

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, March 8, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250