博客
关于我
python中if语句基础与小技巧
阅读量:332 次
发布时间:2019-03-04

本文共 499 字,大约阅读时间需要 1 分钟。

目录

python中条件语句的三种结构:

结构一:

# if structureif True:	print("True")

结构二:

# if-else structureif True:	print("True")else:	print("False")

结构三:

# if-elif-else structure if True:	print("True")elif True:	print("Still True")else:	print("False")

小技巧

但是,实际上当我们使用if-elif-else语句结构时, elif等同于else: if, 如下所示,因此在elif后面紧跟的else语句可以去掉,并不一定要出现,因为if语句中存在if的单独判断结构。

elif condition:else:else:	if condition:	# else:

赋值时的特殊用法

比如我们需要对变量a进行赋值,想要在b大于5的时令a=5,在b<=6的时候令a=0

# 传统写法if b > 5:	a = 5else:	a = 0# 特殊写法a = 5 if b > 5 else 0

转载地址:http://opuh.baihongyu.com/

你可能感兴趣的文章
MySQL - 解读MySQL事务与锁机制
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>