2.4. Variables

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.

Một trong những đặc điểm sức mạnh của một ngôn ngữ lập trình là khả năng vận dụng của các biến. Một biến là một cái tên (có thể coi là nhãn) mà dẫn tới 1 giá trị.

Assignment statements create new variables and also give them values to refer to.
Câu lệnh gán tạo một biến mới và đưa chúng những giá trị để tham khảo.
message = "What's up, Doc?"
n = 17
pi = 3.14159

This example makes three assignments. The first assigns the string value "What's up, Doc?" to a new variable named message. The second gives the integer 17 to n, and the third assigns the floating-point number 3.14159 to a variable called pi.

ví dụ này tạo ra 3 nhiệm vụ. Nhiệm vụ đầu tiên là (gán) chuỗi giá trị "What's up, Doc?" cho một biến tên message. (Nhiệm vụ) thứ 2 là đưa ra số nguyên 17 cho n, và  (nhiệm vụ) thứ 3 là định dạng sô thập phân 3.14159 thành một biến gọi là pi.


The assignment token, =, should not be confused with equality (we will see later that equality uses the== token). The assignment statement links a name, on the left hand side of the operator, with a value, on the right hand side. This is why you will get an error if you enter:

Mã chuyển nhượng (cái này ko chắc lắm), = , chúng ta không nên lầm lẫn bởi dấu bằng (sau này chúng ta sẽ thấy dấu bằng với ký tự ==). Phép gán giữa 1 cái tên, phía bên trái của toán tử, với một giá trị, phía bên phải (của toán tử). Đây là lý do tại sao bạn sẽ nhận được 1 lỗi nếu bạn nhập:

17 = n

Tip
When reading or writing code, say to yourself “n is assigned 17” or “n gets the value 17” or “n is a reference to the object 17” or “n refers to the object 17”. Don’t say “n equals 17”.

Mẹo:
Khi bạn đọc hoặc viết mã, hãy nói với bản thân "n gán 17" hoặc"n nhận giá trị 17" hoặc "n là một (giá trị) tham khảo cho đối tượng 17" hoặc "n dẫn tới giá trị 17". Đừng nói " n bằng 17"

A common way to represent variables on paper is to write the name with an arrow pointing to the variable’s value. This kind of figure, known as a reference diagram, is often called a state snapshot because it shows what state each of the variables is in at a particular instant in time. (Think of it as the variable’s state of mind). This diagram shows the result of executing the assignment statements shown above.

Cách phổ biến để trình bày cho các biến trên giấy là viết tên và một mũi tên hướng tới giá trị của biến. Cách này gọi là sơ đồ tham chiếu, nó thường được gọi là một hình ảnh trạng thái bởi vì nó hiển thị trạng thái của mỗi biến trong một thời điểm cụ thể. (Hãy suy nghĩ về nó như giá trị cốt lõi của biến). Sơ đô này cho thấy cho thấy kết quả của việc gán được trình bày ở trên.

Reference Diagram
If you ask Python to evaluate a variable, it will produce the value that is currently linked to the variable. In other words, evaluating a variable will give you the value that is referred to by the variable.

Sơ đồ tham khảo

Nếu bạn đặt câu hỏi Python để đánh giá 1 biến, nó sẽ đưa ra một giá trị mà được dẫn tới biến. Nói cách khác, việc đánh giá một biến sẽ cho bạn một giá trị mà nó dẫn tới biến.

RunLoad HistoryShow CodeLens

1
message = "What's up, Doc?"
2
n = 17
3
pi = 3.14159
4

5
print(message)
6
print(n)
7
print(pi)
8

(ch02_9)
In each case the result is the value of the variable. To see this in even more detail, we can run the program using codelens.

Python 2.7
1 message = "What's up, Doc?"
2 n = 17
3 pi = 3.14159
4
5 print(message)
6 print(n)
7 print(pi)
<< First < Back Step 1 of 6 Forward > Last >>
 line that has just executed
 next line to execute

Frames
Objects
(ch02_9_codelens)
Now, as you step through the statements, you can see the variables and the values they reference as those references are created.

Variables also have types; again, we can ask the interpreter what they are.

RunLoad HistoryShow CodeLens

1
message = "What's up, Doc?"
2
n = 17
3
pi = 3.14159
4

5
print(type(message))
6
print(type(n))
7
print(type(pi))
8

(ch02_10)
The type of a variable is the type of the object it currently refers to.

We use variables in a program to “remember” things, like the current score at the football game. But variables are variable. This means they can change over time, just like the scoreboard at a football game. You can assign a value to a variable, and later assign a different value to the same variable.

Chúng ta sử dụng biến trong một chương trình để "nhớ" mọi thứ, tương tự như việc mình ghi bàn trong trận bóng đá. Nhưng biến là có thể thay đổi. Điều này có nghĩa là chúng có thể thay đổi trong suốt thời gian, giống như biển tỷ số trong 1 trận bóng đá. Bạn có thể gán một giá trị cho một biến và gán một giá trị khác cho cùng một biến vào lúc sau.

Note
This is different from math. In math, if you give x the value 3, it cannot change to refer to a different value half-way through your calculations!
To see this, read and then run the following program. You’ll notice we change the value of day three times, and on the third assignment we even give it a value that is of a different type.

Ghi chú:
Đây là sự khác nhau với toán học. Trong toán học, nếu bạn đưa (biến) x giá trị 3, nó sẽ không thể thay đổi một giá trị khác trong khi bạn đang tính toán nửa chừng.

Python 2.7
1 day = "Thursday"
2 print(day)
3 day = "Friday"
4 print(day)
5 day = 21
6 print(day)
<< First < Back Step 1 of 6 Forward > Last >>
 line that has just executed
 next line to execute

Frames
Objects
(ch02_11)

A great deal of programming is about having the computer remember things. For example, we might want to keep track of the number of missed calls on your phone. Each time another call is missed, we will arrange to update or change the variable so that it will always reflect the correct value.

Rất nhiều chương trình lập trình về việc để máy tính ghi nhớ nhiều thứ. Ví dụ, chúng ta có thể muốn theo dõi những số điện thoại gọi bị lỡ trên điện thoại của bạn. Mỗi một cuộc điện thoại lỡ gọi vào thời gian khác nhau, chúng ta sẽ phải cập nhật hoặc thay đổi biến để chúng luôn hiển thị giá trị chính xác

reference:
http://interactivepython.org/runestone/static/thinkcspy/SimplePythonData/Variables.html#variables

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

1.11. Formal and Natural Languages

3.10 Stack diagrams - sơ đồ xếp chồng