Generators and Yield keyword in Python

    

   def generator(n):
    for i in range(n):
        yield i

g = (generator(4))
print(iter(g))
print(g) # code from line 6-8 is equivalent
print(g.__iter__())

print(g.__next__()) # code from line 10-12 is equivalent
print(iter(g).__next__())
print(g.__iter__().__next__())

################################

name = "abdur"
print(iter(name))
i = iter(name)
print(i.__next__())
print(i.__next__())
print(i.__next__())
print(i.__next__())
print(i.__next__())
    

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

What is the difference between iostream and iostream.h in cpp?

The Basic Structure of a Full-Stack Web App