5. if判断语句
In [1]: n =input("请输入一个数字:")
请输入一个数字:18
In [2]: n = int(n)
In [3]: type(n)
Out[3]: int
In [4]: if n == 18:
...: print("猜对了!")
...: print("相等")
...:
猜对了!
相等
In [5]: n = 19
In [6]: if n == 19:
...: print("相等")
...: else:
...: print("不相等")
...:
相等
In [7]: if n == 18:
...: print("相等")
...: else:
...: print("不相等")
...:
不相等
In [13]: n = 19
In [14]: if n == 18:
...: print("相等")
...: elif n > 18:
...: print("大了")
...: elif n < 18:
...: print("小了")
...:
大了
In [15]: n = 17
In [16]: if n == 18:
...: print("相等")
...: elif n > 18:
...: print("大了")
...: elif n < 18:
...: print("小了")
...:
小了
else
In [19]: if 19 == 18:
...: print("相等") #缩进四个空格为标准写法
...: elif 17 > 18:
...: print("大了")
...: elif 19 < 18:
...: print("小了")
...: else:
...: print("以上条件都没满足")
...:
以上条件都没满足
In [25]: n = ''
In [26]: if n:
...: print("ok")
...: else:
...: print("no")
...:
no
# '' False [] () {} None 为假的
本文摘自 :https://blog.51cto.com/v