乐趣区

关于python:Python自动化-if判断语句

  1. 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  为假的

以上就是本次分享的全部内容,当初想要学习编程的小伙伴欢送关注 Python 技术大本营,获取更多技能与教程。

退出移动版