Hướng dẫn how to change the value of a variable in python - cách thay đổi giá trị biến trong python

Biến¶

Hãy nhớ lại rằng một biến là một nhãn cho một vị trí trong bộ nhớ. Nó có thể được sử dụng để giữ một giá trị. Trong các ngôn ngữ được gõ tĩnh, các biến có các loại được xác định trước và một biến chỉ có thể được sử dụng để giữ các giá trị của loại đó. Trong Python, chúng tôi có thể sử dụng lại cùng một biến để lưu trữ các giá trị thuộc bất kỳ loại nào.

Một biến tương tự như chức năng bộ nhớ được tìm thấy trong hầu hết các máy tính, trong đó nó chứa một giá trị có thể được truy xuất nhiều lần và lưu trữ một giá trị mới xóa cũ. Một biến khác với bộ nhớ của máy tính ở chỗ người ta có thể có nhiều biến lưu trữ các giá trị khác nhau và mỗi biến được đề cập theo tên.

Xác định các biến

Để xác định một biến mới trong Python, chúng tôi chỉ cần gán một giá trị cho một nhãn. Ví dụ: đây là cách chúng tôi tạo một biến có tên là

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
9, chứa giá trị số nguyên bằng 0:

Đây chính xác là cùng một cú pháp như gán một giá trị mới cho một biến hiện có được gọi là

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
9. Sau này trong chương này, chúng tôi sẽ thảo luận trong trường hợp nào mà tuyên bố này sẽ gây ra một biến mới được tạo ra.

Nếu chúng tôi cố gắng truy cập giá trị của một biến đã được xác định ở bất cứ đâu, trình thông dịch sẽ thoát với lỗi tên.

Chúng ta có thể xác định một số biến trong một dòng, nhưng điều này thường được coi là kiểu xấu:

# Define three variables at once:
count, result, total = 0, 0, 0

# This is equivalent to:
count = 0
result = 0
total = 0

Để phù hợp với phong cách lập trình tốt, chúng ta nên sử dụng các tên có ý nghĩa cho các biến.

Phạm vi biến đổi và tuổi thọ

Không phải tất cả các biến đều có thể truy cập được từ tất cả các phần của chương trình của chúng tôi và không phải tất cả các biến tồn tại trong cùng một khoảng thời gian. Trường hợp một biến có thể truy cập được và thời gian tồn tại trong bao lâu phụ thuộc vào cách xác định nó. Chúng tôi gọi là một phần của một chương trình trong đó một biến có thể truy cập phạm vi của nó và thời lượng mà biến tồn tại tuổi thọ của nó.

Một biến được xác định trong phần thân chính của tệp được gọi là biến toàn cầu. Nó sẽ được hiển thị trong toàn bộ tệp và cả bên trong bất kỳ tệp nào nhập tệp đó. Các biến toàn cầu có thể có những hậu quả không lường trước được vì các hiệu ứng phạm vi rộng của chúng-đó là lý do tại sao chúng ta gần như không bao giờ nên sử dụng chúng. Chỉ các đối tượng được dự định sẽ được sử dụng trên toàn cầu, như các chức năng và các lớp, nên được đặt vào không gian tên toàn cầu.

Một biến được xác định bên trong một hàm là cục bộ với hàm đó. Nó có thể truy cập từ điểm mà nó được xác định cho đến khi kết thúc hàm và tồn tại miễn là hàm được thực thi. Các tên tham số trong định nghĩa hàm hoạt động giống như các biến cục bộ, nhưng chúng chứa các giá trị mà chúng ta chuyển vào hàm khi chúng ta gọi nó. Khi chúng tôi sử dụng toán tử gán (

# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
1) bên trong một hàm, hành vi mặc định của nó là tạo một biến cục bộ mới - trừ khi một biến có cùng tên đã được xác định trong phạm vi cục bộ.

Dưới đây là một ví dụ về các biến trong các phạm vi khác nhau:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)

Ghi chú

Bên trong của một cơ thể lớp cũng là một phạm vi biến cục bộ mới. Các biến được xác định trong phần thân lớp (nhưng ngoài bất kỳ phương thức lớp nào) được gọi là thuộc tính lớp. Chúng có thể được tham chiếu bằng tên trần của chúng trong cùng một phạm vi, nhưng chúng cũng có thể được truy cập từ bên ngoài phạm vi này nếu chúng ta sử dụng toán tử truy cập thuộc tính (

# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
2) trên một lớp hoặc một thể hiện (một đối tượng sử dụng lớp đó làm loại của nó) . Một thuộc tính cũng có thể được đặt rõ ràng trên một thể hiện hoặc lớp từ bên trong một phương thức. Các thuộc tính được đặt trên các phiên bản được gọi là thuộc tính thể hiện. Các thuộc tính lớp được chia sẻ giữa tất cả các trường hợp của một lớp, nhưng mỗi trường hợp có các thuộc tính thể hiện riêng biệt. Chúng tôi sẽ xem xét điều này một cách chi tiết hơn trong chương về các lớp học.

Nhà điều hành chuyển nhượng

Như chúng ta đã thấy trong các phần trước, toán tử gán trong Python là một dấu hiệu bằng duy nhất (

# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
1). Toán tử này gán giá trị ở phía bên phải cho biến ở phía bên trái, đôi khi tạo biến trước. Nếu phía bên tay phải là một biểu thức (chẳng hạn như biểu thức số học), nó sẽ được đánh giá trước khi gán xảy ra. Đây là vài ví dụ:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1

Câu nói cuối cùng có thể trông hơi lạ nếu chúng ta giải thích

# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
1 dưới dạng dấu hiệu toán học - rõ ràng một số không thể bằng cùng một số cộng với một! Hãy nhớ rằng
# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
1 là một toán tử gán - câu lệnh này đang gán một giá trị mới cho biến
# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
6 bằng với giá trị cũ của
# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
6 cộng với một.

Gán một giá trị ban đầu cho biến được gọi là khởi tạo biến. Trong một số ngôn ngữ xác định một biến có thể được thực hiện trong một bước riêng biệt trước khi gán giá trị đầu tiên. Do đó, có thể trong các ngôn ngữ đó để một biến được xác định nhưng không có giá trị - điều này có thể dẫn đến lỗi hoặc hành vi bất ngờ nếu chúng ta cố gắng sử dụng giá trị trước khi nó được gán. Trong Python, một biến được xác định và gán một giá trị trong một bước duy nhất, vì vậy chúng tôi gần như sẽ không bao giờ gặp phải các tình huống như thế này.

Phía bên trái của câu lệnh gán phải là mục tiêu hợp lệ:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3

Một câu lệnh gán có thể có nhiều mục tiêu được phân tách bằng các dấu hiệu bằng. Biểu thức ở phía bên phải của dấu hiệu tương đương cuối cùng sẽ được gán cho tất cả các mục tiêu. Tất cả các mục tiêu phải hợp lệ:

# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b

Người vận hành phân công gộp

Chúng ta đã thấy rằng chúng ta có thể gán kết quả của một biểu thức số học cho một biến:

Đếm là một cái gì đó được thực hiện thường xuyên trong một chương trình. Ví dụ, chúng tôi có thể muốn giữ số lần xảy ra bao nhiêu lần một sự kiện nhất định bằng cách sử dụng một biến gọi là

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
9. Chúng tôi sẽ khởi tạo biến này về 0 và thêm một vào mỗi khi sự kiện xảy ra. Chúng tôi sẽ thực hiện bổ sung với tuyên bố này:

Đây thực tế là một hoạt động rất phổ biến. Python có một toán tử tốc ký,

# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
9, cho phép chúng tôi thể hiện nó sạch sẽ hơn, mà không phải viết tên của biến hai lần:

# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b

Có một toán tử tương tự,

# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
0, cho phép chúng tôi giảm số lượng:

# These statements mean exactly the same thing:
count = count - 3
count -= 3

Các toán tử gán hợp chất phổ biến khác được đưa ra trong bảng dưới đây:

Nhà điều hànhThí dụTương đương với
# both a and b will be set to zero:
a = b = 0

# this is illegal, because we can't set 0 to b:
a = 0 = b
9
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
2
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
3
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
0
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
5
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
6
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
7
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
8
# These statements mean exactly the same thing:
count = count + 1
count += 1

# We can increment a variable by any number we like.
count += 2
count += 7
count += a + b
9
# These statements mean exactly the same thing:
count = count - 3
count -= 3
0
# These statements mean exactly the same thing:
count = count - 3
count -= 3
1
# These statements mean exactly the same thing:
count = count - 3
count -= 3
2
# These statements mean exactly the same thing:
count = count - 3
count -= 3
3
# These statements mean exactly the same thing:
count = count - 3
count -= 3
4
# These statements mean exactly the same thing:
count = count - 3
count -= 3
5

Thông tin thêm về phạm vi: Biến ranh giới

Điều gì sẽ xảy ra nếu chúng ta muốn truy cập một biến toàn cầu từ bên trong một hàm? Có thể, nhưng làm như vậy đi kèm với một vài cảnh báo:

a = 0

def my_function():
    print(a)

my_function()

Câu lệnh in sẽ xuất ra

# These statements mean exactly the same thing:
count = count - 3
count -= 3
6, giá trị của biến toàn cầu
# These statements mean exactly the same thing:
count = count - 3
count -= 3
7, như bạn có thể mong đợi. Nhưng những gì về chương trình này?

a = 0

def my_function():
    a = 3
    print(a)

my_function()

print(a)

Khi chúng ta gọi hàm, câu lệnh in bên trong đầu ra

# These statements mean exactly the same thing:
count = count - 3
count -= 3
8 - nhưng tại sao câu lệnh in ở cuối đầu ra chương trình
# These statements mean exactly the same thing:
count = count - 3
count -= 3
6?

Theo mặc định, câu lệnh gán tạo các biến trong phạm vi cục bộ. Vì vậy, việc gán bên trong hàm không sửa đổi biến toàn cầu

# These statements mean exactly the same thing:
count = count - 3
count -= 3
7 - nó tạo ra một biến cục bộ mới gọi là
# These statements mean exactly the same thing:
count = count - 3
count -= 3
7 và gán giá trị
# These statements mean exactly the same thing:
count = count - 3
count -= 3
8 cho biến đó. Câu lệnh in đầu tiên đưa ra giá trị của biến cục bộ mới - bởi vì nếu một biến cục bộ có cùng tên với biến toàn cầu, biến cục bộ sẽ luôn được ưu tiên. Câu lệnh in cuối cùng in ra biến toàn cầu, vẫn không thay đổi.

Điều gì sẽ xảy ra nếu chúng ta thực sự muốn sửa đổi một biến toàn cầu từ bên trong một hàm? Chúng ta có thể sử dụng từ khóa

a = 0

def my_function():
    print(a)

my_function()
3:

a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)

Chúng tôi không được đề cập đến cả biến toàn cầu và biến cục bộ cùng tên bên trong cùng một hàm. Chương trình này sẽ cho chúng tôi một lỗi:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
0

Bởi vì chúng tôi đã tuyên bố

# These statements mean exactly the same thing:
count = count - 3
count -= 3
7 là toàn cầu, việc chuyển nhượng trong dòng thứ hai của hàm sẽ tạo ra một biến cục bộ
# These statements mean exactly the same thing:
count = count - 3
count -= 3
7. Điều này có nghĩa là chúng ta có thể đề cập đến biến toàn cầu
# These statements mean exactly the same thing:
count = count - 3
count -= 3
7 ở nơi khác trong hàm, ngay cả trước dòng này! Câu lệnh in đầu tiên hiện nay đề cập đến biến cục bộ
# These statements mean exactly the same thing:
count = count - 3
count -= 3
7 - nhưng biến này không có giá trị trong dòng đầu tiên, bởi vì chúng tôi đã chỉ định nó!

Lưu ý rằng thường là thực tế rất tệ để truy cập các biến toàn cầu từ các chức năng bên trong và thậm chí thực hành tồi tệ hơn để sửa đổi chúng. Điều này gây khó khăn cho việc sắp xếp chương trình của chúng tôi thành các phần được đóng gói một cách hợp lý không ảnh hưởng đến nhau theo những cách bất ngờ. Nếu một hàm cần truy cập một số giá trị bên ngoài, chúng ta nên truyền giá trị vào hàm dưới dạng tham số. Nếu hàm là phương pháp của một đối tượng, đôi khi phù hợp để biến giá trị thành một thuộc tính của cùng một đối tượng - chúng ta sẽ thảo luận về điều này trong chương về hướng đối tượng.

Ghi chú

Ngoài ra còn có từ khóa

a = 0

def my_function():
    print(a)

my_function()
8 trong Python - khi chúng ta tổ chức năng bên trong một hàm khác, nó cho phép chúng ta sửa đổi một biến ở hàm bên ngoài từ bên trong hàm bên trong (hoặc, nếu hàm được lồng nhiều lần, một biến trong một trong những biến các chức năng bên ngoài). Nếu chúng tôi sử dụng từ khóa
a = 0

def my_function():
    print(a)

my_function()
3, câu lệnh gán sẽ tạo biến trong phạm vi toàn cầu nếu nó chưa tồn tại. Tuy nhiên, nếu chúng ta sử dụng từ khóa
a = 0

def my_function():
    print(a)

my_function()
8, biến phải được xác định, bởi vì Python không thể xác định được trong phạm vi nào nó nên được tạo.

Bài tập 1¶

  1. Mô tả phạm vi của các biến

    # These statements mean exactly the same thing:
    count = count - 3
    count -= 3
    
    7,
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    2,
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    3 và
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    4 trong ví dụ này:

    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    1

  2. Tuổi thọ của các biến này là gì? Khi nào chúng sẽ được tạo ra và phá hủy?

  3. Bạn có thể đoán điều gì sẽ xảy ra nếu chúng ta chỉ định

    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    3 giá trị
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    6 thay thế không?

  4. Tại sao đây sẽ là một vấn đề? Bạn có thể nghĩ ra một cách để tránh nó?

Sửa đổi các giá trị

Hằng số trong

Trong một số ngôn ngữ, có thể xác định các biến đặc biệt chỉ có thể được gán một giá trị một lần - một khi giá trị của chúng đã được đặt, chúng không thể thay đổi. Chúng tôi gọi các loại hằng số biến này. Python không cho phép chúng tôi đặt ra một hạn chế như vậy đối với các biến, nhưng có một quy ước được sử dụng rộng rãi để đánh dấu các biến nhất định để chỉ ra rằng giá trị của chúng không có nghĩa là thay đổi: chúng tôi viết tên của chúng trong tất cả các mũ, với các từ ngữ tách biệt:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
2

Tại sao chúng ta bận tâm xác định các biến mà chúng ta không có ý định thay đổi? Xem xét ví dụ này:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
3

Có một số lý do tốt để xác định

a = 0

def my_function():
    a = 3
    print(a)

my_function()

print(a)
7 thay vì chỉ viết
a = 0

def my_function():
    a = 3
    print(a)

my_function()

print(a)
8 bên trong câu lệnh in. Đầu tiên, điều này cung cấp cho số một nhãn mô tả giải thích nó là gì - điều này làm cho mã dễ hiểu hơn. Thứ hai, cuối cùng chúng tôi có thể cần phải tham khảo số này trong chương trình của chúng tôi nhiều lần. Nếu chúng ta cần cập nhật mã của mình với giá trị mới cho nhãn hiệu tối đa, chúng ta sẽ chỉ phải thay đổi nó ở một nơi, thay vì tìm mọi nơi được sử dụng-những thay thế đó thường dễ bị lỗi.

Các số theo nghĩa đen nằm rải rác trong một chương trình được gọi là Số ma thuật của Hồi giáo - sử dụng chúng được coi là phong cách mã hóa kém. Điều này không áp dụng cho các số nhỏ được coi là tự giải thích-nó dễ hiểu tại sao tổng số được khởi tạo về 0 hoặc tăng bởi một.

Đôi khi chúng tôi muốn sử dụng một biến để phân biệt giữa một số tùy chọn riêng biệt. Rất hữu ích khi đề cập đến các giá trị tùy chọn bằng cách sử dụng các hằng số thay vì sử dụng trực tiếp chúng nếu chính các giá trị không có ý nghĩa nội tại:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
4

Trong ví dụ trên, các giá trị

a = 0

def my_function():
    a = 3
    print(a)

my_function()

print(a)
6,
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
0 và
# These statements mean exactly the same thing:
count = count - 3
count -= 3
8 không quan trọng - chúng hoàn toàn vô nghĩa. Chúng tôi có thể sử dụng tốt như nhau
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
2,
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
3 và
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
4 hoặc chuỗi
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
5,
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
6 và
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
7. Điều quan trọng duy nhất là ba giá trị phải khác nhau. Nếu chúng ta sử dụng các số trực tiếp thay vì các hằng số, chương trình sẽ khó hiểu hơn nhiều. Sử dụng các chuỗi có ý nghĩa sẽ làm cho mã dễ đọc hơn, nhưng chúng tôi có thể vô tình phạm sai lầm chính tả trong khi đặt một trong các giá trị và không chú ý - nếu chúng tôi đặt tên của một trong các hằng số, chúng tôi có nhiều khả năng gặp lỗi ngay lập tức.

Một số thư viện Python xác định các hằng số phổ biến cho sự thuận tiện của chúng tôi, ví dụ:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
5

Lưu ý rằng nhiều hằng số tích hợp don don tuân theo quy ước đặt tên tất cả các phạm vi.

Các loại có thể thay đổi và bất biến

Một số giá trị trong Python có thể được sửa đổi, và một số không thể. Điều này không bao giờ có nghĩa là chúng ta có thể thay đổi giá trị của một biến - nhưng nếu một biến chứa một giá trị của một loại bất biến, chúng ta chỉ có thể gán cho nó một giá trị mới. Chúng ta không thể thay đổi giá trị hiện có theo bất kỳ cách nào.

Các số nguyên, số và chuỗi dấu phẩy động đều là các loại bất biến-trong tất cả các ví dụ trước, khi chúng tôi thay đổi các giá trị của các biến hiện tại, chúng tôi đã sử dụng toán tử gán để gán cho chúng các giá trị mới:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
6

Ngay cả toán tử này cũng không sửa đổi giá trị của

a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
8 tại chỗ-nó cũng gán một giá trị mới:

Chúng tôi đã gặp phải bất kỳ loại có thể thay đổi nào, nhưng chúng tôi sẽ sử dụng chúng rộng rãi trong các chương sau. Danh sách và từ điển là có thể thay đổi, và hầu hết các đối tượng mà chúng ta có thể tự viết:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
7

Thông tin thêm về Input¶

Trong các phần trước của đơn vị này, chúng tôi đã học cách tạo chương trình hiển thị thông báo bằng hàm

a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
9 hoặc đọc giá trị chuỗi từ người dùng bằng hàm
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
00. Điều gì sẽ xảy ra nếu chúng ta muốn người dùng nhập số hoặc các loại biến khác? Chúng tôi vẫn sử dụng hàm
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
00, nhưng chúng tôi phải chuyển đổi các giá trị chuỗi được trả về bởi
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
00 thành các loại mà chúng tôi muốn. Đây là một ví dụ đơn giản:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
8

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 là một hàm chuyển đổi các giá trị của các loại khác nhau thành INTS. Chúng tôi sẽ thảo luận về chuyển đổi loại chi tiết hơn trong phần tiếp theo, nhưng bây giờ, điều quan trọng là phải biết rằng
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 sẽ không thể chuyển đổi một chuỗi thành một số nguyên nếu nó chứa bất cứ điều gì ngoại trừ các chữ số. Chương trình trên sẽ thoát với lỗi nếu người dùng nhập
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
05,
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
06 hoặc thậm chí
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
07. Khi chúng tôi viết một chương trình dựa trên đầu vào của người dùng, có thể không chính xác, chúng tôi cần thêm một số biện pháp bảo vệ để chúng tôi có thể phục hồi nếu người dùng mắc lỗi. Ví dụ: chúng ta có thể phát hiện xem người dùng đã nhập đầu vào và thoát xấu với thông báo lỗi tốt hơn:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
9

Chương trình này vẫn sẽ chỉ cố gắng đọc trong đầu vào một lần và thoát nếu nó không chính xác. Nếu chúng tôi muốn tiếp tục yêu cầu người dùng đầu vào cho đến khi đúng, chúng tôi có thể làm điều gì đó như sau:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
0

Chúng tôi sẽ tìm hiểu thêm về các giá trị, vòng lặp và ngoại lệ Boolean sau này.

Ví dụ: Tính toán tiêu thụ xăng của xe hơi

Trong ví dụ này, chúng tôi sẽ viết một chương trình đơn giản yêu cầu người dùng cho khoảng cách đi bằng xe hơi và giá trị tiền tệ của xăng được sử dụng để che đi khoảng cách đó. Từ thông tin này, cùng với giá mỗi lít xăng, chương trình sẽ tính toán hiệu quả của chiếc xe, cả hai lít trên 100 km và km mỗi lít.

Đầu tiên chúng tôi sẽ định nghĩa giá xăng là một hằng số ở đầu. Điều này sẽ giúp chúng tôi dễ dàng cập nhật giá khi thay đổi vào thứ Tư đầu tiên hàng tháng:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
1

Khi chương trình bắt đầu, chúng tôi muốn in ra một tin nhắn chào mừng:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
2

Hỏi người dùng cho tên của mình:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
3

Hỏi người dùng cho khoảng cách di chuyển:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
4

Sau đó hỏi người dùng số tiền đã trả:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
5

Bây giờ chúng tôi sẽ thực hiện các tính toán:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
6

Cuối cùng, chúng tôi đưa ra kết quả:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
7

Bài tập 2¶

  1. Viết một chương trình Python để chuyển đổi nhiệt độ được đưa ra theo độ Fahrenheit thành tương đương với độ C. Bạn có thể giả sử rằng t_c = (5/9) x (t_f - 32), trong đó t_c là nhiệt độ trong ° C và T_F là nhiệt độ tính bằng ° F. Chương trình của bạn nên yêu cầu người dùng cho một giá trị đầu vào và in đầu ra. Các giá trị đầu vào và đầu ra phải là số điểm nổi.T_c = (5/9) x (T_f - 32), where T_c is the temperature in °C and T_f is the temperature in °F. Your program should ask the user for an input value, and print the output. The input and output values should be floating-point numbers.
  2. Điều gì có thể làm cho chương trình này sụp đổ? Chúng ta cần làm gì để xử lý tình huống này một cách duyên dáng hơn?

Loại chuyển đổi

Khi chúng tôi viết nhiều chương trình hơn, chúng tôi thường sẽ thấy rằng chúng tôi cần chuyển đổi dữ liệu từ loại này sang loại khác, ví dụ từ chuỗi sang số nguyên hoặc từ số nguyên sang số điểm nổi. Có hai loại chuyển đổi loại trong Python: chuyển đổi ngầm và rõ ràng.

Chuyển đổi ngầm

Nhớ lại từ phần về các toán tử dấu phẩy động rằng chúng ta có thể tùy ý kết hợp các số nguyên và số điểm nổi trong một biểu thức số học-và kết quả của bất kỳ biểu thức nào như vậy sẽ luôn là một số điểm nổi. Điều này là do Python sẽ chuyển đổi các số nguyên thành số điểm nổi trước khi đánh giá biểu thức. Đây là một chuyển đổi ngầm - chúng tôi không phải tự chuyển đổi bất cứ điều gì. Thường không mất độ chính xác khi một số nguyên được chuyển đổi thành số điểm nổi.

Ví dụ: số nguyên

a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
0 sẽ tự động được chuyển đổi thành số điểm nổi trong ví dụ sau:

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
09 là
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
10 trong khi
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
0 là
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03. Python sẽ tự động chuyển đổi các toán hạng để chúng cùng loại. Trong trường hợp này, điều này đạt được nếu số nguyên
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
0 được chuyển đổi thành điểm nổi tương đương
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
14. Sau đó, hai số điểm nổi có thể được nhân lên.

Hãy để một cái nhìn vào một ví dụ phức tạp hơn:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
8

Python thực hiện các hoạt động theo thứ tự ưu tiên và quyết định xem có cần chuyển đổi trên cơ sở hoạt động hay không. Trong ví dụ

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
15 của chúng tôi có ưu tiên cao nhất, vì vậy nó sẽ được xử lý trước.
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
16 và
# These statements mean exactly the same thing:
count = count - 3
count -= 3
8 đều là số nguyên và
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
15 là toán tử phân chia số nguyên - kết quả của hoạt động này là số nguyên
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
0. Bây giờ chúng tôi còn lại với
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
20. Việc bổ sung và trừ ở cùng một mức độ ưu tiên, vì vậy chúng được đánh giá từ trái sang phải, bắt đầu bằng việc bổ sung.
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
0 đầu tiên được chuyển đổi thành số điểm nổi
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
14 và hai số điểm nổi được thêm vào, khiến chúng tôi với
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
23. Kết quả của phép trừ điểm nổi này là
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
14, được gán cho
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
25.

Chuyển đổi rõ ràng

Chuyển đổi số từ

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
10 thành
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 sẽ dẫn đến mất độ chính xác. Ví dụ: cố gắng chuyển đổi
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
28 thành
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 - không thể làm điều này mà không mất độ chính xác. Để điều này xảy ra, chúng ta phải nói rõ ràng rằng Python rằng chúng ta biết rằng độ chính xác sẽ bị mất. Ví dụ: chúng ta cần yêu cầu trình biên dịch chuyển đổi
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
10 thành
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 như thế này:

Hàm

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 chuyển đổi
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
10 thành
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 bằng cách loại bỏ phần phân số - nó sẽ luôn làm tròn! Nếu chúng ta muốn kiểm soát nhiều hơn về cách mà số được làm tròn, chúng ta sẽ cần sử dụng một chức năng khác:

a_number = 5              # a_number becomes 5
a_number = total          # a_number becomes the value of total
a_number = total + 5      # a_number becomes the value of total + 5
a_number = a_number + 1   # a_number becomes the value of a_number + 1
9

Chuyển đổi rõ ràng đôi khi cũng được gọi là đúc-chúng ta có thể đọc về một

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
10 được đúc theo
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 hoặc ngược lại.

Chuyển đổi đến và từ chuỗi

Như chúng ta đã thấy trong các phần trước, Python hiếm khi thực hiện các chuyển đổi ngầm đến và từ

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
37 - chúng ta thường phải chuyển đổi các giá trị rõ ràng. Nếu chúng ta chuyển một số duy nhất (hoặc bất kỳ giá trị nào khác) cho hàm
a = 0

def my_function():
    global a
    a = 3
    print(a)

my_function()

print(a)
9, nó sẽ được chuyển đổi thành một chuỗi tự động - nhưng nếu chúng ta cố gắng thêm một số và một chuỗi, chúng ta sẽ gặp lỗi:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
0

Để chuyển đổi số thành chuỗi, chúng ta có thể sử dụng định dạng chuỗi - đây thường là cách sạch nhất và dễ đọc nhất để chèn nhiều giá trị vào một thông báo. Nếu chúng ta muốn chuyển đổi một số duy nhất thành một chuỗi, chúng ta cũng có thể sử dụng chức năng

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
37 một cách rõ ràng:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
1

Thêm về Chuyển đổi Or

Trong Python, các chức năng như

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
37,
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 và
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
10 sẽ cố gắng chuyển đổi bất cứ điều gì thành các loại tương ứng của chúng-ví dụ, chúng ta có thể sử dụng hàm ____10103 để chuyển đổi chuỗi thành số nguyên hoặc chuyển đổi số điểm nổi sang số nguyên. Lưu ý rằng mặc dù
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
03 có thể chuyển đổi một chiếc phao thành một số nguyên, nó có thể chuyển đổi một chuỗi chứa một float thành một số nguyên trực tiếp!

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
2

Các giá trị của loại

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
45 có thể chứa giá trị
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
46 hoặc
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
47. Các giá trị này được sử dụng rộng rãi trong các câu lệnh có điều kiện, thực thi hoặc không thực hiện các phần của chương trình của chúng tôi tùy thuộc vào một số điều kiện nhị phân:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
3

Điều kiện thường là một biểu thức đánh giá giá trị boolean:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
4

Tuy nhiên, hầu hết mọi giá trị có thể được chuyển đổi hoàn toàn thành Boolean nếu nó được sử dụng trong một câu như thế này:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
5

Điều này thường hoạt động theo cách mà bạn mong đợi: các số khác không là các giá trị

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
46 và số 0 là
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
47. Tuy nhiên, chúng ta cần cẩn thận khi sử dụng chuỗi - chuỗi trống được coi là
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
47, nhưng bất kỳ chuỗi nào khác là
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
46 - thậm chí
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
52 và
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)
53!

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
6

Bài tập 3¶

  1. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    54 thành một chiếc phao.
  2. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    55 thành một số nguyên (với làm tròn).
  3. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    54 thành một số nguyên (với làm tròn).
  4. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    55 thành một chuỗi.
  5. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    58 thành một chuỗi.
  6. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    58 thành một chiếc phao.
  7. Chuyển đổi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    58 thành boolean.

Câu trả lời cho các bài tập

Trả lời cho bài tập 1¶

  1. # These statements mean exactly the same thing:
    count = count - 3
    count -= 3
    
    7 là một biến cục bộ trong phạm vi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    62 vì nó là một tên đối số.
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    2 cũng là một biến cục bộ bên trong
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    62, vì nó được gán một giá trị bên trong
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    62.
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    3 và
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    4 đều là các biến toàn cầu. Không có vấn đề gì khi
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    4 được tạo ra bên trong một khối
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    69, bởi vì bên trong một khối
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    69 không phải là một phạm vi mới - mọi thứ bên trong khối đều là một phần của cùng phạm vi với bên ngoài (trong trường hợp này là phạm vi toàn cầu). Chỉ các định nghĩa chức năng (bắt đầu bằng
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    71) và các định nghĩa lớp (bắt đầu bằng
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    72) cho thấy sự khởi đầu của một mức độ phạm vi mới.
  2. Cả
    # These statements mean exactly the same thing:
    count = count - 3
    count -= 3
    
    7 và
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    2 sẽ được tạo mỗi khi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    62 được gọi và phá hủy khi
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    62 đã hoàn tất việc thực hiện.
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    3 được tạo khi nó được gán giá trị
    # These statements mean exactly the same thing:
    count = count - 3
    count -= 3
    
    8 và tồn tại trong phần còn lại của việc thực hiện chương trình.
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    4 được tạo bên trong khối
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    69 (khi nó được gán giá trị được trả về từ hàm) và cũng tồn tại cho phần còn lại của việc thực hiện chương trình.
  3. Như chúng ta sẽ tìm hiểu trong chương tiếp theo, các khối
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    69 được thực thi có điều kiện. Nếu
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    3 không lớn hơn
    # These statements mean exactly the same thing:
    count = count - 3
    count -= 3
    
    8 trong chương trình này, khối
    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    69 sẽ không được thực thi và nếu điều đó xảy ra thì biến
    a = 0
    
    def my_function():
        a = 3
        print(a)
    
    my_function()
    
    print(a)
    
    4 sẽ không bao giờ được tạo.
  4. Chúng tôi có thể sử dụng biến sau này trong mã, giả sử rằng nó luôn tồn tại và có sự cố chương trình của chúng tôi một cách bất ngờ nếu nó không có. Nó được coi là thực hành mã hóa kém để cho phép một biến được xác định hoặc không xác định tùy thuộc vào kết quả của một tuyên bố có điều kiện. Tốt hơn là đảm bảo rằng luôn luôn được xác định, bất kể điều gì - ví dụ, bằng cách gán nó một số giá trị mặc định khi bắt đầu. Việc kiểm tra xem một biến có giá trị mặc định dễ dàng hơn nhiều và sạch sẽ hơn nhiều hay không.

Trả lời cho bài tập 2¶

  1. Đây là một chương trình ví dụ:

    # this is fine:
    a = 3
    
    # these are all illegal:
    3 = 4
    3 = a
    a + b = 3
    
    7

    Ghi chú

    Biểu tượng định dạng

    # This is a global variable
    a = 0
    
    if a == 0:
        # This is still a global variable
        b = 1
    
    def my_function(c):
        # this is a local variable
        d = 3
        print(c)
        print(d)
    
    # Now we call the function, passing the value 7 as the first and only parameter
    my_function(7)
    
    # a and b still exist
    print(a)
    print(b)
    
    # c and d don't exist anymore -- these statements will give us name errors!
    print(c)
    print(d)
    
    86 được sử dụng với phao và hướng dẫn Python chọn một cách có thể đọc được của con người để hiển thị phao.

  2. Chương trình có thể gặp sự cố nếu người dùng nhập giá trị không thể chuyển đổi thành số dấu phẩy động. Chúng tôi sẽ cần thêm một số loại kiểm tra lỗi để đảm bảo rằng điều này không xảy ra - ví dụ, bằng cách lưu trữ giá trị chuỗi và kiểm tra nội dung của nó. Nếu chúng tôi thấy rằng giá trị đã nhập không hợp lệ, chúng tôi có thể in thông báo lỗi và thoát hoặc tiếp tục nhắc người dùng để nhập cho đến khi nhập hợp lệ được nhập.

Trả lời cho bài tập 3¶

Đây là câu trả lời ví dụ:

# this is fine:
a = 3

# these are all illegal:
3 = 4
3 = a
a + b = 3
8