Skip to content

Instantly share code, notes, and snippets.

@pknowledge
Created September 25, 2018 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pknowledge/cc8918324a486012ed60a5aea6c0242a to your computer and use it in GitHub Desktop.
Save pknowledge/cc8918324a486012ed60a5aea6c0242a to your computer and use it in GitHub Desktop.
Python Generators Example
# Python Generators Example 1
def my_generators_func1():
yield 'a'
yield 'b'
yield 'c'
x = my_generators_func1()
print(next(x))
print(next(x))
print(next(x))
# Python Generators Example 2
def my_generators_func2():
for i in range(5):
print('--------------------', i)
yield i
x = my_generators_func2()
print(next(x))
print(next(x))
print(next(x))
print(next(x))
print(next(x))
# Python Generators Example 3
def list_iterator(list):
for i in list:
yield i
a = [1, 2, 3, 6, 5, 4]
mylist = list_iterator(a)
for x in mylist:
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment