Iterators and iterables in Python



Hello guys this is Satyam again back with another awesome blog and in this blog I am going to show you the difference between the Iterator and Iterable in Python in a very basic language.

Suppose guys,you are a Python programmer and if you are reading this article obviously you are a Python programmer now in looping while you loop thought a given list or string or tuple all are togetherly known as Iterables that is something which is looped or iterated through loops and the technique of loop through a given value is called iteration or iterating over it.

Before diving into the Iterators you should know about the __iter__() method.So the iter() function returns an iterator for the given object. The iter() function creates an object which can be iterated one element at a time. These objects are useful when coupled with loops like for loop, while loop.

Now let's talk about Iterators,
A list is an iterable. But it is not an iterator. If we run the __iter__() method on our list, it will return an iterator. An iterator is an object with a state that remembers where it is during iteration. Iterators also know how to get their next value. They get their next value with a __next__() method. Thus, to know if an object is an iterator, we can check if it has a __next__() method.


Thanks for Reading
Share:

Post a Comment

Copyright © DevTipsNTricks. Designed by OddThemes