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

30次阅读

共计 758 个字符,预计需要花费 2 分钟才能阅读完成。

  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 技术大本营,获取更多技能与教程。

正文完
 0