range函数python_range()函数以及Python中的示例

range函数python

Python range()函数 (Python range() function)

The range() is a built-in function in Python which returns the sequence of values. It is used where we need to perform a specific action for a limited number of times. In general, if we write range starting from a value i and end up to the value j then we will get a sequence i, i+1 up to j-1.

range()是Python中的内置函数,它返回值的序列。 它用于需要有限次数执行特定操作的地方。 通常,如果我们写一个从值i开始到值j的范围,那么我们将得到一个序列i , i + 1到j-1 。

Syntax of range() function:

range()函数的语法:

    range(start, end, step)

It generally takes three arguments which are the following,

通常需要以下三个参数,

  • start: It is an integer from which the sequence has to start that is starting integer of the sequence of the integer.

    start :它是必须从其开始的整数,它是整数序列的起始整数。

  • end: It is an integer before which the sequence of integers is to be generated. Generally, if we provide an integer j then it generates sequence up to j-1.

    end :它是一个整数,在此之前将生成整数序列。 通常,如果我们提供整数j,则它会生成最多j-1的序列。

  • step: It is used to determine the difference or increment between each integer in the sequence.

    step :用于确定序列中每个整数之间的差或增量。

There are three-way by which we can call the range in the program which is the following,

我们可以通过以下三种方式在程序中调用范围:

  1. range(end):

    范围(结束)

    We will give only one argument ending in the

    我们只会给出一个以

    range() function when we want to start the sequence of the integer with 0. If we give the value of end is j the this is seen as a sequence whose upper limit is j and the lower limit is 0 and step is 0.

    当我们想以0开头整数序列时,会使用range()函数。如果给定end的值为j,则这将被视为一个序列,其上限为j,下限为0,step为0。

  2. range(start, end):

    范围(开始,结束)

    When the user decides to generate the sequence starting with a specific integer and also end before a specific integer then they call the range function with the two-argument. When the user gives the value of start then it works like the range function with one argument i.e

    当用户决定生成以特定整数开头并以特定整数结尾的序列时,他们将使用两个参数调用range函数。 当用户提供start的值时,它的工作方式类似于带有一个参数的range函数,即

    range(end) because the range() function by default starts the sequence with zero.

    range(end),因为默认情况下range()函数从零开始序列。

  3. range(start, end, step):

    范围(开始,结束,步骤)

    When we want to generate a sequence where we skip or increment the sequence integer by a value other than 1 that is we want a kind of arithmetic progression which have a common difference greater than 1 then we call the

    当我们要生成一个序列时,我们将序列整数跳过或增加一个非1的值,即我们想要一种算术级数,其公有差异大于1,则我们称

    range() function with the three arguments. If we call a range() function with no step then by default it goes for 1.

    具有三个参数的range()函数 。 如果我们不带任何步长调用range()函数 ,则默认情况下它为1。

Let's see an example by which we understand it in a better way.

让我们看一个例子,通过它我们可以更好地理解它。

Program:

程序:

#call range() with one argument
print('First sequence:')
for k in range(10):
print(k,end=' ')
print()
#call range() with two argument
print('Second sequence:')
for k in range(2,10):
print(k,end=' ')
print()
#call range() with two argument
print('Third sequence:')
for k in range(2,20,1):
print(k,end=' ')
print()
#call range() with negative step
print('Fourth sequence:')
for k in range(20,1,-2):
print(k,end=' ')
print()

Output

输出量

First sequence:
0 1 2 3 4 5 6 7 8 9 
Second sequence:
2 3 4 5 6 7 8 9 
Third sequence:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
Fourth sequence:
20 18 16 14 12 10 8 6 4 2 

翻译自: https://www.includehelp.com/python/range-function-with-example.aspx

range函数python

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544018.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ISP QoS Lab

ISP QoS Lab1-PQ优先级队列(PQ,Priority Queue)中,有高、中、普通、低优先级四个队列。数据包根据事先的定义放在不同的队列中,路由器按照高、中、普通、低顺序服务,只有高优先级的队列为空后才为中优先级的…

面渣逆袭:JVM经典五十问,这下面试稳了!

引言1.什么是JVM?JVM——Java虚拟机,它是Java实现平台无关性的基石。Java程序运行的时候,编译器将Java文件编译成平台无关的Java字节码文件(.class),接下来对应平台JVM对字节码文件进行解释,翻译成对应平台匹配的机器…

操作系统大内核和微内核_操作系统中的内核类型

操作系统大内核和微内核As we have already studied about the Kernels, we know that the Kernel is a program which is the main component of the Operating System. Now let us study about the types of Kernels. 正如我们已经研究了内核一样 ,我们知道内核是…

POJ 3268:Silver Cow Party 求单点的来回最短路径

Silver Cow PartyTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15989 Accepted: 7303Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A t…

【论文解读】Learning based fast H.264 to H.265 transcoding

时间: 2015 年 级别:APSIPA 机构: 上海电力大学 摘要 新提出的视频编码标准HEVC (High Efficiency video coding)以其比H.264/AVC更好的编码效率,被工业界和学术界广泛接受和采用。在HEVC实现了约40%的编码效率提升的同时&#…

面试必备:聊聊sql优化的15个小技巧

sql优化是一个大家都比较关注的热门话题,无论你在面试,还是工作中,都很有可能会遇到。如果某天你负责的某个线上接口,出现了性能问题,需要做优化。那么你首先想到的很有可能是优化sql语句,因为它的改造成本…

Scala程序将字符串转换为整数

In Scala, there is a huge library to support different operations on a string. One such operation is to convert string to int in Scala. 在Scala中,有一个庞大的库来支持对字符串的不同操作。 一种这样的操作是在Scala中将字符串转换为int。 A string can…

面试突击73:IoC 和 DI 有什么区别?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)IoC 和 DI 都是 Spring 框架中的重要概念,就像玫瑰花与爱情一样,IoC 和 DI 通常情况下也是成…

MyBatis整合Spring的实现(13)

2019独角兽企业重金招聘Python工程师标准>>> 本章中分析insert元素的解析。 1 配置文件 <insert id"insert" parameterType"cn.vansky.schedule.time.menu.bo.Menu"><!--WARNING - mbggeneratedThis element is automatically generat…

python 生成对称矩阵_对称矩阵| 使用Python的线性代数

python 生成对称矩阵Prerequisites: 先决条件&#xff1a; Defining a matrix 定义矩阵 Identity matrix 身份矩阵 Transpose matrix 转置矩阵 In linear algebra, if the matrix and its transpose are equal, then the matrix is symmetric (MT M). 在线性代数中&#xff0…

麻了,代码改成多线程,竟有9大问题

很多时候&#xff0c;我们为了提升接口的性能&#xff0c;会把之前单线程同步执行的代码&#xff0c;改成多线程异步执行。比如&#xff1a;查询用户信息接口&#xff0c;需要返回用户基本信息、积分信息、成长值信息&#xff0c;而用户、积分和成长值&#xff0c;需要调用不同…

Javascript 事件冒泡处理

js中子节点和父节点有相同的事件,怎么只触发子节点的事件 方法一、在子节点触发事件里加上阻止冒泡$("p").click(function(event){ event.stopPropagation(); // do something }); 原生的类似 只是不同浏览器有些不同方法二、在父节点触发事件里加上阻止冒泡…

PHP rewinddir()函数与示例

PHP rewinddir()函数 (PHP rewinddir() function) rewinddir() function is used to rewind/reset the directory handle which is created by the opendir() function. rewinddir()函数用于后退/重置由opendir()函数创建的目录句柄。 Syntax: 句法&#xff1a; rewinddir(di…

浅聊一下线程池的10个坑

日常开发中&#xff0c;为了更好管理线程资源&#xff0c;减少创建线程和销毁线程的资源损耗&#xff0c;我们会使用线程池来执行一些异步任务。但是线程池使用不当&#xff0c;就可能会引发生产事故。今天跟大家聊聊线程池的10个坑。大家看完肯定会有帮助的~线程池默认使用无界…

Linux命令行上执行操作,不退回命令行的解决方法

问题描述&#xff1a; 如果你现在登录Centos执行了某个操作&#xff0c;但是操作一直占用命令行&#xff0c;命令行显示的也都是这个命令相关的操作&#xff0c;我想做其它事情 &#xff0c;该怎么办呢 &#xff1f; 解决方法&#xff1a; 根据《Linux命令行与Shell编程大全第2…

python 当日日期_Python程序寻找当日赢家

python 当日日期Problem statement: 问题陈述&#xff1a; There are two basketball teams (Team1 and Team2) in a school and they play some matches every day depending on their time and interest. Some days they play 3 matches, some days 2, some days 1, etc. 一…

SpringBoot 读取配置文件的 5 种方法!

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;Spring Boot 中读取配置文件有以下 5 种方法&#xff1a;使用 Value 读取配置文件。使用 ConfigurationProperties 读…

机器学习朴素贝叶斯算法_机器学习中的朴素贝叶斯算法

机器学习朴素贝叶斯算法朴素贝叶斯算法 (Naive Bayes Algorithm) Naive Bayes is basically used for text learning. Using this algorithm we trained machine from text. 朴素贝叶斯基本上用于文本学习。 使用此算法&#xff0c;我们从文本中训练了机器。 Let’s understan…

jQuery的ajaxFileUpload上传文件插件刷新一次才能再次调用触发change

关于用ajaxfileupload时&#xff0c;遇到一个要刷新一次页面才能再次上传&#xff0c;用live()方法来绑定 file表单 的change事件就能够解决&#xff0c;直接$("xxx").change()这样只能调用一次&#xff0c;再次调用change的时候不能触发;IE浏览器下 file表单 选择完…

使用阿里巴巴 Druid 轻松实现加密!

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;为什么要加密&#xff1f;现在的开发习惯&#xff0c;无论是公司的项目还是个人的项目&#xff0c;都会选择将源码上传到 Gi…