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

preview_player
Показать описание
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
Рекомендации по теме
Комментарии
Автор

Thanks for this wonderful series!

This Python series is the Best and the most practical among so many I found on youtube. Instead of just discussing the basic and surface level concepts, as done in most of the top rated Python series on youtube, you discus everything in depth and that too with excellent explanation. Even the difficult concepts like these Generators, are made so easy and compelling to understand.
This series deserves to reach among at least top 5 Python series on youtube. Sooner or later, you will achieve this milestone.

Prayers for you!

valdemarcarl
Автор

Q1:
def fibonacci(n):
a, b, c=0, 1, 0
while c<n:
yield a
s=a+b
a, b=b, s
c+=1

# Main Program
a=fibonacci(100)
for i in a:
print(i)




Q2:
# Generator Function
def interleave(a, b):
for i in range(len(a)):
yield a[i]
yield b[i]
# Non-Generator Function
def interleave(a, b):
z=zip(a, b)
return (j for i in z for j in i)
# Main Program

x=(5, 3, "Hello")
y=[2, 9, 20]
g=interleave(x, y)
print(g)
print(list(g))

waleedraza
Автор

Q1:
def fibona(e=1, f=1, /, *, ranges):

for _ in range(ranges):
e, f = f, e+f
yield f

e = fibona(ranges=10)
for i in e:
print(i)




Q2:
def interleave(e, f):
for i in range(len(e)):
yield e[i]
yield f[i]
# Non-Generator
def interleave(e, f):
z=zip(e, f)
return (j for i in z for j in i)
# Main Program
x=(5, 3, "Hello")
y=[2, 9, 20]
g=interleave(x, y)
print(g)
print(list(g))

areebaazhar
Автор

1:
def fibona(a=1, b=1, /, *, ranges):

for _ in range(ranges):
a, b = b, a+b
yield b

a = fibona(ranges=10)
for i in a:
print(i)

2:
def interleave(x, y):
a=((x[i], y[i]) for i in range(len(x)))
return (j for i in a for j in i)
# Main Program
a=(5, 3, "Hello")
b=[2, 9, 20]
g=interleave(a, b)
print(g)
print(list(g))

AsadAli-owie
Автор

Task)
def funtion(i=1, j=1, /, *, ranges):
for _ in range(ranges):
i, j = j, i + j
yield j


x=function(ranges=10)
for i in x:
print(i)

hishamawan
Автор

question 1
def fibona(a=1, b=1, /, *, ranges):

for _ in range(ranges):
a, b = b, a+b
yield b

a = fibona(ranges=10)
for i in a:
print(i)

question2
def interleav(*a:tuple)->iter:
for i in a:
yield 'From First iterable {}.From Second iterable {}'.format(*i)

x=(5, 3, "Hello")
y=[2, 9, 20]
a = interleav(*zip(x, y))
for i in a:
print(i)
U=(5, 3, "Hello")
O=[2, 9, 20]
a = ('From First iterable {}.From Second iterable {}'.format(*i) for i in zip(U, O))
for l in a:
print(l)

mukarmarashid
Автор

1:
def fibona(a=1, b=1, /, *, ranges):
for _ in range(ranges):
a, b = b, a+b
yield b
a = fibona(ranges=10)
for i in a:
print(i)
2:
def interleav(*a:tuple)->iter:
for i in a:
yield 'From First iterable {}.From Second iterable {}'.format(*i)
x=(5, 3, "Hello")
y=[2, 9, 20]
a = interleav(*zip(x, y))
for i in a:
print(i)
U=(5, 3, "Hello")
O=[2, 9, 20]
a = ('From First iterable {}.From Second iterable {}'.format(*i) for i in zip(U, O))
for l in a:
print(l)

AliIrfan-soxf
Автор

Q1):
def fibo(n):
x=1
y=1
for i in range(n):
yield x
z=x+y
x=y
y=z
a=fibo(100)
for i in a:
print(i)
Q2):


def interleave(x, y):
for i in range(len(x)):
yield x[i]
yield y[i]
def interleave2(x, y):
z=zip(x, y)
return (j for i in z for j in i)
x=(5, 3, "Hello")
y=[2, 9, 20]
g=interleave(x, y)
g2=interleave2(x, y)
print(g, g2)
print(list(g), list(g2))

yusrakashif
Автор

#Review Q1:
def func(i=1, j=1, /, *, ranges):
for _ in range(ranges):
i, j = j, i + j
yield j


g = func(ranges=10)
for i in g:
print(i)

ahmedimran
Автор

#Question No.1:

def fibonacci(x):
a, b=1, 0
for i in range(x):
a, b=b, a+b
yield b
y= fibonacci(100)
for i in y:
print(i)



#Question No.2:

def interleave(x, y):
for i in range(len(x)):
yield (x[i])
yield (y[i])

def interleave_1(a, b):
return (j for i in zip(a, b) for j in i)

### Main Program ###
a=(5, 3, "Hello")
b=[2, 9, 20]
g=interleave(a, b)
f=interleave_1(a, b)
print(g) #To verify that g is a generator
print(f) #To verify that f is a generator
print(list(g)) #To verify if values interleaved correctly
print(list(f)) #To verify if values interleaved correctly
#Must display = [5, 2, 3, 9, 'Hello', 20]

rajahaseebahmad
Автор

TASK#1

#Fibonacci Generator Function#
def Fibo(n):
a=b=1
for i in range(n):
if(i==0 or i==1):
yield 1
else:
a, b=b, a+b
yield b



#MAIN PROGRAM#
n=100
a=Fibo(n)
for i in a:
print(i)









TASK#2



part#1



#Interleave generator function using yield#
def interleave(a, b):
for i in range(len(a)):
yield a[i]
yield b[i]



#MAIN PROGRAM#
a, b=(5, 3, 'Hello'), [2, 9, 20]
g=interleave(a, b)
print(g)
print(list(g))





part#2



#Simple Interleave function returning generator using generator expression#
def interleave(a, b):
x=((a[i], b[i]) for i in range(len(a)))
return (j for i in x for j in i)



#MAIN PROGRAM#
a, b=(5, 3, 'Hello'), [2, 9, 20]
g=interleave(a, b)
print(g)
print(list(g))

amaanmajid
Автор

Question 01
def fibona(a=1, b=1, /, *, ranges):

for _ in range(ranges):
a, b = b, a+b
yield b

a = fibona(ranges=10)
for i in a:
print(i)

Question 02
def interleav(*a:tuple)->iter:
for i in a:
yield 'From First iterable {}.From Second iterable {}'.format(*i)

x=(5, 3, "Hello")
y=[2, 9, 20]
a = interleav(*zip(x, y))
for i in a:
print(i)
U=(5, 3, "Hello")
O=[2, 9, 20]
a = ('From First iterable {}.From Second iterable {}'.format(*i) for i in zip(U, O))
for l in a:
print(l)

usmantariq
Автор

Question No. 1:-

def fibonacci(n, /, a=0, b=1):
for i in range(n-1):
a, b=b, a+b
yield b

#Main Program
a=fibonacci(100)
print(1)
for b in a:
print(b)

Question No.2 :-


def interleave(iterable1, iterable2):
for i in range(len(iterable1)):
yield iterable1[i]
yield iterable2[i]

def interleave(iterable1, iterable2):
return (j for i in zip(iterable1, iterable2) for j in i)

### 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]

MuhammadAhmad-dshu
Автор

#Q:1:-
def fibonacci(n):
a, b=1, 0
for _ in range(n):
a, b=b, a+b
yield b

# Main Program #
f=fibonacci(100)
for i in f:
print(i)

#Q:2:-
def interleave(x, y):
a=((x[i], y[i]) for i in range(len(x)))
return (j for i in a for j in i)

# Main Program #
a=(5, 3, 'Hello')
b=[2, 9, 20]
g=interleave(a, b)
print(g)
print(list(g))

usman_tariq
Автор

Q no 1

def fibo(n):
x, y=1, 0
for i in range(n):
x, y=y, x+y
yield y

f= fibo(100)
for i in f:
print(i)




Q no 2

def interleave_gen(a, b):
for i in range (len(b)):
yield (a[i])
yield (b[i])
def interleave_NG(a, b):
return (j for i in zip(a, b) for j in i)

x=(5, 3, "Hello")
y=[2, 9, 20]
Gen=interleave_gen(x, y)
NG=interleave_NG(x, y)
print(Gen)
print(NG)
print(list(Gen))
print(list(NG))

fourcloversan
Автор

#Review Q1:
def fibona(a=1, b=1, /, *, ranges):

for _ in range(ranges):
a, b = b, a+b
yield b

a = fibona(ranges=10)
for i in a:
print(i)


#Review Q2:
def interleave(a, b):
for i in range(len(a)):
yield a[i]
yield b[i]
# Non-Generator
def interleave(a, b):
z=zip(a, b)
return (j for i in z for j in i)
# Main Program
x=(5, 3, "Hello")
y=[2, 9, 20]
g=interleave(x, y)
print(g)
print(list(g))

asmaabhatti
Автор

2019-MC-17

Question 01
def fibona(a=1, b=1, /, *, ranges):

for _ in range(ranges):
a, b = b, a+b
yield b

a = fibona(ranges=10)
for i in a:
print(i)

Question 02
def interleav(*a:tuple)->iter:
for i in a:
yield 'From First iterable {}.From Second iterable {}'.format(*i)

x=(5, 3, "Hello")
y=[2, 9, 20]
a = interleav(*zip(x, y))
for i in a:
print(i)
U=(5, 3, "Hello")
O=[2, 9, 20]
a = ('From First iterable {}.From Second iterable {}'.format(*i) for i in zip(U, O))
for l in a:
print(l)

MuhammadHassan-ldyt
Автор

# Review_Question-01
def fibo(n):
a, b=0, 1
count=0
while count<n:
yield a
s=a+b
a, b=b, s
count+=1
# Main Program
a=fibo(100)
for i in a:
print(i, end=', ')

# Review_Question-02
# Generator Function
def interleave(a, b):
for i in range(len(a)):
yield a[i]
yield b[i]
# Non-Generator
def interleave(a, b):
z=zip(a, b)
return (j for i in z for j in i)
# Main Program
x=(5, 3, "Hello")
y=[2, 9, 20]
g=interleave(x, y)
print(g)
print(list(g))

nukhbaiqbal
Автор

#Q1
def fibonacci(n):
a, b=1, 0
for _ in range(n):
a, b=b, a+b
yield b

# Main Program #
f=fibonacci(100)
for i in f:
print(i)

#Q2
def interleave(x, y):
a=((x[i], y[i]) for i in range(len(x)))
return (j for i in a for j in i)

# Main Program #
a=(5, 3, 'Hello')
b=[2, 9, 20]
g=interleave(a, b)
print(g)
print(list(g))

muhammadalihaider
Автор

#Review_Question_1:
def fibo():
x=0
y=1
yield y
for i in range(99):
x, y=y, x+y
yield y

a=fibo()
c=1
for i in a:
print('{} : {}'.format(c, i))
c+=1

##Review_Question_2:
def interleave(a, b):
for i in range(len(a)):
yield a[i]
yield b[i]

x=(5, 3, "Hello")
y=[2, 9, 20]

x=interleave(x, y)
for i in x:
print(i)

abdul-hadi
join shbcf.ru