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

Writing Simple Programs in Python
Writing a program, especially for a beginner, is a challenging and daunting task! He/She knows the language but is not able to start. To make things simple, it is advisable to follow a process that is...

Benefits of Design Patterns in Application Development
Application developments is a tedious job. Programmers write code and test it again and again to make sure their software works fine. So designing complex software is a hard nut to crack. Skilled programmers...

Bit-wise operators
Operation Syntax Function Bitwise And a & b and_(a, b) Bitwise Exclusive Or a ^ b xor(a, b) Bitwise Inversion ~ a invert(a) Bitwise Or a b or_(a, b) Left...

Why Python
Python can be used in any futuristics technology A= Analytics Data Science Artificial Intelligence(AI) Neural Network(NN) Natural Language Processing(NLP) Computer Vision(OpenCV) In Analytics...

Python in Unix
Python in Unix How to run command on unix Commands cannot be run directly on a unix server like we do in shell script. SYNTAX: system("Command") check_output("Command", shell=True) call("Command",...
R

Rishi B.

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