Range Attributes
Introduction:
- A range object in Python has three main attributes or properties:
startstopstep
Examples:
- Print the properties of a range defined with start and stop:
In this program, we define a range with specific start and stop values. Then we read the three properties and print them to the standard output.
pythonmyrange = range(0, 5) start = myrange.start stop = myrange.stop step = myrange.step print('Start :', start) print('Stop :', stop) print('Step :', step)Output:
Start : 0 Stop : 5 Step : 1- The step value is 1 because if we do not mention a step value when defining a range object, the default step value is 1.
- Print the properties of a range defined with start, stop, and step:
In this program, we define a range with specific start, stop, and step values. Then we read the three properties and print them to the standard output.
pythonmyrange = range(0, 20, 3) start = myrange.start stop = myrange.stop step = myrange.step print('Start :', start) print('Stop :', stop) print('Step :', step)Output:
Start : 0 Stop : 20 Step : 3
Summary:
- In this tutorial, we learned how to read the properties/attributes of a range object in Python. Practice using the
range()function to create sequences of numbers and understand how to define ranges with different start, stop, and step values.