Learn Python By Example – Functions Vs. Generators

Functions Vs. Generators

Create A Function


/* Create a function that */
def function(names):
    /* For each name in a list of names */
    for name in names:
        /* Returns the name */
        return name
/* Create a variable of that function */
students = function(['Abe', 'Bob', 'Christina', 'Derek', 'Eleanor'])
/* Run the function */
students
'Abe'

Now we have a problem, we were only returned the name of the first student. Why? Because the function only ran the for name in names iteration once!

Create A Generator

A generator is a function, but instead of returning the return, instead returns an iterator. The generator below is exactly the same as the function above except I have replaced return with yield (which defines whether a function with a regular function or a generator function).


/* Create a generator that */
def generator(names):
    /* For each name in a list of names */
    for name in names:
        /* Yields a generator object */
        yield name
/* Same as above, create a variable for the generator */
students = generator(['Abe', 'Bob', 'Christina', 'Derek', 'Eleanor'])

Everything has been the same so far, but now things get interesting. Above when we ran students when it was a function, it returned one name. However, now that students refers to a generator, it yields a generator object of names!

/* Run the generator */
students
<generator object generator at 0x104837a40>

What can we do this a generator object? A lot! As a generator students will can each student in the list of students:

/* Return the next student */
next(students)
'Abe'
/* Return the next student */
next(students)
'Bob'
/* Return the next student */
next(students)
'Christina'

It is interesting to note that if we use list(students) we can see all the students still remaining in the generator object’s iteration:

/* List all remaining students in the generator */
list(students)
['Derek', 'Eleanor']

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars