2.7. Operators and Operands - toán tử và toán hạng

Reference:
http://interactivepython.org/runestone/static/thinkcspy/SimplePythonData/OperatorsandOperands.html
(Các bạn nếu đọc tiếng việt xong thì nhớ vào trang chủ nó mà giải bài tập)


Operators are special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands.

Các toán tử là những mã đặc biệt thể hiện các phép tính như cộng, nhân và chia. Những giá trị mà toán tử làm việc (tính toán) thì gọi là toán hạng

The following are all legal Python expressions whose meaning is more or less clear:
Theo những biểu thức Python hợp pháp mà có nghĩa hoặc ít nhiều rõ ràng:

20 + 32
hour - 1
hour * 60 + minute
minute / 60
5 ** 2
(5 + 9) * (15 - 7)
The tokens +-, and *, and the use of parenthesis for grouping, mean in Python what they mean in mathematics. The asterisk (*) is the token for multiplication, and ** is the token for exponentiation. Addition, subtraction, multiplication, and exponentiation all do what you expect.
Các ký hiệu +, -, và * và sử dụng dấu ngoặc để chia, nghĩa là trong Python chúng cũng có nghĩa như trong toán học. Dấu hoa thị là ký hiệu cho phép nhân và 2 dấu hoa thị là ký hiệu cho lũy thừa. Phép thêm, phép trừ, phép nhân và lũy thừa thì giống như trong toán học
1
print(2 + 3)
2
print(2 - 3)
3
print(2 * 3)
4
print(2 ** 3)
5
print(3 ** 2)
6
(ch02_15)
When a variable name appears in the place of an operand, it is replaced with the value that it refers to before the operation is performed. For example, what if we wanted to convert 645 minutes into hours. In Python 3, division is denoted by the operator token / which always evaluates to a floating point result.
Khi mà một tên biến xuất hiện trong một toán hạng, nó sẽ được ghi đè với giá trị mà nó được đề cập trước khi nó được thực hiện. Ví dụ, nếu chúng ta muốn chuyển đổi 654 phút thành giờ. Trong Python 3, phép chia được biểu thị bằng ký hiệu toán tử / 
1
minutes = 645
2
hours = minutes / 60
3
print(hours)
4
(ch02_16)
What if, on the other hand, we had wanted to know how many whole hours there are and how many minutes remain. To help answer this question, Python gives us a second flavor of the division operator. This version, called integer division, uses the token //. It always truncates its result down to the next smallest integer (to the left on the number line).
Mặt khác, chúng ta muốn biết có bao nhiêu giờ và bao nhiêu phút thì sao. Để trả lời câu hỏi này, Python dưa cho chúng ta một cách khác của toán tử chia. Trong phiên bản này, gọi là chia số nguyên, sử dụng ký hiệu // . nó luôn rút két quả của nó xuống số nhỏ hơn (đại loại chỉ lấy số nguyên và bỏ số thập phân)

1
print(7 / 4)
2
print(7 // 4)
3
minutes = 645
4
hours = minutes // 60
5
print(hours)
6
(ch02_17)
Pay particular attention to the first two examples above. Notice that the result of floating point division is 1.75 but the result of the integer division is simply 1. Take care that you choose the correct flavor of the division operator. If you’re working with expressions where you need floating point values, use the division operator /. If you want an integer result, use //.

Chú ý tới 2 ví dụ đầu tiên. Lưu ý rằng kết quả của phép chia số thập phân là 1.75 nhưng kết quả của phép chia số nguyên đơn giản là 1. Hãy chú ý rằng bạn chọn đúng (éo hiểu dịch hương vị là gì bây giờ) của phép chia toán tử Nếu bạn đang làm việc với biểu thức mà bạn cần giá trị số thập phân thì hãy sử dụng toán tử / và ngược lại là số nguyên thì //
The modulus operator, sometimes also called the remainder operator or integer remainder operatorworks on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for other operators.

Toán tử modul, cũng có khi gọi là toán tử số dư. Thôi mệt rồi, tóm lại là sử dụng dấu phần trăm (%) để lấy số dư. Cú pháp vẫn như cũ nhé.
1
quotient = 7 // 3     # This is the integer division operator
2
print(quotient)
3
remainder = 7 % 3
4
print(remainder)
5
(ch02_18)
In the above example, 7 divided by 3 is 2 when we use integer division and there is a remainder of 1 when we use the modulus operator.
The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another—if x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits.
Trong ví dụ này, 7 chia 2 nếu chúng ta sử dụng toán tử chia lấy số nguyên sẽ là 3 và nếu sử dụng toán tử lấy số dư sẽ là 1.
Cái thằng phép chia lấy số dư này rất là hữu ích. Ví dụ nhé, mày có thể check xem số này có thể chia hết cho số khác không?

Finally, returning to our time example, the remainder operator is extremely useful for doing conversions, say from seconds, to hours, minutes and seconds. If we start with a number of seconds, say 7684, the following program uses integer division and remainder to convert to an easier form. Step through it to be sure you understand how the division and remainder operators are being used to compute the correct values.

Cuối cùng quay trở lại ví dụ của chúng ta, toán tử thực sự / vô cùng/ vãi chưởng hữu dụng để chuyển đổi, thành giây, tiếng, phút. Mày phải chú ý là mày hiểu mày đang làm cái gì khi sử dụng các toán tử để nhận được kết quả đúng.

Python 2.7
1total_secs = 7684
2hours = total_secs // 3600
3secs_still_remaining = total_secs % 3600
4minutes =  secs_still_remaining // 60
5secs_finally_remaining = secs_still_remaining  % 60
  Step 1 of 5  
 line that has just executed
 next line to execute
Visualized using Online Python Tutor by Philip Guo
Frames
Objects
(ch02_19_codelens)

Nhận xét

Bài đăng phổ biến từ blog này

1.11. Formal and Natural Languages