Hướng dẫn how do i use any condition in python? - làm cách nào để sử dụng bất kỳ điều kiện nào trong python?

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 nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Python Any (): Hàm Boolean được cấp nguồn This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python any(): Powered Up Boolean Function

Là một lập trình viên Python, bạn sẽ thường xuyên đối phó với Booleans và các tuyên bố có điều kiện, đôi khi những người rất phức tạp. Trong những tình huống đó, bạn có thể cần phải dựa vào các công cụ có thể đơn giản hóa logic và củng cố thông tin. May mắn thay,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python là một công cụ như vậy. Nó xem qua các yếu tố trong một điều đáng tin cậy và trả về một giá trị duy nhất cho biết liệu bất kỳ yếu tố nào có đúng trong bối cảnh boolean hay sự thật hay không.
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1
in Python is such a tool. It looks through the elements in an iterable and returns a single value indicating whether any element is true in a Boolean context, or truthy.

Trong hướng dẫn này, bạn sẽ học:

  • Cách sử dụng
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    1
  • Cách quyết định giữa
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    1 và
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    4

Hãy để lặn ngay trong!

Cách sử dụng $ python recruit_developer.py Scheduled interview with Susan Jones Scheduled interview with Sam Hughes 1 trong Python

Hãy tưởng tượng rằng bạn đang viết một chương trình cho bộ phận tuyển dụng của chủ nhân của bạn. Bạn có thể muốn lên lịch phỏng vấn với các ứng viên đáp ứng bất kỳ tiêu chí nào sau đây:

  1. Biết Python rồi
  2. Có năm năm hoặc nhiều năm kinh nghiệm phát triển
  3. Có bằng cấp

Một công cụ bạn có thể sử dụng để viết biểu thức có điều kiện này là

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4:

# recruit_developer.py
def schedule_interview(applicant):
    print(f"Scheduled interview with {applicant['name']}")

applicants = [
    {
        "name": "Devon Smith",
        "programming_languages": ["c++", "ada"],
        "years_of_experience": 1,
        "has_degree": False,
        "email_address": "",
    },
    {
        "name": "Susan Jones",
        "programming_languages": ["python", "javascript"],
        "years_of_experience": 2,
        "has_degree": False,
        "email_address": "",
    },
    {
        "name": "Sam Hughes",
        "programming_languages": ["java"],
        "years_of_experience": 4,
        "has_degree": True,
        "email_address": "",
    },
]
for applicant in applicants:
    knows_python = "python" in applicant["programming_languages"]
    experienced_dev = applicant["years_of_experience"] >= 5

    meets_criteria = (
        knows_python
        or experienced_dev
        or applicant["has_degree"]
    )
    if meets_criteria:
        schedule_interview(applicant)

Trong ví dụ trên, bạn kiểm tra từng thông tin đăng nhập của người nộp đơn và lên lịch phỏng vấn nếu người nộp đơn đáp ứng bất kỳ tiêu chí nào trong ba tiêu chí của bạn.

Nếu bạn thực thi mã này, thì bạn sẽ thấy Susan và Sam sẽ nhận được các cuộc phỏng vấn:

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes

Lý do chương trình chọn sắp xếp các cuộc phỏng vấn với Susan và Sam là vì Susan đã biết Python và Sam có bằng cấp. Lưu ý mỗi ứng viên chỉ cần đáp ứng một tiêu chí.

Một cách khác để đánh giá thông tin đăng nhập của người nộp đơn là sử dụng

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1. Khi bạn sử dụng
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python, bạn phải thông qua thông tin đăng nhập của người nộp đơn như một đối số có thể lặp lại:

for applicant in applicants:
    knows_python = "python" in applicant["programming_languages"]
    experienced_dev = applicant["years_of_experience"] >= 5

    credentials = (
        knows_python,
        experienced_dev,
        applicant["has_degree"],
    )
    if any(credentials):
        schedule_interview(applicant)

Khi bạn sử dụng

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python, hãy nhớ rằng bạn có thể vượt qua bất kỳ điều gì có thể xảy ra như một đối số:

>>>

>>> any([0, 0, 1, 0])
True

>>> any(set((True, False, True)))
True

>>> any(map(str.isdigit, "hello world"))
False

Trong mỗi ví dụ,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 lặp qua một python khác nhau, kiểm tra sự thật của từng yếu tố cho đến khi nó tìm thấy một giá trị sự thật hoặc kiểm tra mọi yếu tố.

Bạn có thể tự hỏi nếu

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 chỉ là phiên bản mặc quần áo của
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4. Trong phần tiếp theo, bạn sẽ học được sự khác biệt giữa các công cụ này.

Cách phân biệt giữa $ python recruit_developer.py Scheduled interview with Susan Jones Scheduled interview with Sam Hughes 4 và $ python recruit_developer.py Scheduled interview with Susan Jones Scheduled interview with Sam Hughes 1

Có hai sự khác biệt chính giữa

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python:

  1. Cú pháp
  2. Giá trị trả về

Đầu tiên, bạn sẽ tìm hiểu về cách cú pháp ảnh hưởng đến khả năng sử dụng và khả năng đọc của từng công cụ. Thứ hai, bạn sẽ học các loại giá trị mà mỗi công cụ trả về. Biết những khác biệt này sẽ giúp bạn quyết định công cụ nào là tốt nhất cho một tình huống nhất định.

Cú pháp

Giá trị trả về

>>>

>>> True or False
True

Trong mỗi ví dụ,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 lặp qua một python khác nhau, kiểm tra sự thật của từng yếu tố cho đến khi nó tìm thấy một giá trị sự thật hoặc kiểm tra mọi yếu tố.

>>>

>>> any((False, True))
True

Trong mỗi ví dụ,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 lặp qua một python khác nhau, kiểm tra sự thật của từng yếu tố cho đến khi nó tìm thấy một giá trị sự thật hoặc kiểm tra mọi yếu tố.

>>>

>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True

Trong mỗi ví dụ,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 lặp qua một python khác nhau, kiểm tra sự thật của từng yếu tố cho đến khi nó tìm thấy một giá trị sự thật hoặc kiểm tra mọi yếu tố.

Bạn có thể tự hỏi nếu

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 chỉ là phiên bản mặc quần áo của
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4. Trong phần tiếp theo, bạn sẽ học được sự khác biệt giữa các công cụ này.

def knows_python(applicant):
    print(f"Determining if {applicant['name']} knows Python...")
    return "python" in applicant["programming_languages"]

def is_local(applicant):
    print(f"Determine if {applicant['name']} lives near the office...")

should_interview = knows_python(applicant) or is_local(applicant)

Cách phân biệt giữa

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1lazy evaluation, or short-circuit evaluation. By default,
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 evaluates conditions lazily, whereas
>>> any([0, 0, 1, 0])
True

>>> any(set((True, False, True)))
True

>>> any(map(str.isdigit, "hello world"))
False
4 does not.

Có hai sự khác biệt chính giữa

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python:

Cú pháp

should_interview = any([knows_python(applicant), is_local(applicant)])

Giá trị trả về

Đầu tiên, bạn sẽ tìm hiểu về cách cú pháp ảnh hưởng đến khả năng sử dụng và khả năng đọc của từng công cụ. Thứ hai, bạn sẽ học các loại giá trị mà mỗi công cụ trả về. Biết những khác biệt này sẽ giúp bạn quyết định công cụ nào là tốt nhất cho một tình huống nhất định.

any((meets_criteria(applicant) for applicant in applicants))

Ví dụ này sử dụng biểu thức máy phát để tạo các giá trị boolean cho biết liệu người nộp đơn có đáp ứng các tiêu chí cho một cuộc phỏng vấn hay không. Khi người nộp đơn đáp ứng các tiêu chí,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 sẽ trả lại
>>> any([0, 0, 1, 0])
True

>>> any(set((True, False, True)))
True

>>> any(map(str.isdigit, "hello world"))
False
5 mà không kiểm tra các ứng viên còn lại. Nhưng hãy nhớ rằng những loại cách giải quyết này cũng trình bày các vấn đề của riêng họ và có thể không phù hợp trong mọi tình huống.

Điều quan trọng nhất cần nhớ là sự khác biệt cú pháp giữa

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 có thể ảnh hưởng đến khả năng sử dụng của chúng.

Cú pháp là sự khác biệt duy nhất ảnh hưởng đến khả năng sử dụng của các công cụ này. Tiếp theo, chúng ta hãy xem xét các giá trị trả về khác nhau cho

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 và cách chúng có thể ảnh hưởng đến quyết định của bạn về công cụ nào sẽ sử dụng.

Giá trị trả về

Python sườn

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 trả về các loại giá trị khác nhau.
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trả về Boolean, cho biết liệu nó có tìm thấy giá trị sự thật trong điều không thể sử dụng được không:

Trong ví dụ này,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 đã tìm thấy một giá trị sự thật (số nguyên
>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True
0), do đó, nó đã trả lại giá trị boolean
>>> any([0, 0, 1, 0])
True

>>> any(set((True, False, True)))
True

>>> any(map(str.isdigit, "hello world"))
False
5.

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4, mặt khác, trả về giá trị sự thật đầu tiên mà nó tìm thấy, điều này sẽ không nhất thiết là một boolean. Nếu không có giá trị sự thật, thì
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 sẽ trả về giá trị cuối cùng:

>>>

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
0

Trong ví dụ đầu tiên,

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 đã đánh giá
>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True
0, đó là sự thật và trả lại nó mà không đánh giá
>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True
6. Trong ví dụ thứ hai,
>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True
7 là giả, vì vậy
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 đã đánh giá
>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True
6 tiếp theo, cũng là giả. Nhưng vì không có nhiều biểu thức để kiểm tra,
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 trả về giá trị cuối cùng,
>>> import functools
>>> functools.reduce(lambda x, y: x or y, (True, False, False))
True
6.

Khi bạn quyết định sử dụng công cụ nào, nó rất hữu ích khi xem xét liệu bạn có muốn biết giá trị thực của đối tượng hay không, liệu một giá trị sự thật có tồn tại ở đâu đó trong bộ sưu tập các đối tượng hay không.

Sự kết luận

Xin chúc mừng! Bạn đã học được các thông tin về việc sử dụng

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python và sự khác biệt giữa
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4. Với sự hiểu biết sâu sắc hơn về hai công cụ này, bạn đã chuẩn bị tốt để quyết định giữa chúng trong mã của riêng bạn.

Bây giờ bạn biết:

  • Cách sử dụng
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    1 trong Python
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    1
    in Python
  • Tại sao bạn sử dụng
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    1 thay vì
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    4
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    1
    instead of
    $ python recruit_developer.py
    Scheduled interview with Susan Jones
    Scheduled interview with Sam Hughes
    
    4

Nếu bạn muốn tiếp tục tìm hiểu về các biểu thức có điều kiện và cách sử dụng các công cụ như

$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
4 và
$ python recruit_developer.py
Scheduled interview with Susan Jones
Scheduled interview with Sam Hughes
1 trong Python, thì bạn có thể kiểm tra các tài nguyên sau:

  • should_interview = any([knows_python(applicant), is_local(applicant)])
    
    0
  • should_interview = any([knows_python(applicant), is_local(applicant)])
    
    1
  • should_interview = any([knows_python(applicant), is_local(applicant)])
    
    2 vòng lặp

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 nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Python Any (): Hàm Boolean được cấp nguồn This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python any(): Powered Up Boolean Function

Làm thế nào để bạn viết một điều kiện trong Python?

Python hỗ trợ các điều kiện logic thông thường từ toán học:..
Bằng: a == b ..
Không bằng: A! = B ..
Ít hơn: A
Nhỏ hơn hoặc bằng: a
Lớn hơn: a> b ..
Lớn hơn hoặc bằng: a> = b ..

Việc sử dụng bất kỳ () nào trong Python?

Python bất kỳ () hàm nào Hàm () any trả về true nếu bất kỳ mục nào trong itable là đúng, nếu không nó sẽ trả về sai.Nếu đối tượng có thể xóa, hàm bất kỳ () sẽ trả về sai.returns True if any item in an iterable are true, otherwise it returns False. If the iterable object is empty, the any() function will return False.

Làm thế nào để bạn sử dụng các câu lệnh có điều kiện trong Python?

Chúng ta cần sử dụng các câu lệnh có điều kiện này để thực thi khối mã cụ thể nếu điều kiện đã cho là đúng hoặc sai ...
Nếu tuyên bố ..
tuyên bố if-else ..
Tuyên bố của Elif ..
Nested If và if-Else tuyên bố ..
Ladder Elif ..

Làm thế nào để bạn sử dụng bất kỳ () hoặc tất cả () trong Python?

Phương thức bất kỳ () trả về đúng nếu bất kỳ mục nào trong danh sách là đúng và hàm tất cả () trả về đúng nếu tất cả các mục danh sách là đúng.Thông thường, khi bạn lập trình, bạn có thể muốn kiểm tra xem bất kỳ hoặc tất cả các giá trị trong danh sách đánh giá là true.. Often, when you're programming, you may want to check whether any or all of the values in a list evaluate to True.