THE range() FUNCTION:-
Python में range() function एक integer को accept करता है और एक range object return करता है, जो integer के sequence के अलावा कुछ नहीं है।
Syntax: range(start, stop, step)
Rules:- 1. सभी argument integer होना चाहिए, अर्थात string, float या किसी भी अन्य प्रकार को एक range() में start, stop, और step argument में पास नहीं कर सकते।
- सभी तीनों argument positive or negative हो सकते हैं।
- step value शून्य नहीं होना चाहिए। यदि एक step शून्य है तो python में ValueError exception देता है।
Important point:- 1. range is a class
- range is immutable sequence
- range can contain only int type value
- range contains sequence of integers with common deference (Arithmetic progression).
# One parameter:-
range(stop)
Note:- { by default: start=0, step=1}
Example:-
for i in range(5): print(i)
Output 0 1 2 3 4 |
for i in range(6): print(i, end= ‘,’)
Output 0, 1, 2, 3, 4, 5,
|
#Two parameter:-
range(start, stop)
Note:- { by default: step=1}
Example:-
for i in range(3, 6): print(i) Output 3 4 5
|
for i in range(10, 15): print(i, end= ‘,’) Output 10, 11, 12, 13, 14,
|
#Three parameter:-
range(start, stop, step)
Example:-
for i in range(4, 10, 2): print(i) Output 4 6 8
|
for i in range(0, 10, 2): print(i) Output 0 2 4 6 8
|
for i in range(0, 5, 1): print(i) Output 0 1 2 3 4 |
#Going Backward:-
range(start, -stop, -step)
Example:-
for i in range(0, -5, -1): print(i) Output 0 -1 -2 -3 -4 |
THE range() FUNCTION:-
The range() function in Python accepts an integer and returns a range object, which is nothing but a sequence of integers.
Syntax: range(start, stop, step)
Rules:- 1. All arguments must be integer, i.e. cannot pass string, float, or any other type into the start, stop, and step arguments of a range() .
- All three arguments can be positive or negative.
- step value should not be zero. If a step is zero then gives ValueError exception in python.
Important point:-
- range is a class
- range is immutable sequence
- range can contain only int type value
- range contains sequence of integers with common deference (Arithmetic progression).