Created
September 25, 2018 22:05
-
-
Save pknowledge/cc8918324a486012ed60a5aea6c0242a to your computer and use it in GitHub Desktop.
Python Generators Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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