Skip to content

Commit 0467808

Browse files
authored
4.1-4.3
1 parent 181652f commit 0467808

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

4.-more-control-flow-tools.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff 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.
22

33

44

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.
66

7-
### 4.1. [`if`](https://docs.python.org/3/reference/compound_stmts.html#if) Statements
7+
### 4.1. Câu lệnh [`if`](https://docs.python.org/3/reference/compound_stmts.html#if)
88

9-
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ụ:>>>
1010

1111
```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
1414
>>> if x < 0:
1515
... x = 0
16-
... print('Negative changed to zero')
16+
...     print('So am duoc doi thanh 0')
1717
... elif x == 0:
18-
... print('Zero')
18+
...     print('So khong')
1919
... elif x == 1:
20-
... print('Single')
20+
...     print('So mot')
2121
... else:
22-
... print('More')
22+
...     print('So khac')
2323
...
24-
More
24+
So khac
2525
```
2626

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.
2828

29-
### 4.2. [`for`](https://docs.python.org/3/reference/compound_stmts.html#for) Statements
29+
### 4.2. Câu lệnh [`for`](https://docs.python.org/3/reference/compound_stmts.html#for)
3030

31-
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\):&gt;&gt;&gt;
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\):&gt;&gt;&gt;
3232

3333
```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))
3838
...
39-
cat 3
40-
window 6
41-
defenestrate 12
39+
meo 3
40+
chuot 5
41+
cho soi 7
4242
```
4343

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:&gt;&gt;&gt;
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:&gt;&gt;&gt;
4545

4646
```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)
5050
...
51-
>>> words
52-
['defenestrate', 'cat', 'window', 'defenestrate']
51+
>>> tu
52+
['cho soi', 'meo', 'chuot', 'cho soi']
5353
```
5454

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`.
5656

57-
### 4.3. The [`range()`](https://docs.python.org/3/library/stdtypes.html#range) Function
57+
### 4.3. Hàm [`range()`](https://docs.python.org/3/library/stdtypes.html#range)
5858

59-
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:&gt;&gt;&gt;
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ố:&gt;&gt;&gt;
6060

6161
```text
6262
>>> 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
6969
4
7070
```
7171

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\):
7373

7474
```text
7575
range(5, 10)
@@ -82,7 +82,7 @@ range(-10, -100, -30)
8282
-10, -40, -70
8383
```
8484

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:&gt;&gt;&gt;
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) [`len()`](https://docs.python.org/3/library/functions.html#len) như sau:&gt;&gt;&gt;
8686

8787
```text
8888
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
@@ -96,25 +96,25 @@ To iterate over the indices of a sequence, you can combine [`range()`](https://d
9696
4 lamb
9797
```
9898

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).
100100

101-
A strange thing happens if you just print a range:&gt;&gt;&gt;
101+
Có một điều lạ khi bạn in một range (khoảng) ra:&gt;&gt;&gt;
102102

103103
```text
104104
>>> print(range(10))
105105
range(0, 10)
106106
```
107107

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ớ.
109109

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:&gt;&gt;&gt;
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:&gt;&gt;&gt;
111111

112112
```text
113113
>>> list(range(5))
114114
[0, 1, 2, 3, 4]
115115
```
116116

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ố.
118118

119119
### 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
120120

0 commit comments

Comments
 (0)