python--range()函数
1:
>>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(0, 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(5, 10, 2) [5, 7, 9] >>> range(10, 5, -2) [10, 8, 6]
2:
1 def test_range(n): 2 for i in range(n): 3 print i 4 5 print '****************************************' 6 for j in range(0, n): 7 print j 8 9 print '****************************************' 10 for k in range(5, n): 11 print k 12 13 print '****************************************' 14 for m in range(0, n , 2): 15 print m 16 17 print '****************************************' 18 for n in range(n, 0, -1): 19 print n 20
输出:
1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 **************************************** 12 0 13 1 14 2 15 3 16 4 17 5 18 6 19 7 20 8 21 9 22 **************************************** 23 5 24 6 25 7 26 8 27 9 28 **************************************** 29 0 30 2 31 4 32 6 33 8 34 **************************************** 35 10 36 9 37 8 38 7 39 6 40 5 41 4 42 3 43 2 44 1