当前位置: 首页 > news >正文

综合类网站怎么做asp网站开发软件

综合类网站怎么做,asp网站开发软件,什么网站做禽苗的多,两个人能用的一个公司做网站吗本文基于python3。 目录 1、三目运算语句的定义2、三目运算语句:包含逻辑运算符 (and、or、not)1、 包含 and2、包含 or3、包含 not4、包含 and、or、not 3、三目运算语句:使用多个if ... else ...形式4、三目运算语句:在列表(li…

本文基于python3。

目录

  • 1、三目运算语句的定义
  • 2、三目运算语句:包含逻辑运算符 (and、or、not)
    • 1、 包含 and
    • 2、包含 or
    • 3、包含 not
    • 4、包含 and、or、not
  • 3、三目运算语句:使用多个if ... else ...形式
  • 4、三目运算语句:在列表(list)中使用

1、三目运算语句的定义

先看一下三目运算语句的定义。

当条件为真时计算的表达式   if   条件   else    当条件为假时计算的表达式

直接看实例:

a = 1
result = 'AAA' if a % 2 == 0 else 'BBB'
print(result)    # BBBa = 2
result = 'AAA' if a % 2 == 0 else 'BBB'
print(result)    # AAA

再简写就是这样:

a = 1
print('AAA') if a % 2 == 0 else print('BBB')     # BBBa = 2
print('AAA') if a % 2 == 0 else print('BBB')     # AAA

三目运算符其实就等价于if语句。
因此上面的语句等价于下面这个if 语句:

a = 1if a % 2 == 0:print('AAA')
else:print('BBB')

在if条件语句中条件里可以存在 and、or、not。
那么同样的,三目运算语句中条件也可以存在 and、or、not。

2、三目运算语句:包含逻辑运算符 (and、or、not)

直接看实例:

1、 包含 and

a = -2
print('AAA') if a < 0 and a % 2 == 0 else print('BBB')     # AAAa = -1
print('AAA') if a < 0 and a % 2 == 0 else print('BBB')     # BBB

就等价于下面这个if 语句:

a = -1if a < 0 and a % 2 == 0:print('AAA')
else:print('BBB')

2、包含 or

a = 1
result = 'AAA' if a > 0 or a % 2 == 0 else 'BBB'
print(result)    # AAAa = -1
result = 'AAA' if a > 0 or a % 2 == 0 else 'BBB'
print(result)    # BBB

就等价于下面这个if 语句:

a = -1if a > 0 or a % 2 == 0:print('AAA')
else:print('BBB')

3、包含 not

a = -1
result = 'AAA' if not(a > 0) or a % 2 == 0 else 'BBB'
print(result)    # AAAa = 2
result = 'AAA' if not(a > 0) or a % 3 == 0 else 'BBB'
print(result)    # BBB

就等价于下面这个if 语句:

a = -1if not(a > 0) or a % 2 == 0:print('AAA')
else:print('BBB')

4、包含 and、or、not

a = -2
result = 'AAA' if not(a > 0) and a % 2 == 0 or a == -1 else 'BBB'
print(result)    # AAAa = 2
result = 'AAA' if not(a > 0) and a % 2 == 0 or a == -1 else 'BBB'
print(result)    # BBB

就等价于下面这个if 语句:

a = -1if not(a > 0) and a % 2 == 0 or a == -1:print('AAA')
else:print('BBB')

3、三目运算语句:使用多个if … else …形式

使用多个if … else …形式的语句也可以用三目运算语句写出来。

a = 2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# positivea = 0
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# zeroa = -2
result = 'negative' if a < 0 else 'positive' if a > 0 else 'zero'
print(result)
# negative

就等价于下面这个if 语句:

if a < 0:print('negative')
else:if a > 0:print('positive')else:print('zero')

可以看到,可读性很差。请不要大量使用。
不过如果能配合小括号得话,应该会好很多。不过还是不推荐。

a = -2
result = 'negative' if a < 0 else ('positive' if a > 0 else 'zero')
print(result)
# negative

4、三目运算语句:在列表(list)中使用

三目运算语句是可以在列表中使用的。
直接看实例:

TestList = [i * 10 if i % 2 == 0 else i for i in range(10)]
print(TestList)
# [0, 1, 20, 3, 40, 5, 60, 7, 80, 9]

看起来有点儿复杂,我们先换成正常的语句,你一看就懂。

TestList = []
for i in range(10):if i % 2 == 0:TestList.append(i * 10)else:TestList.append(i)
print(TestList)
# [0, 1, 20, 3, 40, 5, 60, 7, 80, 9]

用这种方法用于处理列表的奇偶位置的数据不同的时候,会方便。
具体的原理其实利用的是Python中的:列表推导式(generator expression)
详情请参考:列表推导式

以上就是三目运算语句的用法。

如果有问题,请指出,或者copy了谁的,也请指出。
会及时更正。

http://www.yayakq.cn/news/743276/

相关文章:

  • 做演讲和做演讲视频网站各大设计网站
  • 中国风网站表现设计定制型网站建设
  • 长春建设银行网站关于做书的网站
  • 网站开源程序关键词排名优化到首页
  • 青岛外贸假发网站建设phpcms对比wordpress
  • 定制网站开发冬天里的白玫瑰godaddy wordpress托管
  • 网站设计网站项目流程北京网站优化效果怎样
  • 做网站php与python旅游网站开发参考文献
  • 手机网站制作流程情侣主题 wordpress
  • 有源码怎么在本地网站搭建怎么在百度上打广告
  • 做外贸的网站有那些网站建设价格对比单
  • 桂林景区网站建设策划方案关于配色的网站
  • 导师让做网站快速微信网站建设
  • 佛山网站建设公司哪专业系统优化的影响因素
  • 郑州做网站电话湖南做旅游网站
  • 公司网站兰州建设需要多少钱教人做甜品的网站
  • 在线网站建设工程标准电商网站平台有哪些
  • 做网站服务器应该怎么配置百度指数怎么看
  • 织梦网站最新漏洞入侵东台建设局官方网站
  • ppt网站建设答案minisite网站案例
  • 昆明网站建设论坛做网站后的收获
  • 公司创建一个网站多少钱广东省城乡和建设厅网站
  • 网站空间在哪买网站备案和空间备案
  • 建设政务门户网站的基本意义旅游网站系统wordpress
  • 太原市住房和城乡建设部网站微信开发什么时候好
  • 企业网站建设讲解黄山市网站建设
  • 免费手机网站源码下载做自媒体可以搬运国外网站新闻吗
  • 郫县网站制作外贸是做什么的学什么专业
  • 西安建站免费模板手机app开发需要什么技术
  • 中小学智慧校园建设平台网站做网站条件