# 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): fibs.append(fibs[-1]+fibs[-2]) 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) )
@francesco.45332 жыл бұрын
The quality of the content of this channel is simply amazing..
@vinutabellur Жыл бұрын
thank you so much brother. i was struggling to understand yield functionality. you taught me in lucid manner. thank you so much brother.
@user-fh2gt2po8r7 ай бұрын
Yes! same!
@JTNewby2 жыл бұрын
I was struggling to understand why or when I would use yield but not anymore! Thanks bro.
@ans74523 жыл бұрын
This is really helpful.... In competitive exmas where time and size is restricted... Thanks a lot brother...
@simenandreasknudsen92723 жыл бұрын
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!
@Alex-lb7ev2 жыл бұрын
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 !
@wicaksonoleksono732710 ай бұрын
Awesome analogy !
@talisb7883 Жыл бұрын
That was a very clean explanation, thanks
@ryanmanchikanti52653 жыл бұрын
Ayyy , thank you so much man.
@DataDaft3 жыл бұрын
You may notice I recently made some a video on functions and generator expressions since yield is not something you can really understand without understanding those topics first; you may wish to check those out too.
@vishakp892 жыл бұрын
Great Explanation!
@deepaktheproudindian Жыл бұрын
Interesting video! Thanks for sharing it :)
@jackdog06 Жыл бұрын
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.
@canadianmods6162 Жыл бұрын
Thank you so much,
@onderdogmus6326 Жыл бұрын
Thanks for video
@julianzhao76772 жыл бұрын
May I ask what type of python notebook you are using? Seems it is not the usually jupyter notebook.
@vishnuhaasan2714 Жыл бұрын
its a kaggle kernel.
@wicaksonoleksono732710 ай бұрын
Great vid !
@florenciaortega65432 жыл бұрын
Thank you!!!
@asanaaga47102 жыл бұрын
OK THANK-YOU .next time how can create a data analytics and prediction maize machine learning algorithms?
@sasidharnaidu45077 күн бұрын
Your code has a flaw, if you call for fib(0), it will print both 0, 1
@sasidharnaidu45077 күн бұрын
You should have some sort of check for n equals zero or any negative number