How To Use yield in Python

preview_player
Показать описание
This video shows the basics of using the yield keyword to create generator functions in Python. Code used in this video is available in the description below.

Рекомендации по теме
Комментарии
Автор

# Use yield in a function in place of return
# to make the function behave as a generator

def normal_fibonacci(n):
fibs = [0, 1]

for i in range(2, n+1):


return fibs

def generator_fibonacci(n):
x_2 = 0
x_1 = 1

yield x_2
yield x_1

for x in range(2, n+1):
yield x_2 + x_1
x_2, x_1 = x_1, x_2 + x_1

normal_fibs = normal_fibonacci(10)

gen_fibonacci = generator_fibonacci(10)

print(normal_fibs)
print(gen_fibonacci)

# Use next() or a for loop to generate from a generator function

next(gen_fibonacci) # Generate next item

for item_left in gen_fibonacci: # Loop over all remaining items
print(item_left)

def infinite_fibonacci():
x_2 = 0
x_1 = 1

yield x_2
yield x_1

while True:
yield x_2 + x_1
x_2, x_1 = x_1, x_2 + x_1

infinite_fib = infinite_fibonacci()

next(infinite_fib)

# Generate more values from the sequence as needed!
# Get next 10 values

for x in range(10):
print( next(infinite_fib) )

DataDaft
Автор

The quality of the content of this channel is simply amazing..

francesco.
Автор

I was struggling to understand why or when I would use yield but not anymore! Thanks bro.

JTNewby
Автор

thank you so much brother. i was struggling to understand yield functionality. you taught me in lucid manner. thank you so much brother.

vinutabellur
Автор

This is really helpful.... In competitive exmas where time and size is restricted...
Thanks a lot brother...

ans
Автор

That was a very clean explanation, thanks

talisb
Автор

Interesting video! Thanks for sharing it :)

deepaktheproudindian
Автор

Awesome videos my man! Btw are you planning to make a machine learning playlist soon? :) It would be awesome if you could go through some Kaggle problems or competitions with different ML approaches! And just remember to show us how to predict something, instead of just getting a score. So many people focus on just that. Thanks!

simenandreasknudsen
Автор

Hi ! Amazing tutorials for python ! I am currently in my first year in engineering and our coding class is not very well explained. Your videos helped me a ton and I want to thank you for that. I looked through your videos and I did not find a video on python classes. I was wondering if you would one day release a video about classes in python ? Cheers !

Alex-lbev
Автор

Half way through I was thinking “so can you make it go as long as you want with a while True?”
Then I felt smart. My degree in computer science if finally justified.

jackdog
Автор

OK THANK-YOU .next time how can create a data analytics and prediction maize machine learning algorithms?

asanaaga
Автор

May I ask what type of python notebook you are using? Seems it is not the usually jupyter notebook.

julianzhao
Автор

Your code has a flaw, if you call for fib(0), it will print both 0, 1

sasidharnaidu
visit shbcf.ru