Python之几个简单的练习题

Python练习题:

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

#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time     : 2017/10/26 9:20
# @Author   : hantong
# @File     : revise.py

#实现1-100的所有的和
sum = 0 
for i in xrange(1,101):
    sum += i
print(sum)

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

#!/usr/bin/env python
# -*- coding:utf8 -*-
# @Time     : 2017/10/26 9:20
# @Author   : hantong
# @File     : revise.py
#实现1-500所有奇数的和
sum = 0
for j in xrange(1,501,2):
    sum += j
print(sum)

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

#!/usr/bin/env python # -*- coding:utf8 -*- # @Time     : 2017/10/26 9:20 # @Author   : hantong # @File     : revise.py

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

n = 1 m = 0 for i in xrange(1,21):     n *=i

    m += n print(m)

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

方法一:

#!/usr/bin/env python # -*- coding:utf8 -*- # @Time     : 2017/10/26 9:20 # @Author   : hantong # @File     : revise.py

list = [2,32,43,453,54,6,576,5,7,6,8,78,7,89] l = sorted(list) print(l)

方法二:
#!/usr/bin/env python # -*- coding:utf8 -*- # @Time     : 2017/10/26 9:20 # @Author   : hantong # @File     : revise.py
list = [2,32,43,453,54,6,576,5,7,6,8,78,7,89] list.sort() print(list)

你可能感兴趣的:(Python之几个简单的练习题)