Hướng dẫn what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?

Tôi thay vì giải thích nó như thể bạn là một người thông minh, mà bạn là.

Nói một cách đơn giản, luồng kiểm soát là con đường của hành động/sự kiện xảy ra trong chương trình của bạn. Trong trường hợp đơn giản nhất, đường dẫn là tuyến tính (một câu lệnh được thực thi theo cách khác theo thứ tự chúng xuất hiện trong mã nguồn). Mã bên trái tạo ra luồng điều khiển bạn nhìn thấy ở bên phải.control flow is the path of actions/events that happen in your program. In the simplest case, the path is linear (one statement is executed after another in the order that they appear in the source code). The code on the left creates the control flow you see on the right.

Hướng dẫn what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
4 Báo cáo giới thiệu các nhánh trong luồng điều khiển.branches in the control flow.

Đặt

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
5,
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
6 và
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
7 là điều kiện (biểu thức boolean có thể là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8 hoặc
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9, chẳng hạn như
if test expression:
    statement(s)
0). Sau đó, một
if test expression:
    statement(s)
1 làm cho một nhánh:conditions (Boolean expressions that can be either
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8 or
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9, such as
if test expression:
    statement(s)
0). Then an
if test expression:
    statement(s)
1 makes a branch:

Hướng dẫn what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?

Chương trình sẽ chỉ

if test expression:
    statement(s)
2 nếu
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
5 là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8 và
if test expression:
    statement(s)
5 thay vào đó nếu
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
5 là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9.

Mỗi

if test expression:
    statement(s)
8 cho phép bạn giới thiệu một chi nhánh khác:

Hướng dẫn what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?

Điều quan trọng cần lưu ý ở đây là, để đến

if test expression:
    statement(s)
5, không chỉ
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
6 phải là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8, mà
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
5 phải là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9. Và đó là sự khác biệt giữa
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
4 và một loạt
# 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.")
5S:

if a:
    do this
elif b:
    do that
elif c:
    do something else
else:
    print "none of the above"

tương đương với (chính xác giống như) điều này:equivalent to (exactly the same as) this:

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"

Như bạn có thể thấy, ký hiệu

# 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 cho phép bạn viết cùng một logic (cùng một luồng điều khiển) mà không lặp lại điều kiện
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
5 nhiều lần.

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.")
8 đượ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.")
9 và sẽ thực hiện (các) câu lệnh nếu biểu thức kiểm tra là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8.

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

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9, (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.")
5 đượ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 a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8.
3 is a positive number
This is always printed
This is also always printed.
4 và
3 is a positive number
This is always printed
This is also always printed.
5 được hiểu là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9.

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

Hướng dẫn what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?
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.
7 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.")
5 chỉ được thực hiện nếu điều này đánh giá là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8.

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.")
5 đượ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.")
5 bị bỏ qua.

Tuyên bố

if test expression:
    Body of if
else:
    Body of else
2 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.")
5 (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ố

if test expression:
    Body of if
else:
    Body of else
4 đá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.")
9 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.")
5 khi điều kiện thử nghiệm là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
8.

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

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9, cơ thể của
if test expression:
    Body of if
else:
    Body of else
9 đượ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 what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?
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.")
5 được thực thi và
# 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")
1 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
9 đượ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.")
5 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.")
5 được thực thi và
# 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")
1 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

if test expression:
    statement(s)
8 là viết tắt của nếu không. 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.")
5 là
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9, nó sẽ kiểm tra điều kiện của khối
if test expression:
    statement(s)
8 tiếp theo, v.v.

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

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
9, cơ thể khác được thực thi.

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

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
4 đượ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.")
5 chỉ có thể có một khối
if test expression:
    Body of if
else:
    Body of else
9. Nhưng nó có thể có nhiều khối
if test expression:
    statement(s)
8.

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

Hướng dẫn what is the difference between else and if in python? - sự khác biệt giữa khác và nếu trong python là gì?
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ố

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
4 bên trong một tuyên bố
if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
4 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ụ

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
0

Đầu ra 1

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
1

Đầu ra 2

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
2

Đầu ra 3

if a:
    do this
if (not a) and b:
    do that
if (not a) and (not b) and c:
    do something else
if (not a) and (not b) and (not c):
    print "none of the above"
3

Sự khác biệt giữa khác và nếu?

Sử dụng nếu chỉ định một khối mã sẽ được thực thi, nếu một điều kiện được chỉ định là đúng.Sử dụng khác để chỉ định một khối mã sẽ được thực thi, nếu cùng một điều kiện là sai.Sử dụng khác nếu chỉ định một điều kiện mới để kiểm tra, nếu điều kiện đầu tiên là sai.

Sự khác biệt giữa nếu và Elif trong Python là gì?

Sự khác biệt giữa nếu và nếu Elif khác Python sẽ đánh giá cả ba câu nếu xác định xem chúng có đúng không.Khi một điều kiện trong câu lệnh IF ELITE khác là đúng, Python ngừng đánh giá các điều kiện khác.Bởi vì điều này, nếu Elif khác nhanh hơn ba câu lệnh.if elif else is faster than three if statements.