#格式化
print ‘%c‘%98
print ‘%d+%d=%d‘%(3,4,3+4)
print ‘%5.2f‘%28.276
sum=10.
print ‘sum is %.2f‘%sum
ans:
b
3+4=7
28.28
sum is 10.00
#其他函数
x=[1,3,5,2]
#print sum(x)#error!!
#print sum(1,2,3) #error!!
print max(x)
#print max(1,2,3)
print sorted(x)
print list(enumerate(x))
g=lambda x,y:x+y
print g(3,4)
print filter(None,[0,1,2,False,True])
print(filter(lambda x:x%2,range(10)))
print map(lambda x:x**2+1,range(4))
ans:
5
[1, 2, 3, 5]
[(0, 1), (1, 3), (2, 5), (3, 2)]
7
[1, 2, True]
[1, 3, 5, 7, 9]
[1, 2, 5, 10]
a=[‘ ds‘,‘dd ‘]
b=[i.strip() for i in a]
print b
print list(map(str.strip,a)) #两者等价
vec=[(1,2),(3,4)]
c=[j for i in vec for j in i]
print c
ans:
[‘ds‘, ‘dd‘]
[‘ds‘, ‘dd‘]
[1, 2, 3, 4]
x,y=divmod(10,3) print x,y ans:3 1
print [(x,y)for x in range(2) for y in range(2)] ans: [(0, 0), (0, 1), (1, 0), (1, 1)]
from random import* print random() print uniform(3,4) print randint(1,10) a=[1,2,3,4,5,9] print choice(a)#随机选一个 print choice(range(4)) shuffle(a) #随机打乱,a改变了 print a print sample(a,2)#取样2个 ans: 0.826007795044 3.49915618492 6 4 2 [3, 5, 2, 9, 4, 1] [5, 2]