Find the best tutors and institutes for Python Training

Find Best Python Training classes

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

# What is a generator?

Sudheer S
19/06/2017 0 0

A generator is a function that has one or more yield statements.

Example:

>>>def gen_demo(a):

yield a

a = a+10

yield a

a = a+30

yield a

>>>gen_a = gen_demo(10)

>>>gen_a.next()

10

>>>gen_a.next()

20

>>>gen_a.next()

50

if we run gen_a.next(), it gives StopIteration error because the generator is done with yielding values by this time. Unlike a function's return statement a generator function can remember the point where it has left off, so that when next method is called again it continues yielding values from there after. We will see how to generate datasets with a huge number of elements in our next lesson.

0 Dislike

Please Enter a comment

## Other Lessons for You

Small ML Project on Simple Linear Regression
Here is a small Project on Simple Linear Regression

Damodar | 08 Jul

How Machine Learning Predict things - A Short Video for absolute beginners
Here is a short video for Machine Learning Beginners, who want to know how Machine Learning Algorithm predict things

Damodar | 08 Jul

Python programming - Applications
If you’re thinking of learning Python? Or if you recently started learning it? You may be asking yourself: "What exactly can I use Python for?" There are so many applications for python, but there...

Identity operators
Identity operators Operation Syntax Function Identity a is b is_(a, b) Membership operators Operation Syntax Function Containment obj in seq contains(seq, obj)

Ram Kumar | 12 May

Relational operators
Operation Syntax Function Ordering a < b lt(a, b) Ordering a <= b le(a, b) Equality a == b eq(a, b) Ineqality a != b ne(a, b) Ordering a >= b ge(a, b) Ordering a > b gt(a, b)

Ram Kumar | 12 May

### Looking for Python Training classes?

Find best Python Training classes in your locality on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you