Python练习13----类似C语言的条件运算符?:

语法:
c = a if a>b else b //如果a>b返回a,否则返回b

可以参看《Python 一些小的古怪写法 if else for in join》
http://blog.csdn.net/qq_16234613/article/details/64441428

练习

#Hello world program in Python
# -*- coding: utf8 -*-
a,b=1,2
print("a:",a,"b:",b)
a,b=b,a  #交换a,b
print("a:",a,"b:",b)
max = a if a>b else b
print("max:",max)

运行结果
Python练习13----类似C语言的条件运算符?:_第1张图片

你可能感兴趣的:(Python)