You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 4.-more-control-flow-tools.md
+37-37Lines changed: 37 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,62 +1,62 @@
1
-
# 4. More Control Flow Tools
1
+
# 4. Các công cụ điều khiển luồng khác.
2
2
3
3
4
4
5
-
Besides the [`while`](https://docs.python.org/3/reference/compound_stmts.html#while)statement just introduced, Python knows the usual control flow statements known from other languages, with some twists.
5
+
Bên cạnh câu lệnh [`while`](https://docs.python.org/3/reference/compound_stmts.html#while)đã được giới thiệu, Python cũng có các câu lệnh điều khiển luồng thông thường giống như các ngôn ngữ khác.
Perhaps the most well-known statement type is the [`if`](https://docs.python.org/3/reference/compound_stmts.html#if) statement. For example:>>>
9
+
Câu lệnh phổ biến nhất là [`if`](https://docs.python.org/3/reference/compound_stmts.html#if) statement. Ví dụ:>>>
10
10
11
11
```text
12
-
>>> x = int(input("Please enter an integer: "))
13
-
Please enter an integer: 42
12
+
>>> x = int(input("Hay nhap vao mot so nguyen: "))
13
+
Hay nhap vao mot so nguyen: 42
14
14
>>> if x < 0:
15
15
... x = 0
16
-
... print('Negative changed to zero')
16
+
... print('So am duoc doi thanh 0')
17
17
... elif x == 0:
18
-
... print('Zero')
18
+
... print('So khong')
19
19
... elif x == 1:
20
-
... print('Single')
20
+
... print('So mot')
21
21
... else:
22
-
... print('More')
22
+
... print('So khac')
23
23
...
24
-
More
24
+
So khac
25
25
```
26
26
27
-
There can be zero or more [`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif)parts, and the [`else`](https://docs.python.org/3/reference/compound_stmts.html#else)part is optional. The keyword ‘[`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif)’ is short for ‘else if’, and is useful to avoid excessive indentation. An [`if`](https://docs.python.org/3/reference/compound_stmts.html#if) … [`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif) … [`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif) … sequence is a substitute for the `switch`or`case`statements found in other languages.
27
+
Có thể không có hoặc có rất nhiều lệnh [`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif)còn lệnh [`else`](https://docs.python.org/3/reference/compound_stmts.html#else)chỉ có thể có duy nhất hoặc không. Từ khoá ‘[`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif)’ là viết tắt của ‘else if’, nó giúp tránh việc phải thụt lề quá nhiều. Một chuỗi [`if`](https://docs.python.org/3/reference/compound_stmts.html#if) … [`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif) … [`elif`](https://docs.python.org/3/reference/compound_stmts.html#elif) … dùng để thay thế cho câu lệnh `switch`hoặc `case`trong các ngôn ngữ khác.
The [`for`](https://docs.python.org/3/reference/compound_stmts.html#for)statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers\(like in Pascal\), or giving the user the ability to define both the iteration step and halting condition \(as C\), Python’s [`for`](https://docs.python.org/3/reference/compound_stmts.html#for)statement iterates over the items of any sequence \(a list or a string\), in the order that they appear in the sequence. For example\(no pun intended\):>>>
31
+
Câu lệnh [`for`](https://docs.python.org/3/reference/compound_stmts.html#for)trong Python khác một chút so với C và Pascal. Thay vì việc duyệt qua một chuỗi số tăng dần\(như trong Pascal\) hoặc người dùng sẽ định nghĩa về các bước duyệt và điều kiện dừng \(như C\), câu lệnh [`for`](https://docs.python.org/3/reference/compound_stmts.html#for)của Python duyệt qua các phần tử của chuỗi \(một danh sách hoặc xâu\) theo thứ tự mà nó xuất hiện trong chuỗi. Ví dụ\(no pun intened\):>>>
32
32
33
33
```text
34
-
>>> # Measure some strings:
35
-
... words = ['cat', 'window', 'defenestrate']
36
-
>>> for w in words:
37
-
... print(w, len(w))
34
+
>>> # Tinh do dai cac xau:
35
+
... tu = ['meo', 'chuot', 'cho soi']
36
+
>>> for t in tu:
37
+
... print(t, len(t))
38
38
...
39
-
cat 3
40
-
window 6
41
-
defenestrate 12
39
+
meo 3
40
+
chuot 5
41
+
cho soi 7
42
42
```
43
43
44
-
If you need to modify the sequence you are iterating over while inside the loop \(for example to duplicate selected items\), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:>>>
44
+
Nếu cần sửa chính chuỗi mà bạn đang duyệt qua khi đang ở trong vòng lặp \(ví dụ nhân đôi các phần tử\), đầu tiên bạn nên sao chép lại chuỗi đó. Việc duyệt qua chuỗi không ngầm sao chép chuỗi. Kí hiệu cắt ở dưới làm cho điều nãy dễ dàng hơn:>>>
45
45
46
46
```text
47
-
>>> for w in words[:]: # Loop over a slice copy of the entire list.
48
-
... if len(w) > 6:
49
-
... words.insert(0, w)
47
+
>>> for t in tu[:]: # Duyệt qua bản sao của toàn bộ danh sách
48
+
... if len(t) > 6:
49
+
... tu.insert(0, t)
50
50
...
51
-
>>> words
52
-
['defenestrate', 'cat', 'window', 'defenestrate']
51
+
>>> tu
52
+
['cho soi', 'meo', 'chuot', 'cho soi']
53
53
```
54
54
55
-
With`for w in words:`, the example would attempt to create an infinite list, inserting `defenestrate` over and over again.
55
+
Với`for t in tu:`, ví dụ trên sẽ tạo ra một chuỗi vô hạn, lặp đi lặp lại việc chèn phần tử `cho soi`.
56
56
57
-
### 4.3. The[`range()`](https://docs.python.org/3/library/stdtypes.html#range) Function
If you do need to iterate over a sequence of numbers, the built-in function [`range()`](https://docs.python.org/3/library/stdtypes.html#range)comes in handy. It generates arithmetic progressions:>>>
59
+
Nếu bạn muốn duyệt qua một chuỗi các số, hàm [`range()`](https://docs.python.org/3/library/stdtypes.html#range)được xây dựng sẵn sẽ rát tiện dụng. Nó sẽ sinh ra một chuỗi các số:>>>
60
60
61
61
```text
62
62
>>> for i in range(5):
@@ -69,7 +69,7 @@ If you do need to iterate over a sequence of numbers, the built-in function [`ra
69
69
4
70
70
```
71
71
72
-
The given end point is never part of the generated sequence; `range(10)`generates 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment \(even negative; sometimes this is called the ‘step’\):
72
+
Điểm kết thúc sẽ không nằm trong chuỗi; `range(10)`tạo ra 10 giá trị và các chỉ số phù hợp cho các phần tử của chuỗi có độ dài 10. Có thể làm cho khoảng này bắt đầu từ một số khác hoặc các phần tử tăng lên với khoảng cách khác \(với cả khoảng cách âm; thỉnh thoảng việc này được gọi là ‘bước’\):
73
73
74
74
```text
75
75
range(5, 10)
@@ -82,7 +82,7 @@ range(-10, -100, -30)
82
82
-10, -40, -70
83
83
```
84
84
85
-
To iterate over the indices of a sequence, you can combine [`range()`](https://docs.python.org/3/library/stdtypes.html#range)and[`len()`](https://docs.python.org/3/library/functions.html#len)as follows:>>>
85
+
Để duyệt qua các chỉ số của chuỗi, bạn có thể kết hợp [`range()`](https://docs.python.org/3/library/stdtypes.html#range)và[`len()`](https://docs.python.org/3/library/functions.html#len)như sau:>>>
86
86
87
87
```text
88
88
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
@@ -96,25 +96,25 @@ To iterate over the indices of a sequence, you can combine [`range()`](https://d
96
96
4 lamb
97
97
```
98
98
99
-
In most such cases, however, it is convenient to use the [`enumerate()`](https://docs.python.org/3/library/functions.html#enumerate) function, see[Looping Techniques](https://docs.python.org/3/tutorial/datastructures.html#tut-loopidioms).
99
+
Trong phần lớn trường hợp là như vậy, tuy nhiên, sẽ thuận lợi hơn nếu dùng hàm [`enumerate()`](https://docs.python.org/3/library/functions.html#enumerate), xem[Looping Techniques](https://docs.python.org/3/tutorial/datastructures.html#tut-loopidioms).
100
100
101
-
A strange thing happens if you just print a range:>>>
101
+
Có một điều lạ khi bạn in một range (khoảng) ra:>>>
102
102
103
103
```text
104
104
>>> print(range(10))
105
105
range(0, 10)
106
106
```
107
107
108
-
In many ways the object returned by [`range()`](https://docs.python.org/3/library/stdtypes.html#range)behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.
108
+
Object trả về bởi [`range()`](https://docs.python.org/3/library/stdtypes.html#range)được coi như là danh sách, nhưng thực ra không phải. Đây là một object trả về các phần tử liên tiếp của chuỗi mong muốn khi bạn duyệt qua nó nhưng nó không thực sự là một danh sách, điều này giúp giảm bộ nhớ.
109
109
110
-
We say such an object is _iterable_, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the[`for`](https://docs.python.org/3/reference/compound_stmts.html#for)statement is such an _iterator_. The function [`list()`](https://docs.python.org/3/library/stdtypes.html#list)is another; it creates lists from iterables:>>>
110
+
Ta gọi một object như thế là _iterable_, nghĩa là nó phù hợp cho hàm hoặc cấu trúc cần cung cấp các phần tử một cách liên tiếp cho đến khi hết. Chúng ta đã thấy lệnh[`for`](https://docs.python.org/3/reference/compound_stmts.html#for)là một _iterator_ như thế. Hàm [`list()`](https://docs.python.org/3/library/stdtypes.html#list)là một ví dụ khác; nó tạo danh sách từ iterables:>>>
111
111
112
112
```text
113
113
>>> list(range(5))
114
114
[0, 1, 2, 3, 4]
115
115
```
116
116
117
-
Later we will see more functions that return iterables and take iterables as argument.
117
+
Sau này chúng ta sẽ thấy nhiều hàm trả về iterables và nhận iterables với tư cách là tham số.
118
118
119
119
### 4.4. [`break`](https://docs.python.org/3/reference/simple_stmts.html#break) and [`continue`](https://docs.python.org/3/reference/simple_stmts.html#continue) Statements, and [`else`](https://docs.python.org/3/reference/compound_stmts.html#else) Clauses on Loops
0 commit comments