What exactly are iterator iterable and iteration

0 votes
What is the most basic definition of "iterable", "iterator" and "iteration" in Python?
Feb 11, 2022 in Python by Nandini
• 5,480 points
913 views

1 answer to this question.

0 votes

Iteration is a process in which one single element is taken at a time given n elements.
Iterable is an object that one iterates over elements,
while iterator is an object that generates values during iteration and is also returned by the iterable object.
Iterable object implements __iter__() method and returns an iterator object.

Python Built-in iterable objects are list, sets, strings,
If __iter__() method does not work then __getitem__() is used.
Iterators implement the __next__() method. This means that the next values are returned until all the values are exhausted. Also the iterator object gets updated.
Let us take an example:

Obj1 = 'Pie'     # Obj1 is an string and also is Iterable

Piece = iter(Obj1)  #Piece is Iterator
next(Piece)
'P'
next(Piece)
'i'
next(Piece)
'e'
next(Piece)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-26-aad7e70238c3> in <module>
----> 1 next(Piece)

StopIteration: 

answered Feb 11, 2022 by Dev
• 6,000 points

Related Questions In Python

0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
870 views
0 votes
1 answer

What are the differences between type() and isinstance()?

To summarize the contents of other (already ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
702 views
0 votes
1 answer

What are the differences between type() and is instance()?

Normally, in Python, you want your code ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 58,020 points
669 views
0 votes
1 answer

What is getattr() exactly and how do I use it?

You can view a full example here: http://www.diveintopython.net/power_of_introspection/index.html Introspection ...READ MORE

answered Sep 17, 2018 in Python by bug_seeker
• 15,510 points
831 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,467 views
0 votes
1 answer
0 votes
1 answer

What is the difference between sets and lists in Python?

Lists can contain duplicates but sets cannot Sets ...READ MORE

answered Feb 9, 2022 in Python by Dev
• 6,000 points
1,174 views
0 votes
2 answers

What's the difference between %s and %d in Python string formatting?

The reason is that they are using ...READ MORE

answered Feb 8, 2022 in Python by Rahul
• 9,680 points
22,935 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP