शैडोइंग अभ्यास: #62 Python Tutorial for Beginners | Generators - वीडियो के साथ अंग्रेजी बोलना सीखें
पाठ बनाया जा रहा है...
1
Welcome back aliens, my name is Davin Reddy and in this video we'll talk about generators.
2
Now in the earlier video we have talked about iterators.
3
So normally we use iterators to print or to fetch one value at a time.
4
So let's say if you have a list of values, let's say 100 values, And if you want one by one value, what makes sense is to use iterator.
5
But there are small issues with iterator right?
6
The first one you need to get the iterator where you have to define those two functions.
7
Next and iter.
8
We don't want to do that.
9
We want Python to do it for us.
10
And that's where Python says, okay, if you want iterators, we will give you generators.
11
Okay, but now you will think we have two different concepts.
12
Not exactly.
13
Generators will give you iterators.
14
Our job is done, right?
15
So generator give you iterator.
16
But how exactly to create a generator?
17
So let's do that.
18
So what I will do is I will just jump to Another file which is gen .py, you can create your own file there.
19
And here let me define a function.
20
That's right, we have to define a function, not a class this time.
21
So let's say our function define and we'll do the same thing, we'll return the top 10 values.
22
So we'll say top 10 and this will return the top 10 value and I will call this as well.
23
So I will let me just say I have values and this will be top 10.
24
So top 10 is.
25
a function which will give you the iterator.
26
But hold on, a function cannot give you the iterator, right?
27
A function has to be something special.
28
So we have to convert this function into a generator.
29
And the way to do that is, Normally in function we write return, right?
30
So if you say return 5, so this values will be having 5.
31
But if you instead of saying return 5, if you say
32
e -gildify your job is done because e -gildify your special keyword which will make your function as a generator.
33
but now you will think what's the difference even return will
34
give you the value even it will also give you the
35
value what i will do is i will just go back here let me just print the value
36
and if you run this code
37
so you can see we got a different thing instead of
38
getting a value 5 we got a object of generator
39
and that's the thing you know because generator gives you an iterator
40
so this function is not a normal function this is a generator because we are using yield yes if you will be different.
41
A will also return the value back it will return in the format of a iterator.
42
And everyone knows if you want to fetch something from an iterator, we have to use the next Let's run this code and you can see we got 5.
43
But now you will think, okay, that's fine.
44
Even return give the same output, right?
45
No, there's a difference.
46
In yield, since it's a generator which will give you iterator, you can write multiple yield statements.
47
So let's say yield 1.
48
Let's copy this.
49
Let's do it for four times and then they will be having different values here 2, 3 and 4.
50
Now you can see we got 4 yield which is 1, 2, 3, 4 and if you run this code, you will only get the first value because you are saying next only once.
51
So this is same as iterator where you are getting multiple values, right?
52
So you got your own iterator without using next and iterator function.
53
Let me just do that one more time.
54
I will say next.
55
Yes, you can also say next and in bracket you can say valves.
56
That's a choice you have.
57
Otherwise, this will work.
58
You can see we got one, two.
59
In fact, we can also use a loop.
60
So let's use a loop.
61
I will say for I in valves.
62
I'll let you print I here.
63
The moment you do that, you can see it will print all the values for you.
64
The remaining values which is three and four.
65
This is how you can create the rate up just by using that one particular keyword which is yield.
66
Okay, now there is another example we can take here.
67
Example, let's say I want to print top 10 perfect square.
68
You can do that.
69
So let's create one.
70
So what I will do is I will just remove this part.
71
Okay, because the logic will change.
72
Everything else will remain same.
73
Let me just not do this.
74
Let's use a for loop to print all the values.
75
But what should be logic?
76
So what I will do is I will say I will take a variable
77
which is i initially or maybe n will do n is equal to 1
78
and now since we want to print all the perfect square we have to use a loop.
79
I will not be using a for loop here because indirectly for uses iterator.
80
Let's use while so I will say while n less than equal to 10
81
because we want to fetch top 10
82
and every time you find a new value you have to just have to say yield.
83
You can say sq is equal to n into n that's how you find a square.
84
Once you got this square you can simply say yield and you can say sq.
85
Okay, so we are sending the value right.
86
So it is almost same as return but then return will terminate the function this will not.
87
That's something you have to remember.
88
Now, once you say sq you have to say n
89
because you have to also iterate your n right you have
90
to say n plus is equal to one i guess our job is done this should work let's try
91
so i will just run this application
92
and you can see we got top 10 perfect square we got 1 4 9 16 25
93
so that's how you can do it
94
because this yield will give you the next next value
95
because see when you say you're running a loop you're fetching one by one value
96
so when you fetch the first value this yield will return one when the loop says hey i value, it will give you nine.
97
So that's how you create a iterator using a generator.
98
But the question is why someone will use a generator?
99
Think about this.
100
Let's say you are fetching 1000 records from a database.
101
So when you say you're fetching 1000 records, maybe you want to print all, or maybe you want to process something from those records.
102
So when you say you want to fetch 1000 records, all these 1000 records will be loaded in your memory.
103
We don't want that.
104
Maybe you want to work with one value at a time.
105
So let's say 1000 records, we want to work with one value at a time.
106
In that case, you can use generator instead of using, instead of fetching the entire list, you can fetch one by one value.
107
So that's something you can do with generator.
108
That's maybe you can call as a real -life example.
109
So this is how we use generator with the help of yield keyword.
110
So I hope you enjoyed this video.
111
Let me know in the comment section and do subscribe for other videos.
112
Bye.
113
you
📺 उसी चैनल से
✨ अनुशंसित वीडियो
इस पाठ के बारे में
आप "#62 Python Tutorial for Beginners | Generators" के साथ Shadowing तकनीक का उपयोग करके अपनी अंग्रेजी का अभ्यास कर रहे हैं।
शैडोइंग तकनीक क्या है?
शैडोइंग (Shadowing) एक विज्ञान-समर्थित भाषा सीखने की तकनीक है जो मूल रूप से पेशेवर दुभाषिया प्रशिक्षण के लिए विकसित की गई थी। विधि सरल लेकिन शक्तिशाली है: आप मूल अंग्रेज़ी ऑडियो सुनते हैं और तुरंत इसे ज़ोर से दोहराते हैं — जैसे वक्ता की छाया 1-2 सेकंड की देरी से। शोध से पता चलता है कि यह उच्चारण सटीकता, स्वर, लय, जुड़ी हुई ध्वनियाँ, सुनने की समझ और बोलने की प्रवाहशीलता में काफ़ी सुधार करता है।














