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

range函数python_range()函数以及Python中的示例相关推荐

  1. python 中arange函数_浅谈Python中range与Numpy中arange的比较

    本文先比较range与arange的异同点,再详细介绍各自的用法,然后列举了几个简单的示例,最后对xrange进行了简单的说明. 1. range与arange的比较 (1)相同点:A.参数的可选性. ...

  2. python 高斯函数拟合_在python中拟合任意高斯函数,消耗大量内存

    我试图(在python中)将一系列任意数量的高斯函数(由一个仍在改进的简单算法确定)拟合到一个数据集.对于我当前的样本数据集,我有174个高斯函数.我有一个进行拟合的过程,但基本上是复杂的猜测和检查, ...

  3. python中定义匿名函数的关键字是,Python中的匿名函数

    写python的时候,大多数场景下,我都是if else选手,因为最核心的逻辑几乎都是通过if else语句来实现的.关于匿名函数这块儿,其实可以用常见的循环等方法来实现,但是如果你想成为一个pyth ...

  4. python中的goto函数_如何在 Python 中实现 goto 语句

    Python 默认是没有 goto 语句的,但是有一个第三方库支持在 Python 里面实现类似于 比如在下面这个例子里, from goto import with_goto @with_goto ...

  5. python numpy sum函数_如何使用Python中的sum函数?

    之前小编向大家介绍过python中的sum函数(https://www.py.cn/jishu/jichu/22025.html).在python中sunm函数使用分为两种情况,一种是python自带 ...

  6. python正态分布函数_如何在Python中计算正态分布函数和gamma累积分布函数的逆函数?,scipy,python,中正,伽马,反函数...

    对于伽玛分布累积分布函数,在python中是 scipy.special.gdtr 伽玛分布累积分布函数. 返回伽玛概率密度函数从零到 x 的积分, 而伽玛分布累积分布函数的反函数是 gdtrix, ...

  7. python中add函数_如何使用python中的add函数?

    之前向大家介绍过python中的求和函数sum函数,numpy中的sum函数,对于数组可以指定维度进行相加.numpy中还有另一种求和运算方法,即add函数.add函数不仅作用于numpy中加法运算, ...

  8. python中remove函数是什么意思,python中pop函数和remove函数有什么区别

    python中pop函数和remove函数有什么区别 发布时间:2020-12-10 09:36:18 来源:亿速云 阅读:80 作者:小新 小编给大家分享一下python中pop函数和remove函 ...

  9. python中list作为函数参数_在python中list作函数形参,防止被实参修改的实现方法

    0.摘要 我们将一个list传入函数后,函数内部对实参修改后,形参也会随之改变.本文将主要介绍这种错误的现象.原因和解决方法. 1.代码示例 def fun(inner_lst): inner_lst ...

最新文章

  1. IDEA新建springboot项目发生错误
  2. html表单赋值提交,jQuery自动给表单赋值
  3. Android之属性动画初步
  4. 红黑树概念及其相关操作的实现
  5. java年利润编程题_[编程入门]利润计算-题解(Java代码)
  6. spring源码阅读--@Transactional实现原理
  7. 如何在sh的字符串中包含换行符?
  8. 图像处理之opencv---mat、cvmat、IplImage之间的转换
  9. 无线通信技术—LoRa
  10. mysql 多条件求和_多条件求和的8种方法【Excel分享】
  11. 啤酒肚真的是喝啤酒引起的吗?
  12. 利用批处理脚本批量删除电脑上的空文件夹
  13. 仿照浮墨的开源笔记neno
  14. 运动式耳机品牌排行榜,运动耳机排名前六
  15. 史上最全讲解:Spring详解
  16. 我用 10 张脑图,征服了一系列大厂面试官。
  17. 线程【3】- 多线程中PTHREAD_MUTEX_RECURSIVE的理解
  18. 车载毫米波雷达的性能标准与测试方法
  19. 独立显卡 核心显卡 集成显卡
  20. 高可用之——consul 文档

热门文章

  1. LDAP命令介绍---dsreplication--enable:DISABLE
  2. SOLOv2论文简读
  3. 中国官方要求进一步加强中资商业银行境外机构合规管理
  4. 生活之难:生活到底难在哪里
  5. LCD显示屏原理与应用
  6. Oracle 创建表空间,用户,赋值(简装)
  7. vs运行时候冒了这个错:无法启动IIS Express Web 服务器~Win10
  8. 关于liaoxuefeng的python3教程实战第四天
  9. 【转】GitHub入门详细讲解
  10. Visual Studio 2010快捷键大全