首页 > 编程语言 >range方法在Python2和Python3中的不同

range方法在Python2和Python3中的不同

时间:2023-09-04 16:31:44浏览次数:34  
标签:10 ... stop start range 方法 Python3 Python2

range()方法是Python中常用的方法, 但是在Python2和Python3中使用方法不同,下面看下它们的不同使用方法。

range方法详解

range(start, stop[, step])

range是python中的其中一个内置函数

作用:可创建一个整数列表。一般用在 for 循环中。

参数说明:

start:起点,一般和stop搭配使用,既生成从start开始到stop结束(不包括stop)范围内的整数,例如:range(1,10),会生成[1,2,3,4,5,6,7,8,9]

stop:终点,可以和start搭配使用,也可以单独使用,既当start=0时,例如range(5) = range(0, 5)

step:步长,既下一次生成的数和这次生成的数的差,例如range(1, 10, 2) 生成[1,3,5,7,9],再如range(1,10,3) 生成[1, 4, 7]

代码示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


Python 3.7.2 (default, Feb 12 201908:15:36)

[Clang 10.0.0 (clang-1000.11.45.5)] on darwin

Type "help""copyright""credits" or "license" for more information.

>>> for in range(1,101):

...     print(i)

...

1

2

3

4

5

6

7

8

9

>>>


使用区别

在python2中,range方法得到的结果就是一个确定的列表对象,列表对象所拥有的方法,range方法生成的结果对象都可以直接使用,而在python3中,range方法得到的对象是一个迭代器而不是一个确定的列表,如果想要转化为列表对象则需要再使用list方法进行转化。

for i in range(start, stop)在python2和python3中都可使用

代码实例:

Python3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


Python 3.7.2 (default, Feb 12 201908:15:36)

[Clang 10.0.0 (clang-1000.11.45.5)] on darwin

Type "help""copyright""credits" or "license" for more information.

>>> for in range(1,101):

...     print(i)

...

1

2

3

4

5

6

7

8

9

>>>


Python2:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


Python 2.7.10 (default, Aug 17 201819:45:58)

[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin

Type "help""copyright""credits" or "license" for more information.

>>> for in range(1,101):

...     print(i)

...

1

2

3

4

5

6

7

8

9

>>>



Python2直接生成列表,Python3需要配合list方法使用

Python3:

1

2

3

4

5

6

7

8

9

10

11

12


Python 3.7.2 (default, Feb 12 201908:15:36)

[Clang 10.0.0 (clang-1000.11.45.5)] on darwin

Type "help""copyright""credits" or "license" for more information.

>>> l = range(110)

>>> l

range(110)

>>> type(l)

<class 'range'>

>>> l2 = list(l)

>>> l2

[123456789]

>>>


Python2:

1

2

3

4

5

6

7


Python 2.7.10 (default, Aug 17 201819:45:58)

[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin

Type "help""copyright""credits" or "license" for more information.

>>> l = range(110)

>>> l

[123456789]

>>>



Python3中range()方法生成的已经不是一个列表, 而是一个range的迭代器

标签:10,...,stop,start,range,方法,Python3,Python2
From: https://blog.51cto.com/u_16191847/7352266

相关文章

  • CF838D Airplane Arrangements 题解
    题意一架飞机有\(n\)个座位排成一列,有\(m\)名乘客(\(m\leqn\))依次上飞机。乘客会选择一个目标座位(两人可以选同一个目标座位),然后选择从前门或者后门上飞机,上飞机后,他们会走到自己的目标座位,如果目标座位已经有人坐了,他们会继续往前走,在走到第一个空位后坐下。如果走到最后......
  • centos stream 9 编译 python3.11.5 源代码
    wgethttps://www.python.org/ftp/python/3.11.5/Python-3.11.5.tgztar-zxvfPython-3.11.5.tgzcdPython-3.11.5执行配置帮助./configure--help主要是想设置安装路径Installationdirectories:--prefix=PREFIXinstallarchitecture-independentfilesinP......
  • [-004-]-Python3+Unittest+Selenium Web UI自动化测试之部署Jenkins后脚本无界面执行
    一、场景:jenkins构建selenium自动化用例的时候,会有jenkins自带服务后台运行自动化脚本,可无界面运行IE、Chrome、Firefox。 二、解决办法:managejenkis-节点-添加节点 三、填写信息如下:    四、此时看到新建robot节点,但未启动五、点击节点robot,详情界面显示了......
  • 【CF1542C】Strange Function(数论)
    题目大意:#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constllmod=1e9+7;lln;lllcm(llx,lly){ returnx/__gcd(x,y)*y;}intmain(){ intT; cin>>T; while(T--){ cin>>n; llans=n%mod; for(lli=1,j=1;n/j......
  • 【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect
    问题描述在AppServiceForLinux中,部署NodeJS应用,应用启动失败。报错信息为:2023-08-29T11:21:36.329731566ZRangeError:Incorrectlocaleinformationprovided2023-08-29T11:21:36.329776866ZatIntl.getCanonicalLocales(<anonymous>)2023-08-29T11:21:36.329783066ZatC......
  • Centos误删系统自带python2.7,yum报错恢复方法
     使用wget分别下载python以及yum的rpm包资源地址如下:          http://vault.centos.orgmkdir/usr/local/src/pythoncd/usr/local/src/pythonwgethttp://vault.centos.org/7.6.1810/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpmwgethtt......
  • python3.6使用wordcloud 1.9报错ValueError: Only supported for TrueType fonts
    该版本的wordcloud的源码中显示不兼容python3.6 解决办法:修改wordcloud源码修改前508:box_size=draw.textbox((0,0),word,font=transposed_font,anchor="lt")510:result=occupancy.sample_position(box_size[3]+self.margin,......
  • 【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect
    问题描述在AppServiceForLinux中,部署NodeJS应用,应用启动失败。报错信息为:2023-08-29T11:21:36.329731566ZRangeError:Incorrectlocaleinformationprovided2023-08-29T11:21:36.329776866ZatIntl.getCanonicalLocales(<anonymous>)2023-08-29T11:21:36.3297830......
  • 安装python3.5并在QT中配置使用
    一、安装32位的python3.5及各种库链接:https://pan.baidu.com/s/1q94_Vo3XDhfZ_hmssPjz6w提取码:zotf先在dos环境下输入python,如果显示版本则表示python安装成功,安装库文件的时候,在dos环境下进入whl文件所在的目录,然后使用pipinstall***-whl。如果提示pip版本过低,则按照提示更新pip......
  • python3.8以及更高版本 编译pcl
    github https://github.com/strawlab/python-pcl1.安装Cython当前的Cython的版本如下 由于python-pcl代码仓已经是很老的代码了,没有人在维护,高版本的Cython会导致编译失败报错如下Cython.Compiler.Errors.CompileError:pcl/_pcl_180.pyx 使用Cython==0.29版本即可解决......