Có những tình huống trong cuộc sống thực khi chúng ta cần đưa ra một số quyết định và dựa trên những quyết định này, chúng ta quyết định mình nên làm gì tiếp theo. Các tình huống tương tự cũng phát sinh trong lập trình khi chúng ta cần đưa ra một số quyết định và dựa trên những quyết định này, chúng ta sẽ thực thi khối mã tiếp theo. Các câu lệnh ra quyết định trong các ngôn ngữ lập trình quyết định hướng của luồng thực thi chương trình.
Trong Python, câu lệnh if-else elif được sử dụng để ra quyết định
câu lệnh if
câu lệnh if là câu lệnh ra quyết định đơn giản nhất. Nó được sử dụng để quyết định xem một câu lệnh hoặc khối câu lệnh nào đó có được thực thi hay không. e nếu một điều kiện nào đó đúng thì khối câu lệnh được thực thi ngược lại thì không
cú pháp.
if condition: # Statements to execute if # condition is true
Ở đây, điều kiện sau khi đánh giá sẽ là true hoặc false. nếu câu lệnh chấp nhận giá trị boolean – nếu giá trị là true thì nó sẽ thực thi khối câu lệnh bên dưới nó, ngược lại thì không. Chúng ta cũng có thể sử dụng điều kiện với dấu ngoặc ‘[‘ ‘]’.
Như chúng ta đã biết, python sử dụng thụt đầu dòng để xác định một khối. Vì vậy, khối bên dưới câu lệnh if sẽ được xác định như trong ví dụ bên dưới.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.
Sơ đồ câu lệnh if trong Python
Thí dụ. Câu lệnh if trong Python
Python3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
I am Not in if0
I am Not in if1____82
I am Not in if3
I am Not in if4
I am Not in if5______86
I am Not in if7
I am Not in if8
I am Not in if5
I am Not in if6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.91
I am Not in if8
đầu ra.
________số 8Vì điều kiện có trong câu lệnh if là sai. Vì vậy, khối bên dưới câu lệnh if được thực thi
nếu khác
Chỉ riêng câu lệnh if đã cho chúng ta biết rằng nếu một điều kiện là đúng thì nó sẽ thực thi một khối câu lệnh và nếu điều kiện là sai thì nó sẽ không thực thi. Nhưng nếu chúng ta muốn làm gì khác nếu điều kiện là sai. Đây là câu lệnh khác. Chúng ta có thể sử dụng câu lệnh khác với câu lệnh if để thực thi một khối mã khi điều kiện sai.
cú pháp.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.9
Lưu đồ câu lệnh if-else trong Python
ví dụ 1. Câu lệnh if-else trong Python
Python3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.93
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.94
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.98
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.00
I am Not in if2
I am Not in if3
I am Not in if4
I am Not in if5____86
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.06
I am Not in if8
I am Not in if4
I am Not in if5____86
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.11
I am Not in if8
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.14
I am Not in if4
I am Not in if5____86
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.18
I am Not in if8
I am Not in if4
I am Not in if5____86
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.33
I am Not in if8
_______85____86____537____88
đầu ra.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.0
Khối mã theo sau câu lệnh khác được thực thi khi điều kiện có trong câu lệnh if là sai sau khi gọi câu lệnh không nằm trong khối [không có dấu cách]
ví dụ 2. Python nếu khác trong việc hiểu danh sách
Python3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.39
________ 610 ________ 611
I am Not in if4____613____17
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.15
I am Not in if4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.17
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.18
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.19
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.10
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.11
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.12
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.13____714
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.16
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.17
I am Not in if4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.19
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
I am Not in if43
I am Not in if44
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
I am Not in if46
I am Not in if47
I am Not in if48
I am Not in if49
I am Not in if48
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.01
I am Not in if48
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.03
I am Not in if48
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.05
I am Not in if48
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.07
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.08
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.10
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.11
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.13_______617
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.19
I am Not in if44
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.19
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.20
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.08
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.23
I am Not in if5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.25
Đầu ra
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.1
lồng nhau nếu
Một if lồng nhau là một câu lệnh if là đích của một câu lệnh if khác. Các câu lệnh if lồng nhau có nghĩa là một câu lệnh if bên trong một câu lệnh if khác. Có, Python cho phép chúng ta lồng các câu lệnh if bên trong các câu lệnh if. tôi. e, chúng ta có thể đặt một câu lệnh if bên trong một câu lệnh if khác
cú pháp.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.3
Lưu đồ của câu lệnh if lồng nhau trong Python
Thí dụ. Python lồng nhau nếu
Python3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.26
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.94
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32____17
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
I am Not in if3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.37
I am Not in if4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.39
I am Not in if4
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.00
I am Not in if2
I am Not in if3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.12
I am Not in if5
I am Not in if6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.06
I am Not in if8
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.50
I am Not in if4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.52
I am Not in if4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.54
I am Not in if4
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.56
I am Not in if4
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.00
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.60
I am Not in if3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.12
I am Not in if5
I am Not in if6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.65
I am Not in if8
_______84____413____414
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.12
I am Not in if5
I am Not in if6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.18
I am Not in if8
Đầu ra.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.1
thang if-elif-else
Tại đây, người dùng có thể quyết định trong số nhiều tùy chọn. Các câu lệnh if được thực thi từ trên xuống. Ngay khi một trong các điều kiện kiểm soát if là đúng, câu lệnh liên quan đến if đó được thực thi và phần còn lại của bậc thang được bỏ qua. Nếu không có điều kiện nào là đúng, thì câu lệnh khác cuối cùng sẽ được thực thi
cú pháp.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.1
Lưu đồ của câu lệnh if else elif trong Python
Thí dụ. Câu lệnh if else elif trong Python
Python3
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.75
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.94
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.5
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.98
I am Not in if0
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32____17
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
I am Not in if3
I am Not in if4
I am Not in if5____86
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.90
I am Not in if8
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.92
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
I am Not in if2
I am Not in if3
I am Not in if4
I am Not in if5______86
I am Not in if01
I am Not in if8
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.92
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.32
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.98
I am Not in if3
I am Not in if4
I am Not in if5
I am Not in if6
I am Not in if12
I am Not in if8
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.13
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.14
I am Not in if4
I am Not in if5
I am Not in if6
I am Not in if19
I am Not in if8
Đầu ra.
I am Not in if4
Bàn tay ngắn nếu tuyên bố
Bất cứ khi nào chỉ có một câu lệnh duy nhất được thực thi bên trong khối if thì có thể sử dụng tốc ký if. Câu lệnh có thể được đặt trên cùng một dòng với câu lệnh if.
cú pháp.
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.0
Thí dụ. Python nếu tốc ký
Python3
I am Not in if21
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
I am Not in if0
I am Not in if26____82
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.14
I am Not in if4
I am Not in if5____86
I am Not in if32
I am Not in if8
đầu ra
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.1
Câu lệnh if-else tay ngắn
Điều này có thể được sử dụng để viết các câu lệnh if-else trong một dòng khi chỉ có một câu lệnh được thực hiện trong cả khối if và khối lệnh khác.
cú pháp
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.2
Thí dụ. Python if other viết tắt
Python3
I am Not in if34
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.6
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.7
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.8
I am Not in if5
I am Not in if6
I am Not in if40
I am Not in if8
I am Not in if0
I am Not in if26
I am Not in if2
if condition: statement1 statement2 # Here if the condition is true, if block # will consider only statement1 to be inside # its block.13
I am Not in if5
I am Not in if6
I am Not in if48
I am Not in if8