Python Range Function
Range() Function
The range() function is used to generate a iterable sequence of integers between a start and stop point with a specific step.The range function generates a in-built object known as range object which can be used to iterate through. This range object is generally used with “for” loop in Python code.
Python Range Function
The range() function accepts three arguments out of which 2 are optional and 1 is mandatory:
1. Start – This is starting point of the sequence, it is optional so if not specified then it is taken as 0 by default.
2. Stop – This is mandatory argument. It defines the upper limit of the sequence.
3. Step – This is the difference between the numbers in sequence, it is optional so if not specified then it is taken as 1 by default.
Syntax –
range(start, stop[, step])
Example –
1.Using only one argument –
>>> for i in range(5): print(i, end=" ") 0 1 2 3 4
2. Using two arguments –
>>> for i in range(5,10): print(i, end=" ") 5 6 7 8 9
3. Using three arguments –
>>> for i in range(2,20,5): print(i, end=" ") 2 7 12 17
4. Reverse sequence-
>>> for i in range(20,15,-1): print(i, end=" ") 20 19 18 17 16
Note –All the arguments passed must be integer as range() function only accepts all arguments in Integer format only.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment