复习,做如下练习题
1. 实现1-100的所有的和
2. 实现1-500所有奇数的和
3. 求1+ 2! + 3! + 4! + ……20!的和
4. 对指定一个list进行排序[2,32,43,453,54,6,576,5,7,6,8,78,7,89]
5. 复习字典排序,字符串, list, tuple常用方法
#/usr/bin/python
#coding=utf-8
#1.实现1-100的所有的和
sum = 0
for i in xrange(1,101):
sum += i
print sum
>>> 5050
#2.实现1-500所有奇数的和
js = sum([i for i in xrange(1,501) if i %2 ==1])
print js
>>> 62500
#3.求1+ 2! + 3! + 4! + ……20!的和
a = xrange(1,21)
b=1
sum3=0
for i in a:
b *=i
sum3 +=b
print b
print sum3
>>> 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
>>> [2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]