如何在Python里使用if,else,elif
来源:网络收集 点击: 时间:2024-05-16打开python,这里以Jupyter notebook作为示范,新建一个文档。

单单只用if,如下:
a = 5
b = 9
if a b:
print (a is bigger than b)
print(a is less than b)

IF和ELSE一起运用,看起来更容易理解:
c = 6
d = 8
if c d:
print(c is bigger than d)
else:
print(c is less than d)

IF、ELSE和ELIF三个一起使用,可以增加条件:
e = 10
f = 10
if e f:
print(e is bigger than f)
elif e == f:
print(e is equal to f)
else:
print(e is less than f)

e如果比f小:
e = 10
f = 13
if e f:
print(e is bigger than f)
elif e == f:
print(e is equal to f)
else:
print(e is less than f)

e如果比f大:
e = 20
f = 13
if e f:
print(e is bigger than f)
elif e == f:
print(e is equal to f)
else:
print(e is less than f)

else后直接跟着冒号,不加其他
PYTHONIFELSEELIF语句版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_776275.html