[Python study notes] python condition judgment

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.

ifCan also be abbreviated.

if x:
    print('True')

As long as it xis a non-zero value, a non-empty string, a non-empty list, etc., it is judged as Trueotherwise it is False.


else

age = 22
if age >= 18:
    print('二号机,有问题电脑上面联系网管')
else:
    print('又是你')
    print('爬爬爬,还搁着想上网呢')

else remember to add a colon:


elif

elifCan be used to make more detailed classification judgments

age = 18
if age >= 18:
    print('15号机,有问题电脑上面联系网管')
elif age >= 6: 
    print('啊这....')
else:
    print('爬')

elifIs else ifthe abbreviation

if <条件判断1>:
    <执行1>
elif <条件判断2>:
    <执行2>
elif <条件判断3>:
    <执行3>
else:
    <执行4>

Published 18 original articles · Likes6 · Visits 1859

Guess you like

Origin blog.csdn.net/qq_43479203/article/details/105454616