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

Показать описание
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:
Lesson on Comprehension Techniques:
Lesson of CSV file read/write that creates an Iterator
Python documentation on Generator and List Comprehension:
Lab Manual 31 can be downloaded from here:
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:
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 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:
Lesson on Comprehension Techniques:
Lesson of CSV file read/write that creates an Iterator
Python documentation on Generator and List Comprehension:
Lab Manual 31 can be downloaded from here:
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:
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
Комментарии