Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created May 29, 2020 02:49
Show Gist options
  • Save ZhouYang1993/bf5ca71da036f784a95fdc074b3455ee to your computer and use it in GitHub Desktop.
Save ZhouYang1993/bf5ca71da036f784a95fdc074b3455ee to your computer and use it in GitHub Desktop.
Generators in Python
def times_two(nums):
for n in nums:
yield n * 2
def natural_number(maximum):
x = 0
while x < maximum:
yield x
x += 1
p = times_two(natural_number(10))
print(type(p))
print(next(p))
print(next(p))
print(next(p))
print(next(p))
print(next(p))
print(next(p))
# <class 'generator'>
# 0
# 2
# 4
# 6
# 8
# 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment