标签:pytho for code /usr roo += col turn ever
1. 求1-100的和
[root@web sbin]# cat task1.py #!/usr/bin/env python sum = 0 for i in xrange(1, 101): sum += i print sum [root@web sbin]# python task1.py 5050
2. 求1-500所有奇数的和
[root@web sbin]# cat task2.py #!/usr/bin/env python sum = 0 for i in xrange(1, 501): if i % 2 != 0: sum += i print sum [root@web sbin]# python task2.py 62500
3. 求1 + 2! + 3! + .... + 20!
[root@web sbin]# cat task3.py #!/usr/bin/env python def fact(n): sum = 1 for i in xrange(1, n+1): sum *= i return sum if __name__ == ‘__main__‘: a = 0 for i in xrange(1, 21): a += fact(i) print a [root@web sbin]# python task3.py 2561327494111820313
4. 给列表做排序
In [1]: l = [2,32,43,453,54,6,576,5,7,6,8,78,7,89] In [2]: l.sort() //正序排序 In [3]: l Out[3]: [2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576] In [4]: l.reverse() //反序排序 In [5]: l Out[5]: [576, 453, 89, 78, 54, 43, 32, 8, 7, 7, 6, 6, 5, 2]
标签:pytho for code /usr roo += col turn ever
原文地址:http://www.cnblogs.com/tobeone/p/7740086.html