Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • Generators Syntax
  • Examples
  • Now you try it!
  1. Advanced Topics

Lesson 18. Generators

To understand Generator, you should be familiar with Iterator first, because Python Generator is an elegant approach to create an iterator. An Iterator in a nutshell is pointer that points to first element of a collection like list, tuple, or dictionary. Iterator is a class with iter() and next() methods. When we pass iterator object to python's next() function, it points to next element in that collection. When end of iterator is reached an StopIteration is raised, letting you know that are are no further elements. You already see the pain in implementation. This is where the saviour Generator comes into play.

So, a Generator is a function that that uses yield keyword to create an object that could be iterated using next() function. Generator is just like other python functions, but with yield statment instead of return. The difference here is that when return statement is executed, the function stops execution right away which is not the case with yield statement. It keeps generating values and resumes on successive calls. The Generator remembers all local variables when called by subsequent call.

Now, you must be wondering how generators are different from comprehension (Lesson no 19). Well, comprehensions produce the entire list, but generators only give you one element at a time, this is known as lazy operation. Isn't it memory ifficient?

There are two different ways you can create generators in python.

  1. Generators can be created like normal functions with only modification of using yield statment instead of return statement. This is the common way of creating generators.

  2. Generators can be created using comprehension like method using parenthesis instead of square brackets.

Generators Syntax

#Function method
def some_generator_function(some_argument):
    #some expressions
    #and yeild statements
    yield "some variable"
    
#Expression method
generator = (some_function(x) for x in some_collection)

Examples

Example #1: Function Method

Let's create an iterator using Generator by reversing Some company's revenue in million dollars for one week.

def reverse_revenue_generator(x):
    i = len(x)-1
    while i>=0:
        yield x[i]
        i-=1

#company's revenue
revenue = [3,4,10,11,18,21,39]

for rev in reverse_revenue_generator(revenue):
    print(rev)

Example #2: Expression Method

Using the expression method.

# creating a list of numbers
some_vals = [1, 16, 81, 100]

# take square root of each term
list_ = [x**0.5 for x in some_vals]

# This can be done using a generator expression
# generator expressions are surrounded by parenthesis ()
genr = (x**0.5 for x in some_vals)

print(list_)
print(*genr)

Now you try it!

Don't copy and past. Type the code yourself!

PreviousLesson 17. Functional Programing With mapNextLesson 19. Comprehensions

Last updated 3 years ago