第九天学习:简单的练习题

1、实现1-100的所有的和

#!/usr/bin/env python
sum = 0
for i in xrange(1,101):
    sum += i
print(sum)

  

2、实现1-500所有奇数的和

#!/usr/bin/env python
l = [i for i in xrange(1,501) if i%2 != 0]
sum = 0
for i in l:
    sum += i
print(sum)

  

3、求1+ 2! + 3! + 4! + ……20!的和

#!/usr/bin/python
def fac(x):
    mul = 1
    for i in range(1,x+1):
        mul *= i
    return mul
sum = 0
for i in range(1,21):
    sum += fac(i)
print(sum)

  

4、对指定一个list进行排序:[2,32,43,453,54,6,576,5,7,6,8,78,7,89]

>>> li = [2,32,43,453,54,6,576,5,7,6,8,78,7,89]
>>> li.sort()
>>> li
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]

  

5.复习字典排序,字符串, list, tuple常用方法

见第五天和第六天学习

posted @ 2017-10-26 21:56  男孩别哭  阅读(165)  评论(0编辑  收藏  举报