python中if的基础用法(if else和if not)

作者:福永~ 时间:2021-02-01 01:56:18 

前言

python 中 if 的用法(if else, if not, elif)

if语句实际上是:if True: …执行后面的语句

python 中的 if 有下面几种常见用法:

  • if … else…

  • if …elif…else…

  • if not …

  • if … not …

1.if … else …

python中if的基础用法(if else和if not)

实际上,还可以用用下面这种方式,使代码更精简

python中if的基础用法(if else和if not)

赋值也是可以的:

python中if的基础用法(if else和if not)

2. if … elif … else…

elif 是多条件判断语句,比如:

python中if的基础用法(if else和if not)

当然,当条件很多时,可以有多个elif,比如上面这个简单的例子可以再增加几个条件

python中if的基础用法(if else和if not)

3.if not …

i在讲 if not 之前,得先弄清楚 not 在python中的意思:

not 是一个逻辑判断词

python中if的基础用法(if else和if not)

当 not 与变量连用的时候:

python中if的基础用法(if else和if not)

所以,在python中,None, False, 空字符串 ‘’ , 0, 空列表[], 空字典{}, 空元组() 都相当于False。

要注意的是,虽然”0“是 False,但是 ”[0]" 是True ,因为只有列表为False,字典也是如此,另外有空格的字符串 ’ ’ 也不算空字符串。

python中if的基础用法(if else和if not)

仔细的同学可能看到上面元组(0)并不是True,那是因为在写元组tupple时,如果只有一个元素,需要在元素后面加上逗号,比如:(0,),指定为元组类型,如果不加逗号,python会把它当成整型,整数 0 为False,所以(0)也为False。

python中if的基础用法(if else和if not)

弄清楚not之后,加上 if 就很简单了,如果if not 后面的语句是False,则执行冒号后面的语句,否则执行else(如果有else的话)。

python中if的基础用法(if else和if not)

注意:有时候if not 的语句很长,又夹带is、and、or,容易理解错误,比如:

if not x is a:

应该理解为if not (x is a) ,而不是if (not x) is a

if not x or a =b:

应该理解为if (not x) or (ab),而不是 if not (x or ab),当然这里a==b可以换成其他条件。or 换成and也是一样,也就是说,(and、or)和is不一样,要仔细甄别。

if not语句是非常常用的语句,尤其在数据结构中。由于python语言的简洁,if not 和and. or. is.连用可以减少大量的代码空间。

4. if … not…

这种情况一般 not 与 is 连用,is not 直接按字面理解即可。理解为 if x (is not) None,而不是if x is (not None)。

python中if的基础用法(if else和if not)

补充:

在实际写代码的时候,经常遇到要判断None的情况,可能会遇见下面这些写法:

if x is None:…#最好使用这种写法
if not x:…
if not x is None: …

而在判断None的过程,常常伴随着 [] 的判断,这时我们使用if not x是有问题的:

python中if的基础用法(if else和if not)

因为上面讲过,not是逻辑判断,而列表、空字典等的逻辑和None是一样的,都是False,if not 是没办法区分的,输出的都是True。所以要确定变量=[]时对if not 的判断没有影响,否则会报错。

使用 if not x is None也是有问题的,这种写法容易误解为if (not x) is None,而实际上应该理解为 not (a is None)

python中if的基础用法(if else和if not)

a is None 返回False ,所以not(False)返回True,而实际上[] 并不是None,应该返回False

所以最好直接使用if x is None,简介明了。

python中if的基础用法(if else和if not)

a is b比较的a 和 b的id,只有a,b的id相同才会输出True:

python中if的基础用法(if else和if not)

上面if … not …中的 is not其实是一样的道理

补充:if条件语句综合练习

判断闰年:

用户输入年份year, 判断是否为闰年?

我们知道:能被4整除但不能被100整除的 或者 能被400整除 那么就是闰年

year = int(input('输入年份:'))
if year %4 ==0 and year %100 != 0:
   print('%d是闰年' %year)
elif year %400 ==0:
   print('%d是闰年'%year)
else:
   print('%d不是闰年' %year)

结果:

python中if的基础用法(if else和if not)

来源:https://blog.csdn.net/F_Y202172/article/details/123997181

标签:else,if,not
0
投稿

猜你喜欢

  • Python中的pprint打印模块

    2023-04-22 12:11:18
  • 在tensorflow中设置使用某一块GPU、多GPU、CPU的操作

    2023-07-22 11:37:28
  • python去除空格,tab制表符和\\n换行符的小技巧分享

    2022-05-12 14:20:39
  • Python argparse库的基本使用步骤

    2023-12-14 08:02:29
  • 解决SQL SERVER 2008数据库表中修改字段后不能保存

    2024-01-18 07:31:10
  • Node.js和PHP根据ip获取地理位置的方法

    2023-11-14 21:23:13
  • 创意方法杂谈

    2009-05-13 12:53:00
  • MySQL向表中添加列方法实例

    2024-01-20 19:08:37
  • Mysql主键和唯一键的区别点总结

    2024-01-15 10:36:36
  • 使用Python防止SQL注入攻击的实现示例

    2022-02-09 05:28:24
  • Python中lambda的用法及其与def的区别解析

    2021-09-22 13:59:03
  • 一文教你用Pyecharts做交互图表

    2022-11-20 22:33:38
  • python列表与列表算法详解

    2022-09-20 20:36:51
  • python自动发送邮件脚本

    2023-01-24 07:56:28
  • asp下实现代码的“运行代码”“复制代码”“保存代码”功能源码

    2011-04-14 10:39:00
  • mysql存储过程学习总结

    2011-07-04 12:03:53
  • linux CentOS6.5 yum安装mysql5.6

    2024-01-28 16:45:25
  • 忘记ftp密码使用python ftplib库暴力破解密码的方法示例

    2021-01-02 03:08:31
  • 数据库表的查询操作(实验二)

    2024-01-25 13:18:59
  • 详解Python命令行解析工具Argparse

    2022-12-11 15:49:28
  • asp之家 网络编程 m.aspxhome.com