1、原地交换两个数字
x,y = 10,5
print (x,y)
y,x = x,y
print (x,y)
(10, 5)
(5, 10)
2、链状比较操作符
n = 10
print (1 < n < 20)
print (1 < n <= 9)
True
False
3、使用三元操作符实现条件赋值
[表达式为真的返回值] if [表达式] else [表达式为假的返回值]
y = 20
x = 9 if (y==10) else 8
print x
8
4、a,b,c中最小数
def small(a,b,c):
return a if a<b and a<c else(b if b<a and b<c else c)
print (small(4,3,5))
3
5、组合多个字符串
test = ["I", "Like" , "Python"]
print (test)
print ("".join(test))
['I', 'Like', 'Python']
ILikePython
6、找到列表中出现次数最多的数
test = [1,2,3,4,2,2,3,1,4,4,4,4]
print (max(set(test),key=test.count))
4
7、从两个相关序列中构建字典
t = (1,2,3)
t1 = (10,20,30)
print dict(zip(t,t1))
{1: 10, 2: 20, 3: 30}