Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4073a63

Browse files
Merge pull request #9 from binamify/main
generator in python
2 parents c2f72b9 + b580284 commit 4073a63

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎Python Basics/generator.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# A simple generator function
2+
def my_gen():
3+
n = 1
4+
print('This is printed first')
5+
# Generator function contains yield statements
6+
yield n
7+
8+
n += 1
9+
print('This is printed second')
10+
yield n
11+
12+
n += 1
13+
print('This is printed at last')
14+
yield n
15+
16+
17+
# Using for loop
18+
for item in my_gen():
19+
print(item)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /