[Python Programming Basics to Advanced]: Lab 31: Generators in Python

Pubblicato il: 17 maggio 2021
sul canale di: Learning Orbis
630
14

This Python programming playlist is designed to take beginners with zero programming experience to an expert level. The course covers installation, basic syntax, practical scenarios, and efficient logic building. The course material includes PDF handouts, review questions, and covers a wide range of topics, from data types to advanced functions like Lambda and Recursive functions, Generators, and JSON data parsing.


In this lesson we will study about generators in Python. Generators are memory efficient. Generators can be created in two ways; Generator Expression and Generator Functions.

We will also see the difference between Iterator and Iterable. Moreover, there will be discussion on difference between return statement and yield statement.

The last lesson on zip, map and filter which also produces an iterator:
   • [Python Programming Basics to Advance...  

Lesson on Comprehension Techniques:
   • [Python Programming Basics to Advance...  

Lesson of CSV file read/write that creates an Iterator
   • [Python Programming Basics to Advance...  

Python documentation on Generator and List Comprehension:
https://docs.python.org/3/howto/funct...

Complete Playlist:    • Python Programming Basics to Advanced  

If you have the basic programming knowledge and interested to learn Object-Oriented Programming in Python, check out this playlist:    • Object-Oriented Programming in Python  

Lab Manual 31 can be downloaded from here:
https://github.com/MAN1986/Python-Pro...

Review Question:
1- I want to find and display first 100 Fibonacci Numbers. Create a Generator Function to generate those Fibonacci Numbers and use that in Main Program to display those. The details of Fibonacci Numbers was discussed in this lesson:
   • Python Programming Basics to Advanced...  

2- Create a Function named as interleave() that will take two iterables as input (for simplicity you can assume that both will have same number of elements). The function should give a generator object with each of items in both iterables interleaved (first item from first iterable, then first item from second iterable, then second item from first iterable, then second item from second iterable and so on). Use to versions of the function:
a. Using yield inside the function.
b. Using return and returning a Generator Expression
To verify the working of the function we can use it on two iterables and convert the obtained generator into a list to see if the sequence is generated correctly. It was explained earlier that generator objects are not meant to be converted to other iterable as we lose the actual advantage of creating a generator. But for the sake of verification, we have to do this here. Once verified, one can use it on two iterables of many thousands of elements without converting to list and saving a huge amount of memory.
Main Program ###
x=(5,3,"Hello")
y=[2,9,20]
g=interleave(x,y)
print(g) #To verify that g is a generator
print(list(g)) #To verify if values interleaved correctly
#Must display = [5, 2, 3, 9, 'Hello', 20]



#PythonProgramming #python #pythontutorial


In questa pagina del sito puoi guardare il video online [Python Programming Basics to Advanced]: Lab 31: Generators in Python della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Learning Orbis 17 maggio 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 630 volte e gli è piaciuto 14 spettatori. Buona visione!