【每天1分钟】PYTHON基础之条件与循环(if语句)
- if
if语句的判断以冒号结尾,有冒号的地方下一行一定有缩进。
如果if判断通过了,那么就会执行if语句后面所有缩进的代码行,否则将忽略他们。
>>> score = 60
>>> if score >= 60:
print('ok!')
ok!
>>> if score < 60:
print('not ok!')
>>>
- if-else
当if语句没有通过时,就需要执行另一个操作
else语句指定条件未通过时要执行的操作
>>> score = 60
>>> if score >= 60:
print('ok!')
else:
print('not ok!')
ok!
>>>
- if-elif-else
当要执行多个判断语句时,就需要用elif形式来判断
>>> score = 60
>>> if score >= 90:
print('excellent!')
elif score >= 80:
print('good!')
elif score >= 60:
print('not bad!')
else:
print('bad!')
not bad!
>>>