跟讀練習: #62 Python Tutorial for Beginners | Generators - 通過影片學習英語口語

選擇練習模式

每種模式都會開啟全屏練習室。

113 句
如果句子過短或過長,請點選 Edit 進行調整。
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

下載應用

流利口語所需的一切

AI 發音評分逐句打分
音標練習掌握每個音
詞彙學習建立你的詞庫
詞彙遊戲邊玩邊學

同頻道

FOR YOU

推薦影片

本課的詞彙與口語要點

這節 B1 級別的口語課以影片“Python Tutorial for Beginners”為素材。 影片中反覆出現的詞有:value, iterator, generator, function, return。 這段影片共有 113 個句子、1240 個單詞可供跟讀。 講話部分時長為 6:02。 說話人語速較快,每分鐘約 206 個詞,會出現較多連讀和弱讀。 86% 的單詞屬於英語最常用的 3,000 詞,其餘的詞建議在練習前先學一下。

影片中的重點詞彙

影片中最難的 11 個單詞,附發音和釋義:

單詞發音釋義
generator 名詞/ˈdʒɛnəɹeɪtə(ɹ)/發電機 /發電機
yield 動詞/jiːld/產量 /產量, 產生 /產生
fetch 動詞/fɛt͡ʃ/取
loop 名詞/luːp/圈, 環 /環
define 動詞/dɪˈfaɪn/界定
keyword 名詞/ˈkiː.wɜːd/關鍵詞 /關鍵詞, 關鍵字 /關鍵字
valve 名詞/ˈvælv/閥門 /閥門
terminate 動詞/ˈtɝmɪneɪ̯t/結束 /結束, 終止 /終止
bracket 名詞/ˈbɹækɪt/支架, 托架
subscribe 動詞/səbˈskɹaɪb/簽署 /簽署
database 名詞/ˈdeɪtəˌbeɪs/資料庫 /資料庫

影片中出現的短語動詞

單詞釋義
go back 動詞回去, 返回

值得反覆跟讀的句子

影片中簡短而完整的句子,可以直接用在日常對話裡:

  • We don't want to do that.
  • That's a choice you have.
  • I'll let you print I here.
  • That's something you have to remember.

影片中的語法

說話人最常用的結構,並附上影片中的原話:

結構影片中的用法
條件句 if + 從句,will/would + 動詞 — 條件及其結果if you want iterators, we will give · if you run this code, you will only
定語從句 who / which + 從句 — 補充說明人或事物file which is · function which will · keyword which will

需要注意的發音

說話人用了 10 次縮略和弱讀形式,例如 we'll, you're, don't。請按聽到的簡短形式來說。

  • 長單詞——注意重音位置: generator /ˈdʒɛnəɹeɪtə(ɹ)/, indirectly /ˌɪn.dɪˈɹɛk(t).li/

中文母語者容易讀錯的音:

  • /v/ — 不要讀成 /w/,上齒輕觸下唇: valve /ˈvælv/, convert /kənˈvɝt/, variable [ˈvæɹ.i.ə.bl̩]
  • 詞尾子音 — 要讀清楚,後面不要加母音: yield /jiːld/, fetch /fɛt͡ʃ/, loop /luːp/, keyword /ˈkiː.wɜːd/, valve /ˈvælv/
  • /r/ 和 /l/ 的區別: indirectly /ˌɪn.dɪˈɹɛk(t).li/, variable [ˈvæɹ.i.ə.bl̩]

如何用這段影片練習

  1. 先完整聽一遍影片,不要開口,記下不認識的單詞。
  2. 先用 0.75 倍速逐句跟讀,熟練之後再回到正常速度。
  3. 錄下自己的聲音並與原聲對比,特別注意 generator, yield, fetch 這類單詞。

什麼是跟讀法?

跟讀法 (Shadowing) 是一種有科學依據的語言學習技巧,最初開發用於專業口譯員的培訓,並由多語言者Alexander Arguelles博士普及。這個方法簡單而強大:您在聽英語母語原聲的同時立即大聲重複——就像是一個延遲1-2秒緊跟說話者的影子。與被動聽力或語法練習不同,跟讀法強迫您的大腦和口腔肌肉同時處理並模模擬實的講話模式。研究表明它能顯著提高發音準確性,語調,節奏,連讀,聽力理解和口語流利度——使其成為雅思口語備考和真實英語交流最有效的方法之一。

影子跟讀法: 閱讀完整分步指南 →