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

Using the Math Library in Python
Using the Math Library in Python. Python provides many useful mathematical functions in a special math library. A library is a module that contains some useful definitions. We now consider...

Shallow And Deep Copying
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): A shallow copy constructs a new compound object...

What is the difference between PHP and PYTHON
Let's start with PHP: - Ideal for standard web development- Easy integration with MySQL- $_GET and $_POST built into the language- C-Esque syntax Primarily used on an Apache server (Routinely, you...


Arithmetic operators
Operation Syntax Function Addition a + b add(a, b) Subtraction a - b sub(a, b) Multiplication a * b mul(a, b) Division a / b truediv(a, b) Concatenation seq1...
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