range() : Python range function generates a list of numbers which are generally used in many situation for iteration as in for loop or in many other cases. In python range objects are not iterators. range is a class of a list of immutable objects. The iteration behavior of range is similar to iteration behavior of list in list and range we can not directly call next function. We can call next if we get an iterator using iter.
Runtime Errors :
Traceback (most recent call last): File "/home/6881218331a293819d2a4c16029084f9.py", line 13, in print(next(demo)) TypeError: list object is not an iterator
Note : Above runtime error clearly indicates that python range is not a iterator.
Because range is iterable so we can get a iterator with the help of them but we can not directly call next in next. Below example explains it clearly
<listiterator object at 0x7f3f32a46450 > 0
range does not generates all numbers that it contains when we create it. It gives only those numbers which we get them using loop. range has following properties.
- range objects are immutable means that they can not be changed again so they can be used as index in dictionaries.
- They have start stop and step arguments .
- same range can be visit again and again
range(1, 31, 2) 1 2 11
Runtime Error : Since element 30 is not present it will rise an error
Traceback (most recent call last): File "/home/cddaae6552d1d9288d7c5ab503c54642.py", line 19, in print(demo.index(30)) ValueError: 30 is not in range
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Iterator Functions in Python | Set 1
- Iterator Functions in Python | Set 2 (islice(), starmap(), tee()..)
- Python __iter__() and __next__() | Converting an object into an iterator
- Python | Difference between iterable and iterator
- Python PIL | ImageSequence.Iterator()
- Iterator Method - Python Design Patterns
- Ways to increment Iterator from inside the For loop in Python
- When to use yield instead of return in Python?
- Python | Return new list on element insertion
- Python | Ways to sum list of lists and return sum list
- Python | Return lowercase characters from given string
- Python return statement
- Difference between Yield and Return in Python
- Python Program to Return the Length of the Longest Word from the List of Words
- Return the Index label if some condition is satisfied over a column in Pandas Dataframe
- PyQt5 QDateTimeEdit – Signal when return key is pressed
- Return multiple columns using Pandas apply() method
- How does the functools cmp_to_key function works in Python?
- Why does Python automatically exit a script when it’s done?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain