Nếu các câu lệnh trong Python thường thấy một giá trị liên quan đến một giá trị khác như thế nào. Nhưng làm thế nào để chúng ta biến những so sánh đó thành mã? Hãy cùng tìm hiểu.
Trong bài viết này:
- Các nhà khai thác Python sườn thực hiện nếu điều kiện tuyên bố
- Nếu bằng kiểm tra bằng Python:
4 vớiage = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
5age = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
- Nếu tuyên bố kiểm tra sự bình đẳng
- Nếu/tuyên bố khác đánh giá sự bình đẳng
- Nếu không bằng trong Python:
4 kết hợp vớiage = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
7age = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
- Nếu tuyên bố tìm kiếm sự bất bình đẳng
- Nếu/tuyên bố khác đánh giá khác với
- Nếu lớn hơn thử nghiệm trong Python:
4 kết hợp vớiage = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
9age = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
- Nếu tuyên bố tìm kiếm lớn hơn
- Nếu/tuyên bố khác đánh giá lớn hơn
- Nếu ít hơn kiểm tra trong Python:
4 cùng vớiage = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
1You may be too young to drive, or already older than 18.
- Nếu tuyên bố đánh giá nhỏ hơn
- Nếu/tuyên bố khác xử lý ít hơn
- Nếu lớn hơn hoặc bằng thử nghiệm trong Python:
4 vàage = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
3You may be too young to drive, or already older than 18.
- Nếu tuyên bố đánh giá lớn hơn hoặc bằng
- Nếu/tuyên bố khác xử lý lớn hơn hoặc bằng
- Nếu nhỏ hơn hoặc bằng thử nghiệm trong Python:
4 vớiage = 22 # See if the person is 18 or not if age == 18: print["You're old enough to drive!"] else: print["You may be too young to drive, " + "or already older than 18."]
5You may be too young to drive, or already older than 18.
- Nếu câu lệnh xử lý ít hơn hoặc bằng
- Nếu/câu lệnh khác có ít hơn hoặc bằng với điều kiện
- Các tùy chọn mã đúng/sai hơn với các câu lệnh IF
- Bản tóm tắt
# Các nhà khai thác Python, thực hiện nếu điều kiện tuyên bố
Như bạn đã biết, một câu lệnh IF thực thi mã của nó bất cứ khi nào mệnh đề
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 kiểm tra You may be too young to drive, or already older than 18.
7. Nếu chúng ta có một câu lệnh IF/ELSE, thì mệnh đề You may be too young to drive, or already older than 18.
8 chạy khi điều kiện kiểm tra You may be too young to drive, or already older than 18.
9. Hành vi này không yêu cầu điều kiện age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 của chúng tôi là một giá trị You may be too young to drive, or already older than 18.
7 hoặc You may be too young to drive, or already older than 18.
9. Nhưng hầu hết thời gian mã của chúng tôi chỉ cần kiểm tra giá trị biến biến, như để xem priceMoney = 125
# Look if the price money differs from 100
if priceMoney != 100:
print["You didn't win the $100 price!"]
3 có ở trên 18. Làm thế nào để chúng ta có được giá trị đúng/sai sau đó không?Để có được một giá trị đúng/sai từ khi mã của chúng tôi so sánh, chúng tôi phải sử dụng một toán tử được gọi là so sánh. Những toán tử đó liên quan [như trong, so sánh] một giá trị với một giá trị khác [như
priceMoney = 125
# Look if the price money differs from 100
if priceMoney != 100:
print["You didn't win the $100 price!"]
4].Dựa trên kết quả của sự so sánh đó, chúng tôi nhận được một giá trị
You may be too young to drive, or already older than 18.
7 hoặc You may be too young to drive, or already older than 18.
9 duy nhất là kết quả [Sweigart, 2015]. Thông tin đó sau đó là một cái gì đó chúng ta có thể sử dụng với việc ra quyết định tuyên bố IF của chúng tôi.Python có các nhà khai thác so sánh này [Python Docs, N.D .; Sweigart, 2015]:
Bình đẳng | 5 | 8 |
Không công bằng | 7 | 0 |
Lớn hơn | 9 | 2 |
Ít hơn | 1 | 4 |
Lớn hơn hoặc bằng | 3 | 6 |
Ít hơn hoặc bằng | 5 | 8 |
Hãy cùng xem cách chúng ta sử dụng từng nhà khai thác này với các quyết định tuyên bố.
# Nếu bằng kiểm tra bằng Python: age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 với age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
5
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
Các thử nghiệm toán tử bằng [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
5] cho sự bình đẳng. Nó trả về You may be too young to drive, or already older than 18.
7 khi cả hai giá trị được kiểm tra đều giống nhau. Khi giá trị của chúng khác nhau, toán tử trả về You may be too young to drive, or already older than 18.
9. Bằng cách này, nếu các câu lệnh của chúng tôi có thể kiểm tra các tình huống cụ thể, chẳng hạn như một biến có một giá trị nhất định.# Nếu tuyên bố kiểm tra sự bình đẳng
Đây là cách một python nếu câu lệnh kiểm tra cho sự bình đẳng:
age = 18
# See if the age variable equals 18
if age == 18:
print["You're old enough to vote!"]
Trước tiên chúng tôi thực hiện biến
priceMoney = 125
# Look if the price money differs from 100
if priceMoney != 100:
print["You didn't win the $100 price!"]
3 ở đây, mà chúng tôi đưa ra giá trị favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
5. Sau đó, một câu lệnh IF có vẻ nếu biến đó thực sự bằng [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
5] 18. vì nó có, mã IF Statement Runs chạy. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 đầu ra một thông báo:You're old enough to vote!
# Nếu/khác tuyên bố đánh giá sự bình đẳng
Khi một câu lệnh IF/other kiểm tra cho sự bình đẳng, thì cũng có một đường dẫn mã chạy khi các giá trị được kiểm tra được kiểm tra không giống nhau. Đây là một ví dụ:
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
Ở đây chúng tôi thực hiện biến
priceMoney = 125
# Look if the price money differs from 100
if priceMoney != 100:
print["You didn't win the $100 price!"]
3 và đặt giá trị của nó thành 22. Sau đó, câu lệnh IF/ELSE kiểm tra biến đó. Mệnh đề age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 có vẻ nếu biến đó bằng [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
5] favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
5. Bởi vì các thử nghiệm đó You may be too young to drive, or already older than 18.
9, mã You may be too young to drive, or already older than 18.
8 chạy. Có chức năng favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 cho biết người dùng trẻ hơn hoặc lớn hơn 18:You may be too young to drive, or already older than 18.
# Nếu không bằng trong Python: age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 kết hợp với age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
Toán tử không đồng đều với [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7] toán tử đánh giá nếu hai giá trị khác nhau. Khi họ, sự so sánh đó trở lại You may be too young to drive, or already older than 18.
7. Chỉ khi chúng giống nhau, chúng ta sẽ nhận được giá trị You may be too young to drive, or already older than 18.
9. Bằng cách đó nếu các câu lệnh có thể kiểm tra sự khác biệt giữa các giá trị.
# Nếu tuyên bố tìm kiếm sự bất bình đẳng
Đây là cách một câu lệnh IF kiểm tra sự bất bình đẳng với
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7:priceMoney = 125
# Look if the price money differs from 100
if priceMoney != 100:
print["You didn't win the $100 price!"]
Ở đây chúng tôi cung cấp cho biến
answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
1 một giá trị là answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
2. Sau đó, một câu lệnh IF đánh giá nếu biến đó khác với [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7] answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
4. Bởi vì nó là, chức năng favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng chúng tôi đã giành được giá 100 đô la. Xin lưu ý rằng chúng tôi vẫn có thể giành được một mức giá khác; Mã age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 của chúng tôi chỉ kiểm tra giá 100 đô la.You didn't win the $100 price!
# Nếu/câu lệnh khác đánh giá khác với
Khi một câu lệnh IF/other kiểm tra sự bất bình đẳng [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7], thì mã You may be too young to drive, or already older than 18.
8 chạy khi hai giá trị được kiểm tra hoàn toàn giống nhau. Ví dụ:favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
Biến
answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
9 mà chúng tôi thực hiện ở đây giữ giá trị Passed! More than 50% correct!
0. Sau đó, chúng tôi thực hiện một tuyên bố nếu/khác. Phần age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 có xem liệu biến answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
9 có khác với [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7] Passed! More than 50% correct!
0 hay không. Điều đó kiểm tra You may be too young to drive, or already older than 18.
9, tuy nhiên.Vì vậy, mã
You may be too young to drive, or already older than 18.
8 chạy. Ở đó chúng tôi chúc người chơi may mắn với Fortnite. Chúng ta có thể rút ra kết luận đó vì phần You may be too young to drive, or already older than 18.
8 chỉ chạy khi answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
9 bằng Passed! More than 50% correct!
0.Good luck on the Fortnite battleground!
# Nếu lớn hơn kiểm tra trong Python: age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 kết hợp với age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
Toán tử lớn hơn [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9] trả về You may be too young to drive, or already older than 18.
7 khi giá trị bên trái của nó lớn hơn giá trị bên phải của nó. Khi giá trị bên trái nhỏ hơn hoặc khi chúng bằng nhau, thì age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9 trả về You may be too young to drive, or already older than 18.
9. Bằng cách đó, nếu các câu lệnh thực thi mã khi một giá trị cao hơn một số ngưỡng.# Nếu câu lệnh tìm kiếm lớn hơn
Để tạo mã thực thi câu lệnh IF khi một số giá trị lớn hơn một giá trị khác, chúng tôi thực hiện kiểm tra với
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9:answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
Chúng tôi tạo hai biến ở đây,
You're old enough to vote!
07 và You're old enough to vote!
08. Với câu lệnh IF, chúng tôi xem nếu các câu trả lời đúng là ở trên [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9] 50%. Đối với điều đó là You may be too young to drive, or already older than 18.
7, You're old enough to vote!
07 cần phải là hơn một nửa You're old enough to vote!
08.Vì đó là trường hợp, câu lệnh IF thực thi hàm
favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7. Điều đó cho thấy người đó đã qua:Passed! More than 50% correct!
# Nếu/tuyên bố khác đánh giá lớn hơn
Chúng ta cũng có thể đặt một mệnh đề
You may be too young to drive, or already older than 18.
8 sau khi thử nghiệm age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 sử dụng toán tử lớn hơn [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9]. Điều đó làm cho mã You may be too young to drive, or already older than 18.
8 chạy bất cứ khi nào các giá trị giống nhau hoặc khi giá trị đầu tiên nhỏ hơn so với thứ hai. Đây là cách mà một câu lệnh IF/ELSE như vậy trông như vậy:You're old enough to vote!
0Trước tiên chúng tôi lập một danh sách [
You're old enough to vote!
18] với điểm số của hai đội 2 người. Sau đó, chúng tôi thực hiện một tuyên bố nếu/khác. Phần age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 có xem liệu đội thứ nhất [You're old enough to vote!
20] đã ghi được nhiều hơn [age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9] so với đội thứ hai [You're old enough to vote!
22]. Bởi vì điều đó không phải là trường hợp, bài kiểm tra trả về You may be too young to drive, or already older than 18.
9.Điều đó có nghĩa là mã
You may be too young to drive, or already older than 18.
8 chạy. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng một trong hai đội 2 won hoặc cả hai đội có cùng số điểm:You're old enough to vote!
1# Nếu ít hơn kiểm tra trong Python: age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 cùng với You may be too young to drive, or already older than 18.
1
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
You may be too young to drive, or already older than 18.
Với toán tử nhỏ hơn [
You may be too young to drive, or already older than 18.
1], chúng tôi thấy nếu một giá trị nhỏ hơn một giá trị khác. Nếu giá trị đầu tiên thực sự nhỏ hơn, You may be too young to drive, or already older than 18.
1 trả về You may be too young to drive, or already older than 18.
7. Khi giá trị đầu tiên bằng hoặc lớn hơn so với thứ hai, toán tử đó sẽ trả về You may be too young to drive, or already older than 18.
9. Khi câu lệnh IF của chúng tôi sử dụng toán tử You may be too young to drive, or already older than 18.
1, thì mã của chúng tôi chỉ thực thi khi một số giá trị dưới ngưỡng.# Nếu tuyên bố đánh giá nhỏ hơn
Để thực thi mã khi một số giá trị thấp hơn một giá trị khác, chúng tôi sẽ sử dụng câu lệnh IF
You may be too young to drive, or already older than 18.
1 như vậy:You're old enough to vote!
2Ở đây trước tiên chúng tôi thực hiện biến
You're old enough to vote!
34. Sau đó, một câu lệnh IF kiểm tra xem biến đó có nằm dưới [You may be too young to drive, or already older than 18.
1] 100.000 không. Bởi vì nó là, điều kiện đó kiểm tra You may be too young to drive, or already older than 18.
7. Và do đó, mã câu lệnh IF chạy. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 cho biết số lần xem trang còn lại:You're old enough to vote!
3# Nếu/câu lệnh khác xử lý ít hơn
Khi một câu lệnh IF/other kiểm tra thấp hơn [
You may be too young to drive, or already older than 18.
1], thì mã You may be too young to drive, or already older than 18.
8 của nó chạy khi giá trị đầu tiên lớn hơn hoặc bằng thứ hai. Đây là cách mà trông trong mã Python:You're old enough to vote!
4Đầu tiên chúng tôi thực hiện biến
You're old enough to vote!
40 ở đây. Sau đó, phần age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 của câu lệnh if/other trông nếu biến đó nằm dưới [You may be too young to drive, or already older than 18.
1] 500. Bởi vì nó không phải là mã You may be too young to drive, or already older than 18.
8 chạy. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng chúng tôi đã viết nhiều từ hơn mục tiêu hàng ngày của chúng tôi:You're old enough to vote!
5# Nếu lớn hơn hoặc bằng thử nghiệm trong Python: age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 và You may be too young to drive, or already older than 18.
3
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
You may be too young to drive, or already older than 18.
Với toán tử Python từ
You may be too young to drive, or already older than 18.
3, chúng tôi thấy một số giá trị lớn hơn hoặc bằng với giá trị khác. Khi đó, toán tử đó trả về You may be too young to drive, or already older than 18.
7. Nếu giá trị đầu tiên nhỏ hơn thứ hai, chúng tôi nhận được giá trị You may be too young to drive, or already older than 18.
9 thay thế. Bằng cách đó nếu các câu lệnh xem xét nếu một số giá trị ở hoặc trên một số giá trị biên.# Nếu câu lệnh đánh giá lớn hơn hoặc bằng
Ở đây, cách mà một câu lệnh IF có thể sử dụng
You may be too young to drive, or already older than 18.
3 để xem một số giá trị có lớn hơn hay bằng với bản khác không:You're old enough to vote!
6Trước tiên chúng tôi thực hiện biến
You're old enough to vote!
51 ở đây. Câu lệnh IF sau đó trông nếu giá trị biến đó lớn hơn hoặc bằng [You may be too young to drive, or already older than 18.
3] You're old enough to vote!
53. Bởi vì các thử nghiệm đó You may be too young to drive, or already older than 18.
7, mã IF Statement Runs. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng thời gian vòng không phải là một bản ghi mới:You're old enough to vote!
7# Nếu/câu lệnh khác xử lý lớn hơn hoặc bằng
Khi một thử nghiệm
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 sử dụng toán tử You may be too young to drive, or already older than 18.
3 cũng có mệnh đề You may be too young to drive, or already older than 18.
8, thì mã You may be too young to drive, or already older than 18.
8 chạy mỗi lần giá trị đầu tiên nhỏ hơn thứ hai. Ở đây, cách thức một câu lệnh IF/ELSE có thể thực hiện hai đường dẫn mã khác nhau đó:You're old enough to vote!
8Trước tiên chúng tôi thực hiện biến
You're old enough to vote!
60. Câu lệnh if/other sau đó trông nếu giá trị biến đó lớn hơn hoặc bằng [You may be too young to drive, or already older than 18.
3] 225. Bởi vì các thử nghiệm đó You may be too young to drive, or already older than 18.
9, mã You may be too young to drive, or already older than 18.
8 chạy. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng lượng calo nằm dưới mức tối đa cho phép:You're old enough to vote!
9# Nếu nhỏ hơn hoặc bằng kiểm tra trong Python: age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 với You may be too young to drive, or already older than 18.
5
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
You may be too young to drive, or already older than 18.
Với toán tử
You may be too young to drive, or already older than 18.
5, chúng tôi thấy một số giá trị nhỏ hơn hoặc bằng với giá trị khác. Khi trường hợp đó, nhà điều hành trả về You may be too young to drive, or already older than 18.
7. Nếu giá trị đầu tiên lớn hơn so với thứ hai, thì so sánh đó sẽ trả về You may be too young to drive, or already older than 18.
9. Bằng cách này nếu các câu lệnh có thể xem nếu một số giá trị nằm dưới mức tối đa.# Nếu câu lệnh xử lý ít hơn hoặc bằng
Ở đây, cách mà một câu lệnh IF cơ bản có thể xem một giá trị có nhỏ hơn hoặc bằng [
You may be too young to drive, or already older than 18.
5] khác không:age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
0Ở đây trước tiên chúng tôi lập một danh sách [
You're old enough to vote!
71]. Giá trị của nó là giá của ba đôi giày. Sau đó, một câu lệnh IF xem nếu danh sách giá trị đầu tiên [You're old enough to vote!
72] nhỏ hơn hoặc bằng [You may be too young to drive, or already older than 18.
5] answersRight = 12
totalAnswers = 20
# See if more than 50% is correct
if answersRight > totalAnswers * 0.5:
print["Passed! More than 50% correct!"]
4. Vì những đôi giày đó chỉ là 89,90 đô la, điều kiện đó kiểm tra You may be too young to drive, or already older than 18.
7. Và vì vậy, mã IF tuyên bố mã thực thi. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng chúng ta có thể mua những đôi giày đó:age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
1# Nếu/câu lệnh khác với điều kiện nhỏ hơn hoặc bằng
Khi một câu lệnh IF/other kiểm tra thấp hơn hoặc bằng [
You may be too young to drive, or already older than 18.
5], thì mã ____38 của nó chạy bất cứ khi nào giá trị được kiểm tra đầu tiên lớn hơn giá trị thứ hai. Ở đây, một ví dụ về mã Python về điều đó:age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
2Đầu tiên chúng tôi thực hiện hai biến,
You're old enough to vote!
79 và You're old enough to vote!
80. Giá trị của chúng là You're old enough to vote!
81 và You're old enough to vote!
82. Sau đó, một câu lệnh IF/ELSE trông nếu You're old enough to vote!
80 nhỏ hơn hoặc bằng [You may be too young to drive, or already older than 18.
5] You're old enough to vote!
79. Bởi vì các thử nghiệm so sánh đó You may be too young to drive, or already older than 18.
9, mã You may be too young to drive, or already older than 18.
8 chạy. Có favGame = "fortnite"
# Look if favourite game is something else than Fortnite
if favGame != "fortnite":
print["Cartoony graphics not your thing?"]
else:
print["Good luck on the Fortnite battleground!"]
7 nói rằng người đó quá cao đối với tàu lượn siêu tốc:age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
3# Các tùy chọn mã đúng/sai hơn với các câu lệnh IF
Bên cạnh việc so sánh một giá trị với một giá trị khác, chúng ta cũng có thể mã hóa các điều kiện tuyên bố theo những cách khác:
- Với toán tử
89 nếu các câu lệnh kiểm tra câu lệnh đối diện logic. Bằng cách đó, mã của chúng tôi chạy khi một cái gì đó không đúng.You're old enough to vote!
- Nhờ vào nhà điều hành
90 nếu các tuyên bố có thể thực hiện các bài kiểm tra thành viên. Đó là cách mà chúng ta xem nếu một số giá trị cụ thể xuất hiện trong danh sách, từ điển hoặc chuỗi.You're old enough to vote!
- Và chúng ta có thể kiểm tra nhiều điều kiện với một câu lệnh IF. Chúng tôi thực hiện điều đó với các toán tử
91 vàYou're old enough to vote!
92.You're old enough to vote!
Xem tất cả các bài viết tuyên bố để tìm hiểu thêm về các tính năng của Python,
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
4 và You may be too young to drive, or already older than 18.
8.# Bản tóm tắt
Một câu lệnh IF thực thi mã của nó ngay khi điều kiện của nó kiểm tra
You may be too young to drive, or already older than 18.
7. Hầu hết thời gian những điều kiện đó so sánh một giá trị với một giá trị khác. Python có một số toán tử so sánh biến mối quan hệ giữa hai giá trị thành giá trị You may be too young to drive, or already older than 18.
7 hoặc You may be too young to drive, or already older than 18.
9.comparison operators that turn a relationship between two values into a You may be too young to drive, or already older than 18.
7 or You may be too young to drive, or already older than 18.
9 value.Toán tử bình đẳng [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
5] kiểm tra xem biểu thức trái và bên phải có cùng giá trị không. Khi họ có, bài kiểm tra đó trả về You may be too young to drive, or already older than 18.
7. Nếu họ khác nhau, kết quả là You may be too young to drive, or already older than 18.
9.Toán tử bất bình đẳng [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
7] nhìn thấy nếu hai biểu thức không giống nhau. Trong trường hợp đó, nhà điều hành trả về You may be too young to drive, or already older than 18.
7. Khi các giá trị giống nhau, kết quả là You may be too young to drive, or already older than 18.
9,Với các toán tử lớn hơn [
age = 22
# See if the person is 18 or not
if age == 18:
print["You're old enough to drive!"]
else:
print["You may be too young to drive, " +
"or already older than 18."]
9] và nhỏ hơn [You may be too young to drive, or already older than 18.
1], chúng ta nhìn nếu một giá trị lớn hơn hoặc nhỏ hơn giá trị khác. Khi trường hợp đó, các thử nghiệm đó trở lại You may be too young to drive, or already older than 18.
7 [và You may be too young to drive, or already older than 18.
9 nếu không].Tương tự, toán tử lớn hơn hoặc bằng [
You may be too young to drive, or already older than 18.
3] trông với giá trị lớn hơn hoặc giống nhau hơn một số giá trị khác. Và với ít hơn hoặc bằng [You may be too young to drive, or already older than 18.
5], chúng ta thấy nếu một số giá trị nhỏ hơn hoặc bằng với giá trị khác.Người giới thiệu
Python.org [N.D.]. Biểu thức. Truy cập vào ngày 5 tháng 8 năm 2019, từ //docs.python.org/3/reference/expressions.html
Sweigart, A. [2015]. Tự động hóa những thứ nhàm chán với Python: Lập trình thực tế cho toàn bộ người mới bắt đầu. San Francisco, CA: Không có báo chí tinh bột.
Xuất bản ngày 6 tháng 9 năm 2019.
«Tất cả Python nếu/bài viết khác