UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

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
Follow 0

Please Enter a comment

Submit

Other Lessons for You

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)

Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991.
Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What...

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

Python Overview
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python was developed by Gudo vanRossum in the late eighties and early nineties at the National Research Institute...

Decorator in Python - Simplified
By definition, a decorator is a function that takes another function andextends(/decorates) the behaviour of the latter function without explicitly modifying it.Let see how we can apply decorators for...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more