Created
May 29, 2020 02:49
-
-
Save ZhouYang1993/bf5ca71da036f784a95fdc074b3455ee to your computer and use it in GitHub Desktop.
Generators in Python
This file contains hidden or 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
| 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