
The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed). We have seen that the for statement is such a construct, while an example of a function that takes an iterable is sum(): print(sum(range(4))) # 0 + 1 + 2 + 3. We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space. In many ways, the object returned by range() behaves as if it is a list, but in fact, it isn’t.

#a is indexing the list a with the number iĪ strange thing happens if you just print a range: print(range(10)). 0 reader 1 is 2 doing 3 amazingly 4 well. To iterate over the indices of a sequence, you can combine range() and len() as follows: a = for i in range(len(a)): print(i, a). If you add four numbers inside the range function…you are on your own…actually, you will get this: #you can also do increment for negative numbers for i in range(0, -10, -3): print(i). #when you specify three numbers in the range function, the last #number is considered step increment by the last number instead of #the default which is one. It is possible to let the range start at another number, or to specify a different increment (even negative this is called the ‘step’).Įxample: #when you specify two numbers in the range function, it sees the #first number as start number i.e start from the range of this #number and include it, and the second number as end number i.e end #the range here but don't include this number. So basically range(5) generates 5 values and range(10) generates 10 values the legal indices for items of a sequence of the length of the number specified. The given number is never part of the generated sequence. It will stop the iteration at the number specified. This is because you are telling the for loop to iterate through all the numbers in the range of 0–5 without printing 5. In the example above, you will note the number specified is not specified. Pick your fighter:Ĭlass range(stop) class range(start, stop)Įxample: for i in range(5): print(i).

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops. By default range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. Range() is a built-in function of Python.
#Python range series
This is the first article in the series and I hope you will enjoy reading and implementing the codes. In this article, I will be writing about the range function in python and the powerful things you can do with it.

#Python range code
Python is a beautiful and powerful language to code in and I will be breaking down some functions in it to give you a better understanding of what they can do. Demystifying Python one function at a time.
