What exactly are iterator, iterable, and iteration?

前端 未结 13 1670
遥遥无期
遥遥无期 2020-11-21 05:27

What is the most basic definition of \"iterable\", \"iterator\" and \"iteration\" in Python?

I have read multiple definitions but I am unable to identify the exact m

13条回答
  •  广开言路
    2020-11-21 06:00

    iterable = [1, 2] 
    
    iterator = iter(iterable)
    
    print(iterator.__next__())   
    
    print(iterator.__next__())   
    

    so,

    1. iterable is an object that can be looped over. e.g. list , string , tuple etc.

    2. using the iter function on our iterable object will return an iterator object.

    3. now this iterator object has method named __next__ (in Python 3, or just next in Python 2) by which you can access each element of iterable.

    so, OUTPUT OF ABOVE CODE WILL BE:

    1

    2

提交回复
热议问题