Hướng dẫn when should elif be used in python? - khi nào thì nên sử dụng elif trong python?

Nếu ... tuyên bố khác trong Python?

Ra quyết định là bắt buộc khi chúng tôi chỉ muốn thực thi mã nếu một điều kiện nhất định được thỏa mãn.

Tuyên bố

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
2 được sử dụng trong Python để ra quyết định.

Python nếu cú ​​pháp tuyên bố

if test expression:
    statement(s)

Ở đây, chương trình đánh giá

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
3 và sẽ thực hiện (các) câu lệnh nếu biểu thức kiểm tra là
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
4.

Nếu biểu thức kiểm tra là

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
5, (các) câu lệnh không được thực thi.

Trong Python, cơ thể của tuyên bố

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 được chỉ định bởi thụt lề. Cơ thể bắt đầu với một vết lõm và dòng chưa được đánh dấu đầu tiên đánh dấu sự kết thúc.

Python diễn giải các giá trị khác không là

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
4.
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
8 và
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
9 được hiểu là
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
5.

Python nếu sơ đồ tuyên bố

Hướng dẫn when should elif be used in python? - khi nào thì nên sử dụng elif trong python?
Lưu đồ của IF tuyên bố trong chương trình Python

Ví dụ: Python nếu tuyên bố

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

Khi bạn chạy chương trình, đầu ra sẽ là:

3 is a positive number
This is always printed
This is also always printed.

Trong ví dụ trên,

3 is a positive number
This is always printed
This is also always printed.
1 là biểu thức thử nghiệm.

Cơ thể của

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 chỉ được thực hiện nếu điều này đánh giá là
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
4.

Khi num biến bằng 3, biểu thức kiểm tra là đúng và các câu lệnh bên trong phần thân của

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 được thực thi.

Nếu num biến bằng -1, biểu thức kiểm tra là sai và các câu lệnh bên trong phần thân của

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 bị bỏ qua.

Tuyên bố

3 is a positive number
This is always printed
This is also always printed.
6 nằm ngoài khối
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 (chưa được coi là). Do đó, nó được thực hiện bất kể biểu thức kiểm tra.


Python nếu ... tuyên bố khác

Cú pháp của nếu ... khác

if test expression:
    Body of if
else:
    Body of else

Tuyên bố

3 is a positive number
This is always printed
This is also always printed.
8 đánh giá
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
3 và sẽ thực hiện phần thân của
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 khi điều kiện thử nghiệm là
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
4.

Nếu điều kiện là

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
5, cơ thể của
if test expression:
    Body of if
else:
    Body of else
3 được thực thi. Thắng được sử dụng để tách các khối.

Python nếu..else sơ đồ

Hướng dẫn when should elif be used in python? - khi nào thì nên sử dụng elif trong python?
Sơ đồ của nếu ... tuyên bố khác trong Python

Ví dụ về nếu ... khác

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")

Đầu ra

Positive or Zero

Trong ví dụ trên, khi Num bằng 3, biểu thức kiểm tra là đúng và phần thân của

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 được thực thi và
if test expression:
    Body of if
else:
    Body of else
5 của những thứ khác bị bỏ qua.

Nếu num bằng -5, biểu thức kiểm tra là sai và phần thân của

if test expression:
    Body of if
else:
    Body of else
3 được thực thi và cơ thể của
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 bị bỏ qua.

Nếu num bằng 0, biểu thức kiểm tra là đúng và cơ thể của

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 được thực thi và
if test expression:
    Body of if
else:
    Body of else
5 của những thứ khác bị bỏ qua.


Python nếu ... Elif ... tuyên bố khác

Cú pháp của nếu ... Elif ... khác

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
0 là viết tắt cho người khác nếu. Nó cho phép chúng tôi kiểm tra nhiều biểu thức.

Nếu điều kiện cho

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 là
# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
5, nó sẽ kiểm tra điều kiện của khối
# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
0 tiếp theo, v.v.

Nếu tất cả các điều kiện là

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
5, cơ thể khác được thực thi.

Chỉ có một khối trong số một số khối

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
5 được thực thi theo điều kiện.

Khối

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
6 chỉ có thể có một khối
if test expression:
    Body of if
else:
    Body of else
3. Nhưng nó có thể có nhiều khối
# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
0.

Sơ đồ của nếu ... Elif ... khác

Hướng dẫn when should elif be used in python? - khi nào thì nên sử dụng elif trong python?
Sơ đồ của nếu ... Elif .... khác tuyên bố trong Python

Ví dụ về nếu ... Elif ... khác

'''In this program, 
we check if the number is positive or
negative or zero and 
display an appropriate message'''

num = 3.4

# Try these two variations as well:
# num = 0
# num = -4.5

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

Khi biến số là dương, số dương được in.

Nếu num bằng 0, số không được in.

Nếu Num là âm, số âm được in.


Python lồng nhau nếu các câu lệnh

Chúng ta có thể có một tuyên bố

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
5 bên trong một tuyên bố
# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")
5 khác. Điều này được gọi là làm tổ trong lập trình máy tính.

Bất kỳ số lượng câu này có thể được lồng bên trong nhau. Thẩm lớp là cách duy nhất để tìm ra mức độ làm tổ. Họ có thể trở nên khó hiểu, vì vậy họ phải tránh trừ khi cần thiết.

Python lồng nhau nếu ví dụ

'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Đầu ra 1

Enter a number: 5
Positive number

Đầu ra 2

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
0

Đầu ra 3

# If the number is positive, we print an appropriate message

num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")
1

Khi nào bạn nên sử dụng Python tuyên bố Elif?

Trong Python, Elif là viết tắt của "khác nếu" và được sử dụng khi câu lệnh IF đầu tiên không đúng, nhưng bạn muốn kiểm tra một điều kiện khác. Có nghĩa là, nếu các câu lệnh kết hợp với Elif và các câu lệnh khác để thực hiện một loạt các kiểm tra. Một khối IF/ELIF/khác đầy đủ trong ví dụ mã bên dưới.when the first if statement isn't true, but you want to check for another condition. Meaning, if statements pair up with elif and else statements to perform a series of checks. A full if/elif/else block is in the code example below.

Điểm sử dụng Elif là gì?

ELIF là viết tắt cho người khác nếu.Nó cho phép chúng tôi kiểm tra nhiều biểu thức.Nếu điều kiện nếu là sai, nó sẽ kiểm tra điều kiện của khối Elif tiếp theo, v.v.Nếu tất cả các điều kiện là sai, cơ thể khác được thực thi.allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed.

Tôi có nên sử dụng Elif hay không?

Vì vậy, sự khác biệt là mã luôn kiểm tra xem câu lệnh 'if' có đúng không, chỉ kiểm tra các câu lệnh 'elif' nếu mỗi câu 'if' và 'elif' ở trên là sai và 'khác' chỉ chạy khi điều kiệnĐối với tất cả các câu lệnh 'if' và 'elif' là sai.

Tại sao nó tốt để sử dụng nếu Elif và những người khác trong mã của bạn?

Cấu trúc IF / ELIF / ELSE là một cách phổ biến để kiểm soát luồng của chương trình, cho phép bạn thực thi các khối mã cụ thể tùy thuộc vào giá trị của một số dữ liệu.a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data.