标签:python
1. 实现1-100的所有的和
sum = 0
for i in xrange(1,101):
sum+=i
print(sum)
结果:
5050
2. 实现1-500所有奇数的和
sum = 0
for i in xrange(1,501):
if i%2 == 1:
sum+=i
print(sum)
结果:
62500
3. 求1+ 2! + 3! + 4! + ……20!的和
sum = 0
chengji = 1
for i in xrange(1,21):
chengji=chengji*i
sum = sum + chengji
print(chengji)
print(sum)
结果:
2432902008176640000
2561327494111820313
4. 对指定一个list进行排序[2,32,43,453,54,6,576,5,7,6,8,78,7,89]
list1= [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
list1.sort()
print(list1)
list1.reverse()
print(list1)
结果:
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
[576, 453, 89, 78, 54, 43, 32, 8, 7, 7, 6, 6, 5, 2]
标签:python
原文地址:http://huangzp.blog.51cto.com/12434999/1976511