python基础-range用法_python2.x和3.x的区别

时间:2021-12-28 03:11:59

 #range帮助创建连续的数字,通过设置步长来指定不连续

python2.7

#直接就在内存中创建出来(0-99)

>>> range(100)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>>

 

python3.0

#没有在内存中创建出来

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author: nulige
4 
5 v = range(100)
6 print(v)

执行结果:

1 range(0, 100)   #没有在内存中创建出来

 

#只有循环才会创建出来

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author: nulige
4 
5 v = range(100)
6 for item in v:
7     print(item)

执行结果:

python基础-range用法_python2.x和3.x的区别python基础-range用法_python2.x和3.x的区别
  1 0
  2 1
  3 2
  4 3
  5 4
  6 5
  7 6
  8 7
  9 8
 10 9
 11 10
 12 11
 13 12
 14 13
 15 14
 16 15
 17 16
 18 17
 19 18
 20 19
 21 20
 22 21
 23 22
 24 23
 25 24
 26 25
 27 26
 28 27
 29 28
 30 29
 31 30
 32 31
 33 32
 34 33
 35 34
 36 35
 37 36
 38 37
 39 38
 40 39
 41 40
 42 41
 43 42
 44 43
 45 44
 46 45
 47 46
 48 47
 49 48
 50 49
 51 50
 52 51
 53 52
 54 53
 55 54
 56 55
 57 56
 58 57
 59 58
 60 59
 61 60
 62 61
 63 62
 64 63
 65 64
 66 65
 67 66
 68 67
 69 68
 70 69
 71 70
 72 71
 73 72
 74 73
 75 74
 76 75
 77 76
 78 77
 79 78
 80 79
 81 80
 82 81
 83 82
 84 83
 85 84
 86 85
 87 86
 88 87
 89 88
 90 89
 91 90
 92 91
 93 92
 94 93
 95 94
 96 95
 97 96
 98 97
 99 98
100 99
View Code

 

#range帮助创建连续的数字

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author: nulige
4 
5 v = range(0,100)  #从0-99
6 for item in v: 7 print(item)

结果:

python基础-range用法_python2.x和3.x的区别python基础-range用法_python2.x和3.x的区别
  1 0
  2 1
  3 2
  4 3
  5 4
  6 5
  7 6
  8 7
  9 8
 10 9
 11 10
 12 11
 13 12
 14 13
 15 14
 16 15
 17 16
 18 17
 19 18
 20 19
 21 20
 22 21
 23 22
 24 23
 25 24
 26 25
 27 26
 28 27
 29 28
 30 29
 31 30
 32 31
 33 32
 34 33
 35 34
 36 35
 37 36
 38 37
 39 38
 40 39
 41 40
 42 41
 43 42
 44 43
 45 44
 46 45
 47 46
 48 47
 49 48
 50 49
 51 50
 52 51
 53 52
 54 53
 55 54
 56 55
 57 56
 58 57
 59 58
 60 59
 61 60
 62 61
 63 62
 64 63
 65 64
 66 65
 67 66
 68 67
 69 68
 70 69
 71 70
 72 71
 73 72
 74 73
 75 74
 76 75
 77 76
 78 77
 79 78
 80 79
 81 80
 82 81
 83 82
 84 83
 85 84
 86 85
 87 86
 88 87
 89 88
 90 89
 91 90
 92 91
 93 92
 94 93
 95 94
 96 95
 97 96
 98 97
 99 98
100 99
View Code

 

#range帮助创建连续的数字,5就是步长,通过设置步长来指定不连续。

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author: nulige
4 
5 v = range(0,100,5)   #5就是步长,每5个创建一个
6 for item in v:
7     print(item)