python二元表达式用法

yipeiwu_com7年前Python基础

二元表达式:

wide=1
new_w = 299 if not wide else 28
print(new_w)
new_w = 299 if wide>0 else 28
print(new_w)



a,b=1,2
max = a if a > b else b

三元表达式

wide=0

new_w = 299 if wide>0 else 'sdf' if wide==0 else 28
print(new_w)

三目运算符:

这个是三目运算符(伪,因为Python根本就没有三目):

val = float(raw_input('Age: '))
print 'You should be', ('working','retired')[val>65]

以上这篇python二元表达式用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python之生产者消费者模型实现详解

代码及注释如下 #Auther Bob #--*--conding:utf-8 --*-- #生产者消费者模型,这里的例子是这样的,有一个厨师在做包子,有一个顾客在吃包子,有一个服务...

浅谈Pandas中map, applymap and apply的区别

1.apply() 当想让方程作用在一维的向量上时,可以使用apply来完成,如下所示 In [116]: frame = DataFrame(np.random.randn(4,...

Python实现微信好友的数据分析

Python实现微信好友的数据分析

基于微信开放的个人号接口python库itchat,实现对微信好友的获取,并对省份、性别、微信签名做数据分析。 效果: 直接上代码,建三个空文本文件stopwords.txt,ne...

python字典的遍历3种方法详解

python字典的遍历3种方法详解

遍历字典: keys() 、values() 、items()   1. xxx.keys() : 返回字典的所有的key 返回一个序列,序列中保存有字典的所有的键   效果图:   ...

TensorFlow 合并/连接数组的方法

如下所示: import tensorflow as tf a = tf.Variable([4,5,6]) b = tf.Variable([1,2,3]) c = tf.co...