Introduction
Comparing to languages like C, Python's IF statement might be the simplest: There isn't even a single bracket in the statement.
The logical architecture is much simpler:
- ==, >=, <=, <, > comparison operators
- Manipulation of characters, numbers, and letters
- 'and' with 'or' multi-conditional comparison judgments
# and or judgement. (Can't use && and || operators in Python)
age_0 = 23
age_1 = 17
print(age_0 >= 21 and age_1 >= 21)
print(age_0 >= 21 or age_1 >= 21)
Output
False
True
'in' and 'not in' for list checking
topping = ['mushroom', 'pineapple']
if 'mushroom' in topping:
print('OK')
else:
print('No mushroom')
Output
OK
However, if you want to check whether an element is in an array in language like C (propose a number similar structure), this will be an extremely complicated operation to realize, because it's necessary to start from the data structure and determine the element type. Thus the judgment operation is a lot more complicated.
Multiple elif blocks
age = 8
if age < 3:
price = 0
elif age < 12:
price = 3
elif age < 18:
price = 5
else:
price = 10
print(price)
Output
3
Using the combination of list and 'if' operator to select
toppings = ['mushrooms', 'green peppers', 'onion', 'cheese', 'pineapple']
for topping in toppings:
if topping == 'green peppers':
print("No more green peppers.")
else:
print("OK, adding " + topping + ".")
print("Here is your pizza!")
Output
OK, adding mushrooms.
No more green peppers.
OK, adding onion.
OK, adding cheese.
OK, adding pineapple.
Here is your pizza!
Q: Does the list support value re-assignment operations in tuples (immutable after value assignment)?
A: The list is used internally in the tuple, although the tuple is immutable, but we can still mutate the list inside the tuple.
How to determine if the list is 'NULL'
From the perspective of the code, there are many ways to detect whether the list is 'NULL' or 'EMPTY', here are some examples such as the direct detection (if operation) and length detection (if len() is 0):
# Method 1: Using if else
topping = []
if topping:
print('Non-empty.')
else:
print('Empty.')
# Method2: Using len(LIST)
if len(topping) == 0:
print('Empty.')
Comments 19 comments
Blogger 125915708
The comment is private
Blogger 48752187218
https://go88-tài-xỉu.net
https://8us.my
Blogger Latonya Halsey
The comment is private
Blogger hello88
Hello88 la trong nhung thuong hieu ca cuoc truc tuyen hap dan duoc nhieu cuoc thu tin tuong va lua chon hien nay. Voi nguon goc tu chau A, Hello88 da thau hieu nhu cau, mong muon cua nguoi choi tu do co cho dung vung vang tren thi truong. De hieu sau hon ve cac san choi dinh dam nay cung diem qua noi dung thong qua bai viet duoi day. https://hello88-vn.com/
Blogger car diagnostic tools
Cardiagtech is your go-to resource for all things automotive diagnostics. We offer top-quality car diagnostic tools, including car scan tools and car code readers, along with comprehensive car repair manuals .
Blogger hizmet robotu
nice
Blogger eyebrow transplant turkey
awesome
Blogger haartransplantation fue
nice
Blogger Louise Jenner
The comment is private
Blogger Cathern McInnes
The comment is private
Blogger Cookie Clicker Unblocked
Cookie Clicker Unblocked at Cookieclickerunblocked.co is an exciting and addictive game that’s perfect for passing the time. Whether you’re a casual player or someone who enjoys strategy and idle games, Cookie Clicker offers a delightful experience that will keep you hooked for hours. Website: https://cookieclickerunblocked.co/
Blogger ovounblocked.me
Are you ready for a thrilling new challenge? Visit ovounblocked.me to experience the upgraded version of ovounblocked.me This latest update brings enhanced gameplay, smoother controls, and exciting new features, making it the ultimate destination for Ovo fans. Website: https://ovounblocked.me/
Blogger Run 3
The comment is private
Blogger shidefc.com
Chao mung ban den voi shidefc.com – diem den hang dau danh cho nhung ai dam me ca cuoc va giai tri truc tuyen!
Blogger QH88
The comment is private
Blogger Run3.biz
The comment is private
Blogger roy79 club
The comment is private
Blogger roy79 club
The comment is private
Blogger tải game sao79
Sao79 Club or Sao Win is a reputable card game portal with more than 500,000 downloads by 2024. Enjoy playing card games, Sic Bo, coin toss, fish shooting, lottery… Sao79 allows cross-platform transactions via Momo Pay, scanned bank codes, transfer, Zalo Pay, scratch cards… along with quick withdrawals to bank accounts, ATM cards. https://sao79.vip/