2.11. Updating Variables - cập nhật các biến
http://interactivepython.org/runestone/static/thinkcspy/SimplePythonData/UpdatingVariables.html
(Chú ý đọc xong thì cac bạn vô trang chính để làm bài tập )
One of the most common forms of reassignment is an update where the new value of the variable depends on the old. For example,
Một trong những hình thức cập "gán" cho biến giá trị mới là dựa trên giá trị cũ.
(Chú ý đọc xong thì cac bạn vô trang chính để làm bài tập )
One of the most common forms of reassignment is an update where the new value of the variable depends on the old. For example,
Một trong những hình thức cập "gán" cho biến giá trị mới là dựa trên giá trị cũ.
This means get the current value of x, add one, and then update x with the new value. The new value of x is the old value of x plus 1. Although this assignment statement may look a bit strange, remember that executing assignment is a two-step process. First, evaluate the right-hand side expression. Second, let the variable name on the left-hand side refer to this new resulting object. The fact that
Điều này có nghĩa là lấy giá trị hiện tại của x, thêm một, và sau đó cập nhật x bằng giá trị mới. Giá trị mới của x là giá trị cũ của x cộng 1. Mặc dù câu lệnh gán này có thể hơi lạ, nhưng việc thực thi lchỉ à một quá trình gồm hai bước. Đầu tiên, kiểm tra giá trị bên phải. Thứ hai, biến bên tay trái sẽ có kết quả của bên tay phải (so với dấu = ý) việc xuất hiện cả 2 bên của biến x không phải vấn đề cần phải lo lắng và nó không ảnh hưởng tới kết quả cuối cùng.x
appears on both sides does not matter. The semantics of the assignment statement makes sure that there is no confusion as to the result.6 7
(ch07_update1)
If you try to update a variable that doesn’t exist, you get an error because Python evaluates the expression on the right side of the assignment operator before it assigns the resulting value to the name on the left. Before you can update a variable, you have to initialize it, usually with a simple assignment. In the above example,
Nếu bạn thử cập nhật một biến mà không có (chưa được định nghĩa), bạn sẽ nhận được một lỗi bởi vì giá trị Python nằm ở bên phải của toán tử trước khi nó được gán giá trị mới cho tên bên trái (tóm lại là giá trị bên trái dấu "= hoặc ==" sẽ nhận kết quả của các phép toán tử bên phải)
x
was initialized to 6.Nếu bạn thử cập nhật một biến mà không có (chưa được định nghĩa), bạn sẽ nhận được một lỗi bởi vì giá trị Python nằm ở bên phải của toán tử trước khi nó được gán giá trị mới cho tên bên trái (tóm lại là giá trị bên trái dấu "= hoặc ==" sẽ nhận kết quả của các phép toán tử bên phải)
Updating a variable by adding 1 is called an increment; subtracting 1 is called a decrement. Sometimes programmers also talk about bumping a variable, which means the same as incrementing it by 1.
Cập nhật một biến bằng cách thêm 1 thì gọi là increment (để tên tiếng anh đọc cho nó quen mắt), giảm 1 gọi là một decrement. đôi khi lập trình viên cũng gọi đó là bumping một biến để thể hiện là thêm 1.
Cập nhật một biến bằng cách thêm 1 thì gọi là increment (để tên tiếng anh đọc cho nó quen mắt), giảm 1 gọi là một decrement. đôi khi lập trình viên cũng gọi đó là bumping một biến để thể hiện là thêm 1.
Advanced Topics
- Topic 1: Python Beyond the Browser. This is a gentle introduction to using Python from the command line. We’ll cover this later, but if you are curious about what Python looks like outside of this eBook, you can have a look here. There are also instructions for installing Python on your computer here.
- Topic 2: Dive Into Python 3, this is an online textbook by Mark Pilgrim. If you have already had some programming experience, this book takes you off the deep end with both feet.
Topic 1: giới thiệu giao diện cơ bản lập trình python
topic 2: dành cho các bạn chuyên sâu. (cái này chưa học được nên chưa học :D)
Check your understanding
topic 2: dành cho các bạn chuyên sâu. (cái này chưa học được nên chưa học :D)
Check your understanding
data-11-1: What is printed when the following statements execute?
data-11-2: What is printed when the following statements execute?
data-11-3: Construct the code that will result in the value 134 being printed.
Drag from here
mybankbalance = mybankbalance + 34
print(mybankbalance)
mybankbalance = 100
Drop blocks here
Note
This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
(scratch_02)
Nhận xét
Đăng nhận xét