Hướng dẫn what are keywords in python *? - từ khóa trong python * là gì?

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Khám phá các từ khóa trong Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Exploring Keywords in Python

Show

Mỗi ngôn ngữ lập trình đều có các từ dành riêng, hoặc từ khóa, có ý nghĩa và hạn chế cụ thể xung quanh cách sử dụng chúng. Python không khác. Từ khóa Python là các khối xây dựng cơ bản của bất kỳ chương trình Python nào.keywords, that have specific meanings and restrictions around how they should be used. Python is no different. Python keywords are the fundamental building blocks of any Python program.

Trong bài viết này, bạn sẽ tìm thấy một giới thiệu cơ bản về tất cả các từ khóa Python cùng với các tài nguyên khác sẽ hữu ích để tìm hiểu thêm về từng từ khóa.

Đến cuối bài viết này, bạn sẽ có thể:

  • Xác định từ khóa Python Python keywords
  • Hiểu những gì mỗi từ khóa được sử dụng cho what each keyword is used for
  • Làm việc với các từ khóa được lập trình bằng cách sử dụng mô -đun
    >>> def func():
    ...     print("hello")
    ...
    >>> x = func()
    hello
    >>> print(x)
    None
    
    5
    with keywords programmatically using the
    >>> def func():
    ...     print("hello")
    ...
    >>> x = func()
    hello
    >>> print(x)
    None
    
    5 module

Từ khóa Python

Từ khóa Python là những từ dành riêng đặc biệt có ý nghĩa và mục đích cụ thể và có thể được sử dụng cho bất cứ điều gì ngoại trừ những mục đích cụ thể đó. Các từ khóa này luôn có sẵn, bạn sẽ không bao giờ phải nhập chúng vào mã của bạn.

Các từ khóa Python khác với các chức năng và loại tích hợp Python. Các chức năng và loại tích hợp cũng luôn có sẵn, nhưng chúng không bị hạn chế như các từ khóa trong việc sử dụng của chúng.

Một ví dụ về một cái gì đó bạn có thể làm với các từ khóa Python là gán một cái gì đó cho chúng. Nếu bạn cố gắng, thì bạn sẽ nhận được

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
6. Bạn đã giành được một
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
6 nếu bạn cố gắng gán một cái gì đó cho một chức năng hoặc loại tích hợp, nhưng nó vẫn không phải là một ý tưởng tốt. Để có thể giải thích sâu hơn về các cách từ khóa có thể bị sử dụng sai, hãy xem cú pháp không hợp lệ trong Python: Lý do phổ biến cho cú pháp.

Kể từ Python 3.8, có ba mươi lăm từ khóa trong Python. Ở đây họ có liên kết đến các phần có liên quan trong suốt phần còn lại của bài viết này:

Bạn có thể sử dụng các liên kết này để nhảy vào các từ khóa mà bạn muốn đọc, hoặc bạn có thể tiếp tục đọc cho một tour du lịch có hướng dẫn.

Cách xác định từ khóa Python

Danh sách các từ khóa Python đã thay đổi theo thời gian. Ví dụ: từ khóa

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8 và
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 weren được thêm vào cho đến khi Python 3.7. Ngoài ra, cả
left if not left else right
0 và
left if not left else right
1 đều là từ khóa trong Python 2.7 nhưng đã được biến thành các hàm tích hợp trong Python 3+ và không còn xuất hiện trong danh sách các từ khóa.

Trong các phần dưới đây, bạn sẽ học được một số cách để biết hoặc tìm ra từ nào là từ khóa trong Python.

Sử dụng IDE với cú pháp làm nổi bật

Có rất nhiều id python tốt ngoài kia. Tất cả chúng sẽ làm nổi bật các từ khóa để phân biệt chúng với các từ khác trong mã của bạn. Điều này sẽ giúp bạn nhanh chóng xác định các từ khóa Python trong khi bạn lập trình để bạn không sử dụng chúng không chính xác.

Sử dụng mã trong một bản sao để kiểm tra các từ khóa

Trong Python Repl, có một số cách bạn có thể xác định các từ khóa Python hợp lệ và tìm hiểu thêm về chúng.

Bạn có thể nhận được một danh sách các từ khóa có sẵn bằng cách sử dụng

left if not left else right
2:

>>>

>>> help("keywords")

Here is a list of the Python keywords.  Enter any keyword to get more help.

False               class               from                or
None                continue            global              pass
True                def                 if                  raise
and                 del                 import              return
as                  elif                in                  try
assert              else                is                  while
async               except              lambda              with
await               finally             nonlocal            yield
break               for                 not

Tiếp theo, như được chỉ ra trong đầu ra ở trên, bạn có thể sử dụng một lần nữa bằng cách truyền trong từ khóa cụ thể mà bạn cần thêm thông tin về. Bạn có thể làm điều này, ví dụ, với từ khóa

left if not left else right
4:

>>>

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)

Tiếp theo, như được chỉ ra trong đầu ra ở trên, bạn có thể sử dụng một lần nữa bằng cách truyền trong từ khóa cụ thể mà bạn cần thêm thông tin về. Bạn có thể làm điều này, ví dụ, với từ khóa

left if not left else right
4:

  1. Python cũng cung cấp một mô -đun
    >>> def func():
    ...     print("hello")
    ...
    >>> x = func()
    hello
    >>> print(x)
    None
    
    5 để làm việc với các từ khóa Python theo cách lập trình. Mô -đun
    >>> def func():
    ...     print("hello")
    ...
    >>> x = func()
    hello
    >>> print(x)
    None
    
    5 trong Python cung cấp hai thành viên hữu ích để xử lý các từ khóa:
    provides a list of all the Python keywords for the version of Python you’re running.
  2. left if not left else right
    
    7 cung cấp một danh sách tất cả các từ khóa Python cho phiên bản Python mà bạn đang chạy.
    provides a handy way to determine if a string is also a keyword.

left if not left else right
8 cung cấp một cách tiện dụng để xác định xem một chuỗi cũng là từ khóa.

>>>

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35

Tiếp theo, như được chỉ ra trong đầu ra ở trên, bạn có thể sử dụng một lần nữa bằng cách truyền trong từ khóa cụ thể mà bạn cần thêm thông tin về. Bạn có thể làm điều này, ví dụ, với từ khóa

left if not left else right
4:

Python cũng cung cấp một mô -đun >>> def func(): ... print("hello") ... >>> x = func() hello >>> print(x) None 5 để làm việc với các từ khóa Python theo cách lập trình. Mô -đun >>> def func(): ... print("hello") ... >>> x = func() hello >>> print(x) None 5 trong Python cung cấp hai thành viên hữu ích để xử lý các từ khóa:

left if not left else right
7 cung cấp một danh sách tất cả các từ khóa Python cho phiên bản Python mà bạn đang chạy.

left if not left else right 8 cung cấp một cách tiện dụng để xác định xem một chuỗi cũng là từ khóa.

Để có được danh sách tất cả các từ khóa trong phiên bản Python mà bạn đang chạy và để nhanh chóng xác định có bao nhiêu từ khóa được xác định, hãy sử dụng

left if not left else right
9:

Nếu bạn cần biết thêm về một từ khóa hoặc cần làm việc với các từ khóa theo cách lập trình, thì Python cung cấp tài liệu và công cụ này cho bạn.

  • Sự trung thực đề cập đến việc đánh giá boolean về một giá trị. Sự thật của một giá trị cho thấy giá trị là sự thật hay giả. refers to the Boolean evaluation of a value. The truthiness of a value indicates whether the value is truthy or falsy.

  • Sự thật có nghĩa là bất kỳ giá trị nào đánh giá đúng trong bối cảnh Boolean. Để xác định xem một giá trị có phải là sự thật hay không, hãy truyền nó như là đối số cho

    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    2. Nếu nó trả về
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    3, thì giá trị là sự thật. Ví dụ về các giá trị sự thật là các chuỗi không trống, bất kỳ số nào aren
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    4, danh sách không trống và nhiều thứ khác.
    means any value that evaluates to true in the Boolean context. To determine if a value is truthy, pass it as the argument to
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    2. If it returns
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    3, then the value is truthy. Examples of truthy values are non-empty strings, any numbers that aren’t
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    4, non-empty lists, and many more.

  • Falsy có nghĩa là bất kỳ giá trị nào đánh giá sai trong bối cảnh Boolean. Để xác định xem một giá trị có phải là giả mạo hay không, hãy truyền nó làm đối số cho

    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    2. Nếu nó trả về
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    6, thì giá trị là giả. Ví dụ về các giá trị giả là
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    7,
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    4,
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    9,
    True if bool(<expr>) is False else False
    
    0 và
    True if bool(<expr>) is False else False
    
    1.
    means any value that evaluates to false in the Boolean context. To determine if a value is falsy, pass it as the argument to
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    2. If it returns
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    6, then the value is falsy. Examples of falsy values are
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    7,
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    4,
    >>> val = ""  # Truthiness value is `False`
    >>> not val
    True
    
    >>> val = 5  # Truthiness value is `True`
    >>> not val
    False
    
    9,
    True if bool(<expr>) is False else False
    
    0, and
    True if bool(<expr>) is False else False
    
    1.

Để biết thêm về các thuật ngữ và khái niệm này, hãy kiểm tra các nhà khai thác và biểu thức trong Python.

Giá trị Từ khóa: >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 3, >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 6, True if bool() is False else False 4

Có ba từ khóa Python được sử dụng làm giá trị. Các giá trị này là các giá trị singleton có thể được sử dụng nhiều lần và luôn tham chiếu cùng một đối tượng chính xác. Bạn rất có thể thấy và sử dụng các giá trị này rất nhiều.

Từ khóa
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 và
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6

Từ khóa

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 được sử dụng làm giá trị true boolean trong mã Python. Từ khóa Python
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 tương tự như từ khóa
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3, nhưng với giá trị boolean ngược lại của FALSE. Trong các ngôn ngữ lập trình khác, bạn sẽ thấy các từ khóa này được viết bằng chữ thường (
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
00 và
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
01), nhưng trong Python, chúng luôn được viết bằng chữ hoa.
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3
keyword is used as the Boolean true value in Python code. The Python keyword
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6
is similar to the
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 keyword, but with the opposite Boolean value of false. In other programming languages, you’ll see these keywords written in lowercase (
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
00 and
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
01), but in Python they are always written in uppercase.

Các từ khóa Python

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 và
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 có thể được gán cho các biến và so sánh với trực tiếp:

>>>

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True

Hầu hết các giá trị trong Python sẽ đánh giá đến

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2. Chỉ có một vài giá trị trong Python sẽ đánh giá thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
4,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
7,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
9 và
True if bool(<expr>) is False else False
0 để đặt tên cho một số. Chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2 cho thấy độ thật của giá trị, hoặc giá trị boolean tương đương. Bạn có thể so sánh độ thật của giá trị với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 bằng cách chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:

>>>

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True

Hầu hết các giá trị trong Python sẽ đánh giá đến

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2. Chỉ có một vài giá trị trong Python sẽ đánh giá thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
4,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
7,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
9 và
True if bool(<expr>) is False else False
0 để đặt tên cho một số. Chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2 cho thấy độ thật của giá trị, hoặc giá trị boolean tương đương. Bạn có thể so sánh độ thật của giá trị với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 bằng cách chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18
doesn’t work. You should directly compare a value to
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 or
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 only if you want to know whether it is actually the values
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 or
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6.

Lưu ý rằng việc so sánh giá trị sự thật trực tiếp với

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 bằng cách sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 không hoạt động. Bạn nên so sánh trực tiếp một giá trị với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 chỉ khi bạn muốn biết liệu đó có thực sự là các giá trị
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hay
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6.

>>>

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy

Hầu hết các giá trị trong Python sẽ đánh giá đến

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2. Chỉ có một vài giá trị trong Python sẽ đánh giá thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
4,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
7,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
9 và
True if bool(<expr>) is False else False
0 để đặt tên cho một số. Chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2 cho thấy độ thật của giá trị, hoặc giá trị boolean tương đương. Bạn có thể so sánh độ thật của giá trị với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 bằng cách chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:

Lưu ý rằng việc so sánh giá trị sự thật trực tiếp với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 bằng cách sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 không hoạt động. Bạn nên so sánh trực tiếp một giá trị với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 chỉ khi bạn muốn biết liệu đó có thực sự là các giá trị
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hay
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6.

Khi viết các tuyên bố có điều kiện dựa trên sự thật của một giá trị, bạn không nên so sánh trực tiếp với

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Bạn có thể dựa vào Python để kiểm tra sự thật trong các điều kiện cho bạn:
True if bool(<expr>) is False else False
4
represents no value. In other programming languages,
True if bool(<expr>) is False else False
4 is represented as
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
30,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
31,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
32,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
33, or
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
34.

Trong Python, bạn thường không cần phải chuyển đổi các giá trị để được rõ ràng

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Python sẽ ngầm xác định sự thật của giá trị cho bạn.

>>>

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None

Hầu hết các giá trị trong Python sẽ đánh giá đến

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2. Chỉ có một vài giá trị trong Python sẽ đánh giá thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 khi được chuyển đến
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
4,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
7,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
9 và
True if bool(<expr>) is False else False
0 để đặt tên cho một số. Chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2 cho thấy độ thật của giá trị, hoặc giá trị boolean tương đương. Bạn có thể so sánh độ thật của giá trị với
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 bằng cách chuyển giá trị cho
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
2:

Lưu ý rằng việc so sánh giá trị sự thật trực tiếp với >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 3 hoặc >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 6 bằng cách sử dụng >>> help("pass") The "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) 18 không hoạt động. Bạn nên so sánh trực tiếp một giá trị với >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 3 hoặc >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 6 chỉ khi bạn muốn biết liệu đó có thực sự là các giá trị >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 3 hay >>> val = "" # Truthiness value is `False` >>> not val True >>> val = 5 # Truthiness value is `True` >>> not val False 6.

Khi viết các tuyên bố có điều kiện dựa trên sự thật của một giá trị, bạn không nên so sánh trực tiếp với

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Bạn có thể dựa vào Python để kiểm tra sự thật trong các điều kiện cho bạn:

Trong Python, bạn thường không cần phải chuyển đổi các giá trị để được rõ ràng
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Python sẽ ngầm xác định sự thật của giá trị cho bạn.
Từ khóa
True if bool(<expr>) is False else False
4
Từ khóa Python
True if bool(<expr>) is False else False
4 đại diện cho không có giá trị. Trong các ngôn ngữ lập trình khác,
True if bool(<expr>) is False else False
4 được biểu thị là
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
30,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
31,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
32,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
33 hoặc
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
34.
True if bool(<expr>) is False else False
4 cũng là giá trị mặc định được trả về bởi một hàm nếu nó không có câu lệnh
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36:
Để đi sâu hơn về từ khóa Python rất quan trọng và hữu ích này, hãy xem NULL trong Python: Hiểu đối tượng Python tựa Nonetype.Từ khóa của nhà điều hành:
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18
Một số từ khóa Python được sử dụng làm toán tử. Trong các ngôn ngữ lập trình khác, các toán tử này sử dụng các biểu tượng như
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
42,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
43 và
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
44. Các toán tử Python cho đây đều là tất cả các từ khóa:
Toán học toánNhững ngôn ngữ khác
Từ khóa PythonVà, ∧
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
45
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37
Hoặc, ∨
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
47
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38
Không, ¬

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
44

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39

Chứa,

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37 is used to determine if both the left and right operands are truthy or falsy. If both operands are truthy, then the result will be truthy. If one is falsy, then the result will be falsy:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40

XÁC THỰC

left if not left else right

Tuyên bố trên sẽ tạo ra kết quả tương tự như

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
70.

Bởi vì

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37 trả về toán hạng đầu tiên nếu nó Falsy và nếu không thì trả về toán hạng cuối cùng, bạn cũng có thể sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37 trong một bài tập:

Nếu y giả, thì điều này sẽ dẫn đến

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
73 được gán giá trị
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
74. Mặt khác,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
73 sẽ được gán giá trị của
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
76. Tuy nhiên, điều này làm cho mã khó hiểu. Một sự thay thế rõ ràng hơn và rõ ràng sẽ là:

Mã này dài hơn, nhưng nó chỉ rõ hơn những gì bạn đang cố gắng thực hiện.

Từ khóa
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38

Từ khóa Python từ

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38 được sử dụng để xác định xem ít nhất một trong các toán hạng là sự thật. Tuyên bố
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38 trả về toán hạng đầu tiên nếu đó là sự thật và nếu không thì trả về toán hạng thứ hai:
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38
keyword is used to determine if at least one of the operands is truthy. An
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38 statement returns the first operand if it is truthy and otherwise returns the second operand:

Giống như từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38 không chuyển đổi các toán hạng của nó thành các giá trị boolean của họ. Thay vào đó, nó dựa vào sự thật của họ để xác định kết quả.

Nếu bạn muốn viết một cái gì đó như biểu thức

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38 mà không cần sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38, thì bạn có thể làm như vậy với biểu thức ternary:

Biểu thức này sẽ tạo ra kết quả tương tự như

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
84. Để tận dụng hành vi này, đôi khi bạn cũng sẽ thấy
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38 được sử dụng trong các bài tập. Điều này thường không được khuyến khích ủng hộ một nhiệm vụ rõ ràng hơn.

Để có cái nhìn sâu hơn về

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38, bạn có thể đọc về cách sử dụng toán tử Python
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38.

Từ khóa
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39

Từ khóa Python từ

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39 được sử dụng để có được giá trị boolean ngược lại của một biến:
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39
keyword is used to get the opposite Boolean value of a variable:

>>>

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False

Từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39 được sử dụng trong các câu lệnh có điều kiện hoặc các biểu thức boolean khác để lật ý nghĩa hoặc kết quả của boolean. Không giống như
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37 và
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39 sẽ xác định giá trị boolean rõ ràng,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6, sau đó trả lại ngược lại.
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37
and
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38
,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39
will determine the explicit Boolean value,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 or
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6, and then return the opposite.

Nếu bạn muốn có được hành vi tương tự mà không cần sử dụng

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39, thì bạn có thể làm như vậy với biểu thức ternary sau:

True if bool(<expr>) is False else False

Tuyên bố này sẽ trả về kết quả tương tự như

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
97.

Từ khóa
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40

Từ khóa Python từ

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 là một kiểm tra ngăn chặn mạnh mẽ hoặc toán tử thành viên. Cho một phần tử để tìm và một container hoặc chuỗi để tìm kiếm,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 sẽ trả về
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 cho biết liệu phần tử có được tìm thấy trong container hay không:
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40
keyword is a powerful containment check, or membership operator. Given an element to find and a container or sequence to search,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 will return
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 or
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 indicating whether the element was found in the container:

Một ví dụ tốt về việc sử dụng từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 đang kiểm tra một chữ cái cụ thể trong một chuỗi:

>>>

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
0

Từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39 được sử dụng trong các câu lệnh có điều kiện hoặc các biểu thức boolean khác để lật ý nghĩa hoặc kết quả của boolean. Không giống như
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
37 và
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
38,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39 sẽ xác định giá trị boolean rõ ràng,
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6, sau đó trả lại ngược lại.

Nếu bạn muốn có được hành vi tương tự mà không cần sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
39, thì bạn có thể làm như vậy với biểu thức ternary sau:

Tuyên bố này sẽ trả về kết quả tương tự như

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
97.
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18
keyword is an identity check. This is different from the
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
08 operator, which checks for equality. Sometimes two things can be considered equal but not be the exact same object in memory. The
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 keyword determines whether two objects are exactly the same object:

Từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40

Từ khóa Python từ

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 là một kiểm tra ngăn chặn mạnh mẽ hoặc toán tử thành viên. Cho một phần tử để tìm và một container hoặc chuỗi để tìm kiếm,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 sẽ trả về
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hoặc
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6 cho biết liệu phần tử có được tìm thấy trong container hay không:

Một ví dụ tốt về việc sử dụng từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 đang kiểm tra một chữ cái cụ thể trong một chuỗi:

Từ khóa >>> help("pass") The "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) 40 hoạt động với tất cả các loại container: danh sách, dicts, bộ, chuỗi và bất cứ thứ gì khác xác định >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 05 hoặc có thể được lặp lại.

Từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18

Từ khóa Python từ
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 là một kiểm tra nhận dạng. Điều này khác với toán tử
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
08, kiểm tra sự bình đẳng. Đôi khi hai điều có thể được coi là bằng nhau nhưng không phải là cùng một đối tượng trong bộ nhớ. Từ khóa
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 xác định xem hai đối tượng có chính xác cùng một đối tượng hay không:

Điều này sẽ trả về

>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 nếu
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
11 là cùng một đối tượng chính xác trong bộ nhớ là
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
12, nếu không nó sẽ trả về
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6.
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67
keyword is used to start a conditional statement. An
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 statement allows you to write a block of code that gets executed only if the expression after
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 is truthy.

Hầu hết thời gian bạn sẽ thấy

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 được sử dụng để kiểm tra xem một đối tượng có phải là
True if bool(<expr>) is False else False
4 không. Vì
True if bool(<expr>) is False else False
4 là một singleton, chỉ có một trường hợp của
True if bool(<expr>) is False else False
4 có thể tồn tại, do đó, tất cả các giá trị
True if bool(<expr>) is False else False
4 là cùng một đối tượng chính xác trong bộ nhớ.

Nếu các khái niệm này là mới đối với bạn, thì bạn có thể nhận được lời giải thích sâu hơn bằng cách kiểm tra Python ‘! Để tìm hiểu sâu hơn về cách

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
18 hoạt động, hãy xem các nhà khai thác và biểu thức trong Python.

Kiểm soát luồng từ khóa:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67,
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21,
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
1

Ba từ khóa Python được sử dụng cho luồng điều khiển:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67,
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21 và
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22. Các từ khóa Python này cho phép bạn sử dụng logic có điều kiện và thực thi mã được đưa ra một số điều kiện nhất định. Các từ khóa này rất phổ biến, chúng sẽ được sử dụng trong hầu hết các chương trình bạn thấy hoặc viết bằng Python.

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
2

Nếu các biểu thức của bạn là các câu lệnh không biến chứng, thì sử dụng biểu thức ternary cung cấp một cách tốt đẹp để đơn giản hóa mã của bạn một chút. Khi các điều kiện trở nên phức tạp, nó thường tốt hơn để dựa vào tuyên bố

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 tiêu chuẩn.

Từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21

Tuyên bố

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21 trông và chức năng như câu lệnh
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67, với hai khác biệt chính:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21
statement looks and functions like the
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 statement, with two major differences:

  1. Sử dụng
    >>> import keyword
    >>> keyword.kwlist
    ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
    >>> len(keyword.kwlist)
    35
    
    21 chỉ hợp lệ sau tuyên bố
    >>> help("pass")
    The "pass" statement
    ********************
    
       pass_stmt ::= "pass"
    
    "pass" is a null operation — when it is executed, nothing happens. It
    is useful as a placeholder when a statement is required syntactically,
    but no code needs to be executed, for example:
    
       def f(arg): pass    # a function that does nothing (yet)
    
       class C: pass       # a class with no methods (yet)
    
    67 hoặc một
    >>> import keyword
    >>> keyword.kwlist
    ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
    >>> len(keyword.kwlist)
    35
    
    21 khác.
  2. Bạn có thể sử dụng nhiều câu lệnh
    >>> import keyword
    >>> keyword.kwlist
    ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
    >>> len(keyword.kwlist)
    35
    
    21 như bạn cần.

Trong các ngôn ngữ lập trình khác,

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21 là
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
45 (hai từ riêng biệt) hoặc
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
46 (cả hai từ được nghiền với nhau). Khi bạn nhìn thấy
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21 trong Python, hãy nghĩ
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
45:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
3

Python không có tuyên bố

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
49. Một cách để có được chức năng tương tự mà các ngôn ngữ lập trình khác cung cấp các câu lệnh
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
49 là bằng cách sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 và
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21. Đối với các cách khác để sao chép tuyên bố
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
49 trong Python, hãy xem các câu lệnh chuyển đổi/trường hợp mô phỏng trong Python.

Từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22

Câu lệnh

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22, kết hợp với các từ khóa Python
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 và
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21, biểu thị một khối mã chỉ được thực thi nếu các khối có điều kiện khác,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 và
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21, đều là giả mạo:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22
statement, in conjunction with the Python keywords
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 and
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21, denotes a block of code that should be executed only if the other conditional blocks,
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 and
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21, are all falsy:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
4

Lưu ý rằng câu lệnh

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 không có biểu thức có điều kiện. Kiến thức về từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
21 và
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 và việc sử dụng đúng của chúng là rất quan trọng đối với các lập trình viên Python. Cùng với
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67, chúng tạo thành một số thành phần được sử dụng thường xuyên nhất trong bất kỳ chương trình Python nào.

Từ khóa lặp lại: >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 64, >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 65, >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 66, >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 67, >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 22

Vòng lặp và lặp là các khái niệm lập trình cực kỳ quan trọng. Một số từ khóa Python được sử dụng để tạo và làm việc với các vòng lặp. Chúng, giống như các từ khóa Python được sử dụng cho các điều kiện ở trên, sẽ được sử dụng và nhìn thấy trong mọi chương trình Python bạn gặp. Hiểu chúng và cách sử dụng đúng đắn của họ sẽ giúp bạn cải thiện như một lập trình viên Python.

Từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64

Vòng lặp phổ biến nhất trong Python là vòng

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64. Nó được xây dựng bằng cách kết hợp các từ khóa Python
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 và
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 đã giải thích trước đó. Cú pháp cơ bản cho vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 như sau:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64
and
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40
explained earlier. The basic syntax for a
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 loop is as follows:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
5

Một ví dụ phổ biến là lặp qua các số một đến năm và in chúng ra màn hình:

>>>

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
6

Trong các ngôn ngữ lập trình khác, cú pháp cho vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 sẽ trông hơi khác một chút. Bạn thường cần chỉ định biến, điều kiện để tiếp tục và cách tăng biến đó (
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
75).

Trong Python, vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 giống như một vòng lặp lại trong các ngôn ngữ lập trình khác. Đưa đối tượng để lặp lại, nó gán giá trị của từng lần lặp cho biến:

>>>

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
7

Trong các ngôn ngữ lập trình khác, cú pháp cho vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 sẽ trông hơi khác một chút. Bạn thường cần chỉ định biến, điều kiện để tiếp tục và cách tăng biến đó (
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
75).

Trong Python, vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 giống như một vòng lặp lại trong các ngôn ngữ lập trình khác. Đưa đối tượng để lặp lại, nó gán giá trị của từng lần lặp cho biến:

Trong ví dụ này, bạn bắt đầu với danh sách (container) của những người tên. Vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 bắt đầu với từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 ở đầu dòng, theo sau là biến để gán từng phần tử của danh sách, sau đó là từ khóa
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 và cuối cùng là container (
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
80).

Python sườn

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 Loop là một thành phần chính khác trong bất kỳ chương trình Python nào. Để tìm hiểu thêm về các vòng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64, hãy xem Python Hồi cho các vòng lặp (lặp lại xác định).
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65
and works like a
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 loop in other programming languages. As long as the condition that follows the
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 keyword is truthy, the block following the
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 statement will continue to be executed over and over again:

Từ khóa

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65

Vòng lặp Python từ

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 sử dụng từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 và hoạt động giống như vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 trong các ngôn ngữ lập trình khác. Miễn là điều kiện theo sau từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 là sự thật, khối theo câu lệnh
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 sẽ tiếp tục được thực thi nhiều lần:

>>>

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
9

Trong các ngôn ngữ lập trình khác, cú pháp cho vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 sẽ trông hơi khác một chút. Bạn thường cần chỉ định biến, điều kiện để tiếp tục và cách tăng biến đó (
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
75).

Trong Python, vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 giống như một vòng lặp lại trong các ngôn ngữ lập trình khác. Đưa đối tượng để lặp lại, nó gán giá trị của từng lần lặp cho biến:

Trong ví dụ này, bạn bắt đầu với danh sách (container) của những người tên. Vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 bắt đầu với từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 ở đầu dòng, theo sau là biến để gán từng phần tử của danh sách, sau đó là từ khóa
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
40 và cuối cùng là container (
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
80).
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66
keyword. This keyword will work in both
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 and
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 loops:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
0

Python sườn

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 Loop là một thành phần chính khác trong bất kỳ chương trình Python nào. Để tìm hiểu thêm về các vòng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64, hãy xem Python Hồi cho các vòng lặp (lặp lại xác định).

>>>

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
1

Trong các ngôn ngữ lập trình khác, cú pháp cho vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 sẽ trông hơi khác một chút. Bạn thường cần chỉ định biến, điều kiện để tiếp tục và cách tăng biến đó (
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
75).

Từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67

Python cũng có từ khóa

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67 khi bạn muốn bỏ qua vòng lặp vòng tiếp theo. Giống như trong hầu hết các ngôn ngữ lập trình khác, từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67 cho phép bạn ngừng thực hiện lặp lại vòng lặp hiện tại và chuyển sang lần lặp tiếp theo:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67
keyword for when you want to skip to the next loop iteration. Like in most other programming languages, the
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67 keyword allows you to stop executing the current loop iteration and move on to the next iteration:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
2

Từ khóa

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67 cũng hoạt động trong các vòng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65. Nếu từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
67 đạt được trong một vòng lặp, thì lần lặp hiện tại bị dừng và lần lặp tiếp theo của vòng lặp được bắt đầu.

Từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 được sử dụng với các vòng lặp

Ngoài việc sử dụng từ khóa

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 với các câu lệnh
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 có điều kiện, bạn cũng có thể sử dụng nó như một phần của vòng lặp. Khi được sử dụng với một vòng lặp, từ khóa
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 chỉ định mã sẽ được chạy nếu vòng lặp thoát ra bình thường, có nghĩa là
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66 không được gọi để thoát khỏi vòng lặp sớm.
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22
keyword specifies code that should be run if the loop exits normally, meaning
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66 was not called to exit the loop early.

Cú pháp để sử dụng

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 với vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 trông giống như sau:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
3

Điều này rất giống với việc sử dụng

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 với câu lệnh
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67. Sử dụng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 với vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
65 trông giống nhau:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
4

Tài liệu tiêu chuẩn Python có một phần về việc sử dụng

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66 và
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 với vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 mà bạn thực sự nên kiểm tra. Nó sử dụng một ví dụ tuyệt vời để minh họa tính hữu ích của khối
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22.

Nhiệm vụ mà nó hiển thị là lặp qua các số từ hai đến chín để tìm các số nguyên tố. Một cách bạn có thể làm điều này là với vòng lặp

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 tiêu chuẩn với biến cờ:flag variable:

>>>

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
5

Bạn có thể sử dụng cờ

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 để cho biết cách thoát vòng. Nếu nó thoát ra bình thường, thì cờ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 vẫn ở mức
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3. Nếu nó thoát ra với
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66, thì cờ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 sẽ được đặt thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Khi ở ngoài vòng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 bên trong, bạn có thể kiểm tra cờ để xác định xem
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 có phải là
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hay không và nếu vậy, in rằng số đó là số nguyên tố.

Khối

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 cung cấp cú pháp đơn giản hơn. Nếu bạn thấy mình phải đặt cờ trong một vòng lặp, thì hãy xem ví dụ tiếp theo như một cách để có khả năng đơn giản hóa mã của bạn:

>>>

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
6

Bạn có thể sử dụng cờ

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 để cho biết cách thoát vòng. Nếu nó thoát ra bình thường, thì cờ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 vẫn ở mức
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3. Nếu nó thoát ra với
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66, thì cờ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 sẽ được đặt thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Khi ở ngoài vòng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 bên trong, bạn có thể kiểm tra cờ để xác định xem
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 có phải là
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hay không và nếu vậy, in rằng số đó là số nguyên tố.

Khối

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 cung cấp cú pháp đơn giản hơn. Nếu bạn thấy mình phải đặt cờ trong một vòng lặp, thì hãy xem ví dụ tiếp theo như một cách để có khả năng đơn giản hóa mã của bạn:

Điều duy nhất bạn cần làm để sử dụng khối >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 22 trong ví dụ này là tháo cờ >>> x = True >>> x is True True >>> y = False >>> y is False True 21 và thay thế câu lệnh >>> help("pass") The "pass" statement ******************** pass_stmt ::= "pass" "pass" is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed, for example: def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods (yet) 67 cuối cùng bằng khối >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 22. Điều này cuối cùng tạo ra kết quả tương tự như ví dụ trước đây, chỉ với mã rõ ràng hơn.

Đôi khi sử dụng từ khóa

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 với một vòng lặp có vẻ hơi lạ, nhưng một khi bạn hiểu rằng nó cho phép bạn tránh sử dụng cờ trong vòng lặp của mình, nó có thể là một công cụ mạnh mẽ.

Cấu trúc Từ khóa:
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36,
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
37,
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38,
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39,
left if not left else right
4,
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41

Để xác định các chức năng và các lớp hoặc sử dụng các trình quản lý ngữ cảnh, bạn sẽ cần sử dụng một trong các từ khóa Python trong phần này. Họ là một phần thiết yếu của ngôn ngữ Python và sự hiểu biết khi nào nên sử dụng chúng sẽ giúp bạn trở thành một lập trình viên Python tốt hơn.

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36 is used to define a function or method of a class. This is equivalent to
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
44 in JavaScript and PHP. The basic syntax for defining a function with
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36 looks like this:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
7

Từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36

Từ khóa Python từ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36 được sử dụng để xác định hàm hoặc phương thức của một lớp. Điều này tương đương với
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
44 trong JavaScript và PHP. Cú pháp cơ bản để xác định chức năng với
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36 trông như thế này:

Các chức năng và phương pháp có thể là các cấu trúc rất hữu ích trong bất kỳ chương trình Python nào. Để tìm hiểu thêm về việc xác định chúng và tất cả các trong và ngoài của chúng, hãy xem xác định chức năng Python của riêng bạn.

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
37 is as follows:

Từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
37

Để xác định một lớp trong Python, bạn sử dụng từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
37. Cú pháp chung để xác định một lớp có
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
37 như sau:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
8

Các lớp là các công cụ mạnh mẽ trong lập trình hướng đối tượng và bạn nên biết về chúng và cách xác định chúng. Để tìm hiểu thêm, hãy xem chương trình hướng đối tượng (OOP) trong Python 3.

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38 keyword:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
9

Từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38

Các nhà quản lý bối cảnh là một cấu trúc thực sự hữu ích trong Python. Mỗi Trình quản lý bối cảnh thực hiện mã cụ thể trước và sau các câu lệnh bạn chỉ định. Để sử dụng một, bạn sử dụng từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38:

>>>

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
0

Bạn có thể sử dụng cờ

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 để cho biết cách thoát vòng. Nếu nó thoát ra bình thường, thì cờ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 vẫn ở mức
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3. Nếu nó thoát ra với
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
66, thì cờ
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 sẽ được đặt thành
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
6. Khi ở ngoài vòng
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64 bên trong, bạn có thể kiểm tra cờ để xác định xem
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
21 có phải là
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
3 hay không và nếu vậy, in rằng số đó là số nguyên tố.
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38
keyword opens the file for reading, assigns the open file pointer to
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
55, then executes whatever code you specify in the
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38 block. Then, after the block is executed, the file pointer closes. Even if your code in the
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38 block raises an exception, the file pointer would still close.

Để biết ví dụ tuyệt vời về việc sử dụng

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38 và người quản lý bối cảnh, hãy xem các hàm của Python Timer: ba cách để theo dõi mã của bạn.

Từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39 được sử dụng với
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38

Nếu bạn muốn truy cập vào kết quả của biểu thức hoặc trình quản lý ngữ cảnh được chuyển sang

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38, bạn sẽ cần phải bí danh bằng cách sử dụng
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39. Bạn cũng có thể đã thấy
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39 được sử dụng để nhập khẩu bí danh và ngoại lệ, và điều này không khác. Bí danh có sẵn trong khối
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38:
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39
. You may have also seen
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39 used to alias imports and exceptions, and this is no different. The alias is available in the
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38 block:

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
1

Hầu hết thời gian, bạn sẽ thấy hai từ khóa Python này,

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
38 và
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39, được sử dụng cùng nhau.

Từ khóa
left if not left else right
4

Vì Python không có các chỉ số khối để chỉ định kết thúc của một khối, từ khóa

left if not left else right
4 được sử dụng để chỉ định rằng khối bị bỏ trống một cách cố ý. Nó tương đương với không có hoạt động, hoặc không có hoạt động. Dưới đây là một vài ví dụ về việc sử dụng
left if not left else right
4 để chỉ định rằng khối trống:
left if not left else right
4
keyword is used to specify that the block is intentionally left blank. It’s the equivalent of a no-op, or no operation. Here are a few examples of using
left if not left else right
4 to specify that the block is blank:

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
2

Để biết thêm về

left if not left else right
4, hãy xem câu lệnh PASS: Làm thế nào để không làm gì trong Python.

Từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41

Từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 được sử dụng để xác định một hàm không có tên và chỉ có một câu lệnh, kết quả được trả về. Các chức năng được xác định với
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 được gọi là các hàm Lambda:
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41
keyword is used to define a function that doesn’t have a name and has only one statement, the results of which are returned. Functions defined with
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 are referred to as lambda functions:

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
3

Một ví dụ cơ bản về hàm

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 tính toán đối số được nâng lên với sức mạnh của
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
75 sẽ trông như thế này:

Điều này tương đương với việc xác định chức năng với

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36:

Một cách sử dụng phổ biến cho hàm

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 là chỉ định một hành vi khác cho chức năng khác. Ví dụ, hãy tưởng tượng bạn muốn sắp xếp một danh sách các chuỗi theo các giá trị số nguyên của chúng. Hành vi mặc định của
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
78 sẽ sắp xếp các chuỗi theo thứ tự bảng chữ cái. Nhưng với
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
78, bạn có thể chỉ định danh sách nào nên được sắp xếp.

Chức năng Lambda cung cấp một cách tốt đẹp để làm như vậy:

>>>

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
4

Ví dụ này sắp xếp danh sách không dựa trên thứ tự bảng chữ cái mà theo thứ tự số của các ký tự cuối cùng của các chuỗi sau khi chuyển đổi chúng thành số nguyên. Không có

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, bạn sẽ phải xác định một hàm, đặt tên cho nó và sau đó chuyển nó cho
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
78.
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 làm cho mã này sạch hơn.

Để so sánh, đây là những gì ví dụ trên sẽ trông như thế nào nếu không sử dụng

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41:

>>>

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
5

Ví dụ này sắp xếp danh sách không dựa trên thứ tự bảng chữ cái mà theo thứ tự số của các ký tự cuối cùng của các chuỗi sau khi chuyển đổi chúng thành số nguyên. Không có

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, bạn sẽ phải xác định một hàm, đặt tên cho nó và sau đó chuyển nó cho
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
78.
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 làm cho mã này sạch hơn.

Để so sánh, đây là những gì ví dụ trên sẽ trông như thế nào nếu không sử dụng

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41:

Mã này tạo ra kết quả tương tự như ví dụ >>> x = True >>> x is True True >>> y = False >>> y is False True 41, nhưng bạn cần xác định chức năng trước khi sử dụng.

Để biết thêm thông tin về

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, hãy xem cách sử dụng các hàm Python Lambda.

Trả về từ khóa:
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36,
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87

Có hai từ khóa Python được sử dụng để chỉ định những gì được trả về từ các chức năng hoặc phương thức:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36 và
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87. Hiểu khi nào và ở đâu để sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36 là rất quan trọng để trở thành một lập trình viên Python tốt hơn. Từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87 là một tính năng nâng cao hơn của Python, nhưng nó cũng có thể là một công cụ hữu ích để hiểu.
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36
keyword is valid only as part of a function defined with
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36. When Python encounters this keyword, it will exit the function at that point and return the results of whatever comes after the
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36 keyword:

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
6

Từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36

>>>

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
7

Ví dụ này sắp xếp danh sách không dựa trên thứ tự bảng chữ cái mà theo thứ tự số của các ký tự cuối cùng của các chuỗi sau khi chuyển đổi chúng thành số nguyên. Không có

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, bạn sẽ phải xác định một hàm, đặt tên cho nó và sau đó chuyển nó cho
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
78.
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 làm cho mã này sạch hơn.

>>>

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
8

Ví dụ này sắp xếp danh sách không dựa trên thứ tự bảng chữ cái mà theo thứ tự số của các ký tự cuối cùng của các chuỗi sau khi chuyển đổi chúng thành số nguyên. Không có

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, bạn sẽ phải xác định một hàm, đặt tên cho nó và sau đó chuyển nó cho
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
78.
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41 làm cho mã này sạch hơn.

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
9

Để so sánh, đây là những gì ví dụ trên sẽ trông như thế nào nếu không sử dụng

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41:

Mã này tạo ra kết quả tương tự như ví dụ

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, nhưng bạn cần xác định chức năng trước khi sử dụng.

Để biết thêm thông tin về
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
41, hãy xem cách sử dụng các hàm Python Lambda.

Trả về từ khóa:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36,
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87
keyword is kind of like the
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36 keyword in that it specifies what gets returned from a function. However, when a function has a
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87 statement, what gets returned is a generator. The generator can then be passed to Python’s built-in
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
05 to get the next value returned from the function.

Có hai từ khóa Python được sử dụng để chỉ định những gì được trả về từ các chức năng hoặc phương thức:

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36 và
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87. Hiểu khi nào và ở đâu để sử dụng
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36 là rất quan trọng để trở thành một lập trình viên Python tốt hơn. Từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87 là một tính năng nâng cao hơn của Python, nhưng nó cũng có thể là một công cụ hữu ích để hiểu.

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
0

Từ khóa

>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
36

>>>

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
1

Khi ngoại lệ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
08 được nâng lên, trình tạo hoàn thành các giá trị trả về. Để đi qua các tên một lần nữa, bạn sẽ cần phải gọi
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
09 một lần nữa và lấy một trình tạo mới. Hầu hết thời gian, một hàm máy phát sẽ được gọi là một phần của vòng lặp
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
64, trong đó
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
05 gọi cho bạn.

Để biết thêm về từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
87 và sử dụng các hàm tạo và chức năng máy phát, hãy xem cách sử dụng trình tạo và mang lại trong các trình tạo Python và Python 101.

Nhập từ khóa: >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 13, >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 14, >>> x = True >>> x is True True >>> y = False >>> y is False True 39

Đối với những công cụ đó, không giống như các từ khóa và tích hợp Python, chưa có sẵn cho chương trình Python của bạn, bạn sẽ cần nhập chúng vào chương trình của mình. Có nhiều mô -đun hữu ích có sẵn trong thư viện tiêu chuẩn Python, chỉ nhập khẩu. Ngoài ra còn có nhiều thư viện và công cụ hữu ích khác có sẵn trong PYPI, một khi bạn đã cài đặt chúng vào môi trường của mình, bạn sẽ cần nhập vào các chương trình của mình.

Sau đây là các mô tả ngắn gọn về ba từ khóa Python được sử dụng để nhập các mô -đun vào chương trình của bạn. Để biết thêm thông tin về các từ khóa này, hãy xem các mô -đun và gói Python - Giới thiệu và Nhập Python: Kỹ thuật và Mẹo nâng cao.

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13

Từ khóa Python từ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13 được sử dụng để nhập hoặc bao gồm một mô -đun để sử dụng trong chương trình Python của bạn. Cú pháp sử dụng cơ bản trông như thế này:

Sau khi tuyên bố đó chạy,

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
18 sẽ có sẵn cho chương trình của bạn.

Ví dụ: nếu bạn muốn sử dụng lớp

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 từ mô -đun
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
20 trong thư viện tiêu chuẩn, thì bạn có thể sử dụng mã sau:

>>>

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
2

Nhập

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
20 theo cách này tạo ra toàn bộ mô -đun
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
20, bao gồm cả lớp
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19, có sẵn cho chương trình của bạn. Bằng cách sử dụng tên mô -đun, bạn có quyền truy cập vào tất cả các công cụ có sẵn trong mô -đun đó. Để có quyền truy cập vào
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19, bạn tham khảo nó từ mô -đun:
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
25.

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
14

Từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
14 được sử dụng cùng với
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13 để nhập một cái gì đó cụ thể từ một mô -đun:
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
14
keyword is used together with
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13 to import something specific from a module:

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
3

Điều này sẽ nhập bất cứ thứ gì

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
29 nằm trong
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
18 để được sử dụng trong chương trình của bạn. Hai từ khóa Python này,
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
14 và
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13, được sử dụng cùng nhau.

Nếu bạn muốn sử dụng

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 từ mô -đun
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
20 trong thư viện tiêu chuẩn, thì bạn có thể nhập cụ thể:

>>>

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
4

Nhập

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 như thế này làm cho lớp
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 có sẵn, nhưng không có gì khác từ mô -đun
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
20 có sẵn.
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 hiện có sẵn mà không cần bạn phải tham khảo nó từ mô -đun
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
20.

Từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39

Từ khóa

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39 được sử dụng để bí danh một mô -đun hoặc công cụ đã nhập. Nó được sử dụng cùng với các từ khóa Python
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13 và
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
14 để thay đổi tên của thứ đang được nhập:
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39
keyword is used to alias an imported module or tool. It’s used together with the Python keywords
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
13 and
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
14 to change the name of the thing being imported:

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
5

Đối với các mô -đun có tên thực sự dài hoặc bí danh nhập khẩu thường được sử dụng,

>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39 có thể hữu ích trong việc tạo bí danh.

Nếu bạn muốn nhập lớp

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 từ mô -đun bộ sưu tập nhưng đặt tên cho nó là một cái gì đó khác biệt, bạn có thể bí danh bằng cách sử dụng
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39:

>>>

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
6

Bây giờ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
19 có sẵn để được sử dụng trong chương trình của bạn, nhưng nó được tham chiếu bởi
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
48 thay thế. Một cách sử dụng phổ biến hơn của
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39 Bí danh nhập khẩu là với các gói Numpy hoặc Pandas. Chúng thường được nhập khẩu bí danh tiêu chuẩn:

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
7

Đây là một giải pháp thay thế tốt hơn để chỉ nhập mọi thứ từ một mô -đun và nó cho phép bạn rút ngắn tên của mô -đun đang được nhập.

Từ khóa xử lý ngoại lệ: >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 50, >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 51, >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 52, >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 53, >>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'async', ... >>> len(keyword.kwlist) 35 22, >>> x = "this is a truthy value" >>> x is True False >>> bool(x) is True True >>> y = "" # This is falsy >>> y is False False >>> bool(y) is False True 55

Một trong những khía cạnh phổ biến nhất của bất kỳ chương trình Python nào là việc nâng cao và bắt các trường hợp ngoại lệ. Bởi vì đây là một khía cạnh cơ bản của tất cả các mã Python, có một số từ khóa Python có sẵn để giúp làm cho phần này của mã của bạn rõ ràng và súc tích.

Các phần dưới đây đi qua các từ khóa Python này và cách sử dụng cơ bản của chúng. Đối với một hướng dẫn chuyên sâu hơn về các từ khóa này, hãy xem các ngoại lệ của Python: Giới thiệu.

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50

Bất kỳ khối xử lý ngoại lệ nào cũng bắt đầu với từ khóa Python từ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50. Điều này giống nhau trong hầu hết các ngôn ngữ lập trình khác có xử lý ngoại lệ.
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50
keyword. This is the same in most other programming languages that have exception handling.

Mã trong khối

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 là mã có thể tăng ngoại lệ. Một số từ khóa Python khác được liên kết với
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 và được sử dụng để xác định những gì nên làm nếu các trường hợp ngoại lệ khác nhau được nêu ra hoặc trong các tình huống khác nhau. Đây là
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51,
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 và
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53:

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
8

Một khối

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 không hợp lệ trừ khi nó có ít nhất một trong các từ khóa Python khác được sử dụng để xử lý ngoại lệ như là một phần của câu lệnh
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 tổng thể.

Nếu bạn muốn tính toán và trả lại dặm cho mỗi gallon khí (

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
65) được điều khiển dặm và gallon khí được sử dụng, thì bạn có thể viết một chức năng như sau:

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
9

Vấn đề đầu tiên bạn có thể thấy là mã của bạn có thể tăng

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
66 nếu tham số
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
67 được truyền dưới dạng
>>> val = ""  # Truthiness value is `False`
>>> not val
True

>>> val = 5  # Truthiness value is `True`
>>> not val
False
4. Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 cho phép bạn sửa đổi mã ở trên để xử lý tình huống đó một cách thích hợp:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
0

Bây giờ nếu

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
70, thì
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 đã giành được một ngoại lệ và sẽ trở lại
True if bool(<expr>) is False else False
4 thay thế. Điều này có thể tốt hơn, hoặc bạn có thể quyết định rằng bạn muốn nâng cao một loại ngoại lệ khác hoặc xử lý tình huống này một cách khác nhau. Bạn sẽ thấy một phiên bản mở rộng của ví dụ này dưới đây để minh họa các từ khóa khác được sử dụng để xử lý ngoại lệ.

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51

Từ khóa Python từ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 được sử dụng với
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 để xác định những việc cần làm khi các ngoại lệ cụ thể được nêu ra. Bạn có thể có một hoặc nhiều khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 với một
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50. Việc sử dụng cơ bản trông như thế này:
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51
keyword is used with
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 to define what to do when specific exceptions are raised. You can have one or more
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 blocks with a single
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50. The basic usage looks like this:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
1

Lấy ví dụ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 từ trước, bạn cũng có thể làm điều gì đó cụ thể trong trường hợp ai đó vượt qua các loại đã giành được công việc với nhà điều hành
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
79. Đã xác định
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 trong một ví dụ trước, bây giờ hãy thử gọi nó bằng các chuỗi thay vì số:

>>>

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
2

Bạn có thể sửa đổi

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 và sử dụng nhiều khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 để xử lý tình huống này:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
3

Tại đây, bạn sửa đổi

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 để tăng ngoại lệ
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
84 chỉ sau khi bạn đã in một lời nhắc hữu ích lên màn hình.

Lưu ý rằng từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 cũng có thể được sử dụng cùng với từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39. Điều này có tác dụng tương tự như các cách sử dụng khác của
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39, mang lại cho ngoại lệ được nâng cao một bí danh để bạn có thể làm việc với nó trong khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51.

Mặc dù nó được phép về mặt cú pháp, nhưng hãy cố gắng không sử dụng các câu lệnh

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 như những điều hấp dẫn ngầm. Nó thực hành tốt hơn để luôn luôn bắt được một cái gì đó một cách rõ ràng, ngay cả khi nó chỉ là
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
90:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
4

Nếu bạn thực sự muốn bắt một loạt các ngoại lệ, thì hãy chỉ định cha mẹ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
90. Đây rõ ràng hơn là một Catchall, và nó đã giành được các trường hợp ngoại lệ mà bạn có thể không muốn bắt, như
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
92 hoặc
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
93.

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52

Từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52 làm tăng một ngoại lệ. Nếu bạn thấy bạn cần phải tăng một ngoại lệ, thì bạn có thể sử dụng
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52 theo sau là ngoại lệ để được nâng lên:
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52
keyword raises an exception. If you find you need to raise an exception, then you can use
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52 followed by the exception to be raised:

Bạn đã sử dụng

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52 trước đây, trong ví dụ
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71. Khi bạn bắt được
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
84, bạn sẽ ghi lại ngoại lệ sau khi in tin nhắn lên màn hình.

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53

Từ khóa Python từ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53 rất hữu ích cho việc chỉ định mã sẽ được chạy bất kể điều gì xảy ra trong các khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50,
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 hoặc
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22. Để sử dụng
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53, hãy sử dụng nó như một phần của khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 và chỉ định các câu lệnh nên được chạy bất kể điều gì:
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53
keyword is helpful for specifying code that should be run no matter what happens in the
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50,
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51, or
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 blocks. To use
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53, use it as part of a
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 block and specify the statements that should be run no matter what:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
5

Sử dụng ví dụ từ trước, có thể hữu ích để chỉ định rằng, bất kể điều gì xảy ra, bạn muốn biết những đối số nào chức năng được gọi với. Bạn có thể sửa đổi

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 để bao gồm một khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
53 chỉ làm điều đó:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
6

Bây giờ, cho dù

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 được gọi là gì hoặc kết quả là gì, bạn in các đối số do người dùng cung cấp:

>>>

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
7

Bạn có thể sửa đổi

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 và sử dụng nhiều khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 để xử lý tình huống này:

Tại đây, bạn sửa đổi
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
71 để tăng ngoại lệ
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
84 chỉ sau khi bạn đã in một lời nhắc hữu ích lên màn hình.

Lưu ý rằng từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 cũng có thể được sử dụng cùng với từ khóa
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39. Điều này có tác dụng tương tự như các cách sử dụng khác của
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
39, mang lại cho ngoại lệ được nâng cao một bí danh để bạn có thể làm việc với nó trong khối
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51.
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22
keyword can be used with both the
>>> help("pass")
The "pass" statement
********************

   pass_stmt ::= "pass"

"pass" is a null operation — when it is executed, nothing happens. It
is useful as a placeholder when a statement is required syntactically,
but no code needs to be executed, for example:

   def f(arg): pass    # a function that does nothing (yet)

   class C: pass       # a class with no methods (yet)
67 keyword and loops in Python, but it has one more use. It can be combined with the
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
50 and
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 Python keywords. You can use
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', ...
>>> len(keyword.kwlist)
35
22 in this way only if you also use at least one
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 block:

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
8

Mặc dù nó được phép về mặt cú pháp, nhưng hãy cố gắng không sử dụng các câu lệnh

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
51 như những điều hấp dẫn ngầm. Nó thực hành tốt hơn để luôn luôn bắt được một cái gì đó một cách rõ ràng, ngay cả khi nó chỉ là
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
90:

Nếu bạn thực sự muốn bắt một loạt các ngoại lệ, thì hãy chỉ định cha mẹ

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
90. Đây rõ ràng hơn là một Catchall, và nó đã giành được các trường hợp ngoại lệ mà bạn có thể không muốn bắt, như
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
92 hoặc
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
93.

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
9

Từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52

Từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52 làm tăng một ngoại lệ. Nếu bạn thấy bạn cần phải tăng một ngoại lệ, thì bạn có thể sử dụng
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
52 theo sau là ngoại lệ để được nâng lên:

Từ khóa
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55

Từ khóa

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 trong Python được sử dụng để chỉ định câu lệnh
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 hoặc khẳng định về biểu thức. Một tuyên bố
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 sẽ dẫn đến không có gì nếu biểu thức (
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
41) là sự thật và nó sẽ tăng
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
42 nếu biểu thức là giả. Để xác định khẳng định, hãy sử dụng
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 theo sau là biểu thức:
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55
keyword in Python is used to specify an
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 statement, or an assertion about an expression. An
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 statement will result in a no-op if the expression (
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
41) is truthy, and it will raise an
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
42 if the expression is falsy. To define an assertion, use
>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 followed by an expression:

Nói chung, các câu lệnh

>>> x = "this is a truthy value"
>>> x is True
False
>>> bool(x) is True
True

>>> y = ""  # This is falsy
>>> y is False
False
>>> bool(y) is False
True
55 sẽ được sử dụng để đảm bảo một cái gì đó cần phải đúng. Tuy nhiên, bạn không nên dựa vào họ, vì chúng có thể bị bỏ qua tùy thuộc vào cách thực thi chương trình Python của bạn.

Từ khóa lập trình không đồng bộ: >>> def func(): ... print("hello") ... >>> x = func() hello >>> print(x) None 9, >>> def func(): ... print("hello") ... >>> x = func() hello >>> print(x) None 8

Lập trình không đồng bộ là một chủ đề phức tạp. Có hai từ khóa Python được xác định để giúp làm cho mã không đồng bộ trở nên dễ đọc hơn và sạch hơn:

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 và
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8.

Các phần dưới đây giới thiệu hai từ khóa không đồng bộ và cú pháp cơ bản của chúng, nhưng họ đã giành được chiều sâu về lập trình không đồng bộ. Để tìm hiểu thêm về lập trình không đồng bộ, hãy xem Async IO trong Python: một hướng dẫn hoàn chỉnh và bắt đầu với các tính năng Async trong Python.

Từ khóa
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9

Từ khóa

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 được sử dụng với
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36 để xác định hàm không đồng bộ hoặc coroutine. Cú pháp giống như xác định một hàm, với việc bổ sung
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 ở đầu:
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9
keyword is used with
>>> x = True
>>> x is True
True

>>> y = False
>>> y is False
True
36 to define an asynchronous function, or coroutine. The syntax is just like defining a function, with the addition of
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 at the beginning:

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
0

Bạn có thể tạo một chức năng không đồng bộ bằng cách thêm từ khóa

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 trước khi định nghĩa thông thường của hàm.

Từ khóa
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8

Từ khóa Python từ ____ ____68 được sử dụng trong các chức năng không đồng bộ để chỉ định một điểm trong hàm nơi điều khiển được đưa trở lại vòng lặp sự kiện để các chức năng khác chạy. Bạn có thể sử dụng nó bằng cách đặt từ khóa

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8 trước cuộc gọi đến bất kỳ chức năng
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 nào:
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8
keyword is used in asynchronous functions to specify a point in the function where control is given back to the event loop for other functions to run. You can use it by placing the
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8 keyword in front of a call to any
>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
9 function:

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
1

Khi sử dụng

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
8, bạn có thể gọi hàm không đồng bộ và bỏ qua kết quả hoặc bạn có thể lưu trữ kết quả trong một biến khi cuối cùng hàm trở lại.

Từ khóa xử lý biến: >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 59, >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 60, >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 61

Ba từ khóa Python được sử dụng để làm việc với các biến. Từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
59 được sử dụng phổ biến hơn nhiều so với từ khóa
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60 và
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
61. Nhưng nó vẫn hữu ích để biết và hiểu cả ba từ khóa để bạn có thể xác định khi nào và cách sử dụng chúng.

Từ khóa
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
59

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
59 được sử dụng trong Python để không đặt một biến hoặc tên. Bạn có thể sử dụng nó trên các tên biến, nhưng sử dụng phổ biến hơn là xóa các chỉ mục khỏi danh sách hoặc từ điển. Để giải quyết một biến, hãy sử dụng
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
59 theo sau là biến bạn muốn giải quyết:
is used in Python to unset a variable or name. You can use it on variable names, but a more common use is to remove indexes from a list or dictionary. To unset a variable, use
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
59 followed by the variable you want to unset:

Hãy giả sử bạn muốn làm sạch một từ điển mà bạn nhận được từ một phản hồi API bằng cách ném ra các phím mà bạn biết bạn đã giành được sử dụng. Bạn có thể làm như vậy với từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
59:

>>>

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
2

Điều này sẽ loại bỏ các khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
69 và
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
70 khỏi từ điển
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
71.

Từ khóa
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60

Nếu bạn cần sửa đổi một biến được xác định trong một hàm nhưng được xác định trong phạm vi toàn cầu, thì bạn sẽ cần sử dụng từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60. Điều này hoạt động bằng cách chỉ định trong hàm mà các biến cần được kéo vào hàm từ phạm vi toàn cầu:global scope, then you’ll need to use the
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60
keyword. This works by specifying in the function which variables need to be pulled into the function from the global scope:

Một ví dụ cơ bản là tăng một biến toàn cầu với một cuộc gọi chức năng. Bạn có thể làm điều đó với từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60:

>>>

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
3

Điều này sẽ loại bỏ các khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
69 và
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
70 khỏi từ điển
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
71.

Từ khóa
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60

Nếu bạn cần sửa đổi một biến được xác định trong một hàm nhưng được xác định trong phạm vi toàn cầu, thì bạn sẽ cần sử dụng từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60. Điều này hoạt động bằng cách chỉ định trong hàm mà các biến cần được kéo vào hàm từ phạm vi toàn cầu:
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
61
keyword is similar to
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60 in that it allows you to modify variables from a different scope. With
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60, the scope you’re pulling from is the global scope. With
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
61, the scope you’re pulling from is the parent scope. The syntax is similar to
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60:

Một ví dụ cơ bản là tăng một biến toàn cầu với một cuộc gọi chức năng. Bạn có thể làm điều đó với từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
60:

Điều này thường không được coi là thực hành tốt, nhưng nó có cách sử dụng của nó. Để tìm hiểu thêm về từ khóa >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 60, hãy xem Python Phạm vi & Quy tắc LegB: Giải quyết tên trong mã của bạn.

Từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
61

Từ khóa >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 61 tương tự như >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 60 ở chỗ nó cho phép bạn sửa đổi các biến từ một phạm vi khác. Với >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 60, phạm vi mà bạn đã lấy từ là phạm vi toàn cầu. Với >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 61, phạm vi mà bạn kéo từ là phạm vi cha mẹ. Cú pháp tương tự như >>> x = "this is a truthy value" >>> if x is True: # Don't do this ... print("x is True") ... >>> if x: # Do this ... print("x is truthy") ... x is truthy 60:

Từ khóa này được sử dụng rất thường xuyên, nhưng đôi khi nó có thể tiện dụng. Để biết thêm về phạm vi và từ khóa

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
61, hãy xem phạm vi Python & Quy tắc LegB: Giải quyết tên trong mã của bạn.
left if not left else right
0
was a keyword, the syntax to print something to the screen looked like this:

Từ khóa Python không dùng nữa

Đôi khi một từ khóa Python trở thành một hàm tích hợp. Đó là trường hợp với cả

left if not left else right
0 và
left if not left else right
1. Chúng từng là từ khóa Python trong phiên bản 2.7, nhưng chúng đã được thay đổi thành các chức năng tích hợp.

Từ khóa

left if not left else right
0 trước đây

Từ khóa left if not left else right 1 trước đây

Trong Python 2.7, từ khóa

left if not left else right
1 lấy mã Python làm chuỗi và thực hiện nó. Điều này đã được thực hiện với cú pháp sau:
left if not left else right
1
keyword took Python code as a string and executed it. This was done with the following syntax:

Bạn có thể có được hành vi tương tự trong Python 3+, chỉ với

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
91 tích hợp. Ví dụ: nếu bạn muốn thực thi
>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
92 trong mã Python của mình, thì bạn có thể làm như sau:

>>>

>>> def func():
...     print("hello")
...
>>> x = func()
hello
>>> print(x)
None
4

Để biết thêm về

>>> x = "this is a truthy value"
>>> if x is True:  # Don't do this
...     print("x is True")
...
>>> if x:  # Do this
...     print("x is truthy")
...
x is truthy
91 và việc sử dụng của nó, hãy xem cách chạy các tập lệnh Python và Python từ Exec (): Thực thi mã được tạo động.

Sự kết luận

Từ khóa Python là các khối xây dựng cơ bản của bất kỳ chương trình Python nào. Hiểu sử dụng đúng cách của họ là chìa khóa để cải thiện các kỹ năng và kiến ​​thức của bạn về Python.

Trong suốt bài viết này, bạn đã thấy một vài điều để củng cố các từ khóa Python hiểu của bạn và để giúp bạn viết mã hiệu quả và dễ đọc hơn.

Trong bài viết này, bạn đã học được:

  • Các từ khóa Python trong phiên bản 3.8 và cách sử dụng cơ bản của chúngPython keywords in version 3.8 and their basic usage
  • Một số tài nguyên để giúp bạn hiểu sâu hơn về nhiều từ khóaresources to help deepen your understanding of many of the keywords
  • Cách sử dụng mô -đun Python từ
    >>> def func():
    ...     print("hello")
    ...
    >>> x = func()
    hello
    >>> print(x)
    None
    
    5 để làm việc với các từ khóa theo cách lập trình
    >>> def func():
    ...     print("hello")
    ...
    >>> x = func()
    hello
    >>> print(x)
    None
    
    5 module
    to work with keywords in a programmatic way

Nếu bạn hiểu hầu hết các từ khóa này và cảm thấy thoải mái khi sử dụng chúng, thì bạn có thể quan tâm để tìm hiểu thêm về ngữ pháp Python và cách các câu lệnh sử dụng các từ khóa này được chỉ định và xây dựng.

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Khám phá các từ khóa trong Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Exploring Keywords in Python

Từ khóa trong Python là gì?

Từ khóa Python là những từ dành riêng đặc biệt có ý nghĩa và mục đích cụ thể và không thể được sử dụng cho bất cứ điều gì ngoại trừ những mục đích cụ thể đó.Các từ khóa này luôn có sẵn, bạn sẽ không bao giờ phải nhập chúng vào mã của bạn.Từ khóa Python khác với các chức năng và loại tích hợp của Python.special reserved words that have specific meanings and purposes and can't be used for anything but those specific purposes. These keywords are always available—you'll never have to import them into your code. Python keywords are different from Python's built-in functions and types.

Có bao nhiêu từ khóa trong Python?

Họ là để xác định cú pháp và cấu trúc của ngôn ngữ Python.Bạn nên biết có 33 từ khóa trong ngôn ngữ lập trình Python khi viết hướng dẫn này.Mặc dù số lượng có thể thay đổi theo thời gian.Ngoài ra các từ khóa trong Python là trường hợp nhạy cảm.33 keywords in Python programming language as of writing this tutorial. Although the number can vary in course of time. Also keywords in Python is case sensitive.

Từ khóa giải thích với ví dụ là gì?

Từ khóa là các từ và cụm từ mà mọi người gõ vào các công cụ tìm kiếm để tìm những gì họ đang tìm kiếm.Ví dụ: nếu bạn đang tìm mua một chiếc áo khoác mới, bạn có thể gõ một cái gì đó giống như áo khoác da nam giới vào Google.Mặc dù cụm từ đó bao gồm nhiều hơn một từ, nó vẫn là một từ khóa.the words and phrases that people type into search engines to find what they're looking for. For example, if you were looking to buy a new jacket, you might type something like “mens leather jacket” into Google. Even though that phrase consists of more than one word, it's still a keyword.