三元操作符
#! /usr/bin/python
#coding:utf-8
age=20
type="adult" if age >= 20 else "child"
print "type is:%s"%(type)
type="adult" if (age >= 20) else ("child")
print "Type is:%s"%(type)
分析:
三元运算符格式:赋值语句 if (条件判断) else (表达式)
若条件判断成立,则赋值语句中变量的值就该赋值语句中表达式的值,否则用表达式中的值赋值给赋值语句中的变量。