二周四次课(10月26日)
复习,做如下练习题
1.实现1-100的所有的和
sum = 0 for n in xrange(1, 101): sum += n n += 1 print(‘1+2+3+...+100 = ‘ + str(sum))
结果:
1+2+3+...+100 = 5050
2.实现1-500所有奇数的和
sum = 0 for x in xrange(1, 501, 2): sum += x x += 1 print(‘1+3+5+...+499 = ‘ + str(sum))
结果:
1+3+5+...+499 = 62500
3.求1+ 2! + 3! + 4! + ……20!的和
sum = 0 x = 1 n = 1 for n in xrange(1, 21): x *= n sum += x n += 1 print(‘1+ 2! + 3! + 4! + ……20! = ‘ + str(sum))
结果:
1+ 2! + 3! + 4! + ……20! = 2561327494111820313
4.对指定一个list进行排序[2,32,43,453,54,6,576,5,7,6,8,78,7,89]
l1 = [2, 32, 43, 453, 54, 6, 576, 5, 7, 6, 8, 78, 7, 89] l1.sort() print(l1)
结果:
[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]
5.复习字典排序,字符串, list, tuple常用方法
字符串的常用方法:http://13406610.blog.51cto.com/13396610/1974779
列表、元组、字典的常用方法:http://13406610.blog.51cto.com/13396610/1975421
原文地址:http://13406610.blog.51cto.com/13396610/1976429