Expert Python Tutorial #5 - Generators

preview_player
Показать описание
In this expert python tutorial we will be discussing generators. Generators are a way to generate sequences or values in a memory efficient way. They use the yield keyword rather than return and are useful for optimizing programs and avoiding memory issues.

◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python

◾◾◾◾◾◾

⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡

⭐ Tags ⭐
- Tech With Tim
- Python Tutorials
- Python Generators
- Generators in Python
- Python Generator Tutorial
- How to Use Generators Python

⭐ Hashtags ⭐
#Python #ExpertPython
Рекомендации по теме
Комментарии
Автор

I am a masters student with major in data science. Though I am familiar with python(mostly) but your insights are amazing and the way you articulate. Really appreciate your content Tim :)
Thanks so much.

PranavArrora
Автор

Hi Tim. I'm a software tester working in automation, and having a go at intermediate level certification in Python. This is what I needed has complement to Python Institute's course. Thank you for sharing your knowledge! Greetings from Portugal.

antoniodsantos
Автор

Another way of making a generator is by using () other then [] in the list comprehension:

a = (i**2 for i in range (100))
print(a) # < generator at...>
for i in a:
print(i)

adryelbarros
Автор

Great video, as per usual. Tim is really a programming prodigy and a great teacher, making complicated topics seem simple and easy-going. Thank you!
As a bit of trivia, a useful method for keeping track of large numbers in code is to use the " _ " character for visual delimitation:
>>> nr =
>>> print(nr)
Output:

alexandrumoldovan
Автор

I enjoy your videos. Not only do you explain how, but you also explain why. You could explain to me how something was built. However, explaining why this method was chosen over a different method, is a whole different level of understanding. 10/10

Xaminn
Автор

def fibb_gen(n):
a, b=1, 1
yield 1
while a<n:
yield a
a, b=a+b, a

fibby=fibb_gen(15000)

for i in fibby: print(i)

claudiu_ivan
Автор

Holy smokes, I copied out the code you did in the first two minutes and ran it, my computer made a noise I've never heard before...

arimakishou
Автор

One tip that I don't see a lot is a method for taking over iteration of another iterable. Let's say you have an iterable out of which you would like to make a generator, such as in the example at 7:31. Instead if manually looping over the iterable and yielding each value individually, you could simply write the following:

def gen(n):
yield from range(n)

It's way more concise. Hopefully that's helpful.

paulzupan
Автор

Probably the best explanation out there.

amreshgiri
Автор

You can also create asynchronous programs using generators because you can pause a function with yield and then use the send method of generators to continue execution. Not that this is insanely useful but perhaps nice to know ;)

XRay
Автор

This is such a good series. Keep it up Tim!!

harmannatsingh
Автор

Consider using what is called generator expression much like list comprehension, the difference being you get a generator object instead of, well, a list. Just wrap the expression with parentheses instead of square brackets. gen=(x**2 for x in print(gen.next())

escalatedchi
Автор

Keep up the series man! I'm a college student as well and most of the time, you are one of the channel which has helped me to continue learning python and self explore projects on the side to build my portfolio!

augustong
Автор

At 8:28 you create a generator object 'g' that you then use in a for loop. If you are going to use a generator in a loop, you can write for i in and miss out creating g.

gedtoon
Автор

wow! now i understand why yield and next are so useful. that wass greatly explained

mampiisaotaku
Автор

Then, I guess, range() also uses yield keyword, right? I used it a lot in C# with many Reflection APIs: the function actually don't run when it is called, but only when the returned value's iterator is requested, just until the next yield keyword at which time it is paused...

learnserve
Автор

This make so much sense and thank you for your video

georgettebeulah
Автор

Amazing series, I hope there are more!

debjyotibiswas
Автор

I have the exact code with Tim, I try to get the generator size, and it prints 112, does memory consumption different in machines?

ultimatejy
Автор

def gen(n)
for i in range(n):
yield i**2

g=gen(10000)

wongkingshun