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 the sequence is going to start. This is optional if we specify the end parameter
  2. End → This number specifies the number where the sequence will end. This number is excluded from the sequence
  3. Step → This is the number by which the sequence gets incremented. This is optional. If this parameter is not specified, then the 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)



Output


Picture showing the output of range function in python



Convert Range To List




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



Output


Picture showing the output of converting range into list



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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250