2.6. Statements and Expressions

http://media.interactivepython.org/thinkcsVideos/Expressions.mov

A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we’ll see shortly are while statements, forstatements, if statements, and import statements. (There are other kinds too!)
Một câu lệnh là một chỉ thị mà trình biên dịch Python có thể thực thi. Chúng ta có thể nhìn thấy câu lệnh được giao gần đây. Một vài loại khác của câu lệnh chúng ta mà chúng ta có thể nhìn như câu lệnh white, for, if, import (Cũng có những loại khác)

An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to print an expression, the interpreter evaluates the expression and displays the result.

Một cách biểu thức là một sự kết hợp của giá trị, biến, toán tử và gọi là hàm. Các biểu thức cần phải được đánh giá. Nếu bạn yêu cầu Python để in ra một biểu thức, trình thông dịch đánh giá biểu thức và hiển thị kết quả
1
print(1 + 1)
2
print(len("hello"))
3
(ch02_13)
In this example len is a built-in Python function that returns the number of characters in a string. We’ve previously seen the print and the type functions, so this is our third example of a function!
The evaluation of an expression produces a value, which is why expressions can appear on the right hand side of assignment statements. A value all by itself is a simple expression, and so is a variable. Evaluating a variable gives the value that the variable refers to.
1
y = 3.14
2
x = len("hello")
3
print(x)
4
print(y)
5
(ch02_14)
If we take a look at this same example in the Python shell, we will see one of the distinct differences between statements and expressions.

Nếu chúng ta nhìn vào vài ví dụ trong lớp vỏ Python, chúng ta sẽ nhìn thấy sự khác một vài những sự khác biệt giữa câu lệnh và biểu thức.
>>> y = 3.14
>>> x = len("hello")
>>> print(x)
5
>>> print(y)
3.14
>>> y
3.14
>>>
Note that when we enter the assignment statement, y = 3.14, only the prompt is returned. There is no value. This is due to the fact that statements, such as the assignment statement, do not return a value. They are simply executed.

Ghi chú rằng khi chúng ta nhập câu lệnh được giao, y = 3.14, chỉ có dấu nhắc trở lại. Đây không phải là giá trị. Đây là do các câu lệnh, ví như là câu lệnh gán, không đưa ra một giá trị. Chúng chỉ đơn giản là thực hiện nó.
(ý rằng là nếu chỉ gán thôi không thì trình biên dịch của python chỉ thực hiện gán nhưng sẽ không trả về giá trị)
On the other hand, the result of executing the assignment statement is the creation of a reference from a variable, y, to a value, 3.14. When we execute the print function working on y, we see the value that y is referring to. In fact, evaluating y by itself results in the same response.

Mặt khác, trong kết quả thực hiện của câu lệnh gán là được tạo ra một tham chiếu từ một biến, cho một giá trị, 3.14. Khi chúng ta thực thi hàm print với y, chúng ta sẽ thấy giá trị của y được hiển thị. Tương tự như vậy, giá trị y sẽ được hiển thị kết quả khi chúng ta đánh giá nó
Reference:
http://interactivepython.org/runestone/static/thinkcspy/SimplePythonData/StatementsandExpressions.html

Nhận xét

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

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

Bài ôn tập chương 1 - Quan trọng

4.5. Flow of Execution of the for Loop