s

=

'cat'

# s is an ITERABLE

# s is a str object that is immutable

# s has no state

# s has a __getitem__() method

t

=

iter

(

s

)

# t is an ITERATOR

# t has state (it starts by pointing at the "c")

# t has a __next__() method and an __iter__() method

try

:

print

(

next

(

t

))

# the next() function returns the next value and advances the state

print

(

next

(

t

))

# the next() function returns the next value and advances

print

(

next

(

t

))

# the next() function returns the next value and advances

print

(

next

(

t

))

# next() raises StopIteration to signal that iteration is complete

except

StopIteration

as

e

:

print

(

"StopIteration raised"

)