Python condition judgment (if, else, elif)
if
age = 15
if age <= 18:
print('爬爬爬,未成年还上网吧!')
According to Python's indentation rules, if it is true
, then the indented code will be executed, otherwise nothing will happen.
if
Can also be abbreviated.
if x:
print('True')
As long as it x
is a non-zero value, a non-empty string, a non-empty list, etc., it is judged as True
otherwise it is False
.
else
age = 22
if age >= 18:
print('二号机,有问题电脑上面联系网管')
else:
print('又是你')
print('爬爬爬,还搁着想上网呢')
else remember to add a colon:
elif
elif
Can be used to make more detailed classification judgments
age = 18
if age >= 18:
print('15号机,有问题电脑上面联系网管')
elif age >= 6:
print('啊这....')
else:
print('爬')
elif
Is else if
the abbreviation
if <条件判断1>:
<执行1>
elif <条件判断2>:
<执行2>
elif <条件判断3>:
<执行3>
else:
<执行4>