python范围数字求和

Python range() function is a utility function to generate the list of numbers. The list of numbers generated is useful for iteration logic.

Python range()函数是一个实用程序函数,用于生成数字列表。 生成的数字列表对于迭代逻辑很有用。

Python范围() (Python range())

If you are following our tutorial from the beginning, you may notice that many times we have used python range function.

如果您从一开始就遵循我们的教程,您可能会注意到我们已经多次使用python range函数。

Basically, Python range is used to generate a list of numbers. Note that, Python range function does not return a list, rather it acts like a list. The basic structure of Python range function is given below.

基本上,Python range用于生成数字列表。 注意,Python range函数不会返回列表,而是像列表一样。 Python range函数的基本结构如下。

  1. range(n) : This will generate a list of numbers from 0 to n.range(n):这将生成一个从0到n的数字列表。
  2. range(a, b) : This will generate a list of numbers from a to b-1.range(a,b):这将生成一个从a到b-1的数字列表。
  3. range(a, b, c) : This will generate a list of numbers from a to b-1 where step size is c.range(a,b,c):这将生成一个从a到b-1的数字列表,步长为c。

Please remember that, the range() function does not return any list. In the following example, we will see that.

请记住, range()函数不会返回任何列表。 在下面的示例中,我们将看到。

# initialize a list from 0 to 5
init_list = [0, 1, 2, 3, 4, 5]# it will show you the type is 'list'
print('Type of init_list is :', type(init_list))# get the instance of range() function
instance_range = range(1, 10)# it will show that the type is 'range'
print("Type of instance_range is :", type(instance_range))

The output of the following code will be

以下代码的输出将是

Python range()函数示例 (Python range() function example)

Many examples can be given for Python range function. You can use it in many places of your code. Suppose, you need to print the first 1-to-n odd numbers. You can do that easily using python range function. The code will be;

Python范围函数可以给出许多示例。 您可以在代码的许多地方使用它。 假设您需要打印前1到n个奇数。 您可以使用python range函数轻松地做到这一点。 该代码将是;

# prompt for input
num = int(input('Enter the max limit: '));# so, generate list from 1 to num(inclusive)
for i in range(1, num+1, 2):print(i, end=' ')

Here, given 11 as input, we will get the following output

在这里,给定11作为输入,我们将得到以下输出

Enter the max limit: 11
1 3 5 7 9 11

使用Python range()遍历遍历列表 (Traversing List using Python range() for loop)

However you can access python list using index of the list. In this case, the index will be generated by python range function. The following code will help you understand this clearly.

但是,您可以使用列表索引访问python列表。 在这种情况下,索引将由python range函数生成。 以下代码将帮助您清楚地理解这一点。

# initialize a list
init_list = [1, 'abc', 23, 'def']for i in range(len(init_list)):print(init_list[i])

The output of the following code will be

以下代码的输出将是

1
abc
23
def

So, that’s all for Python range function. Most of the time python range function is used with for loop and to iterate the list.

因此,这就是Python范围函数的全部内容。 大多数时候,python range函数与for循环一起使用并迭代列表。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/15073/python-range

python范围数字求和

python范围数字求和_Python范围()相关推荐

  1. python数组数字求和_python数组求和

    Pandas是Python的一个大数据处理模块.Pandas使用一个二维的数据结构DataFrame来表示表格式的数据,相比较于Numpy,Pandas可以存储混合的数据结构,同时使用NaN来表示缺失 ...

  2. python字符串数字求和_python处理字符串:将字符串中的数字相加求和

    原博文 2018-07-27 23:15 − 计算字符串中所有数字的和,字符串中有数字和字母组合而成如果出现连续数字,按照一个数操作具体解释在代码行里: def sum_str(str1): len1 ...

  3. python数字求和_python数字求和

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 对于每个值,我右对齐值的位(忽略0b),计算1的数量,并移动1的数量. #!us ...

  4. python输出数字怎么办_python怎么输出数字

    Python数字运算 Python 解释器可以作为一个简单的计算器:您可以在解释器里输入一个表达式,它将输出表达式的值. 表达式的语法很直白: +, -, * 和/ 和在许多其它语言(如Pascal或 ...

  5. python判断数字位数_python求数字位数的方法

    第一种: 利用str()函数将数字转化成字符串,再利用len()函数判断位长. 1 a=Int(raw_input("the number you want type in:") ...

  6. python输出数字金字塔_Python输出数字金字塔

    原博文 2019-10-25 19:47 − 使用Python输出一个数字金字塔 运行结果: 源代码: ''' Python输出数字金字塔 ''' for x in range(1,10): prin ...

  7. python 字符 数字比较_Python数字和字符串(5/30)

    前言 Life is short,you need Python.本系列是学习Python的心酸历程!(持续更新) 大纲 数值类型及操作 字符串类型及操作 模块time的使用方法 数字迭代器和文本进度 ...

  8. python的数字运算_Python基础语法——数字运算

    数字运算 编程是将问题数据化的一个过程,数据离不开数字,Python的数字运算规则与我们学习的四则运算规则是一样的,即使不使用Python来编写复杂的程序,也可以将其当作一个强大的计算器.打开Pyth ...

  9. python创建数字列表_Python创建数字列表

    [一]range()函数在python中可以使用range()函数来产生一系列数字for w in range(1,11): print(w) 输出: 1 2 3 4 5 6 7 8 9 10 #注意 ...

最新文章

  1. 代码检查规则:Java语言案例详解
  2. 云服务器一直显示关机中,云服务器一直提示关机中
  3. Sql 中的变量使用
  4. Redis学习之Docker环境搭建
  5. 从移动广告业务“长出”的新增长点,汇量科技进军云服务
  6. c#遍历一个文件夹下的所有文件包括子文件夹【原】
  7. 1026. 节点与其祖先之间的最大差值
  8. 现在论文用手写还是用计算机写,毕业论文计算机手写数字识别技术完整版.docx...
  9. php 中文获取拼音,php获取中文的拼音代码_php
  10. 中国单箱梁体最宽矮塔斜拉桥合龙
  11. Could not initialize proxy - the owning Session was closed ---Hibernate与延迟加载:
  12. uni-app基础知识
  13. uniapp 基础框架模板
  14. Python基础——修改Python字典中的key(键)
  15. 定期清理gitlab的备份监控gitlab备份
  16. h5 实现公众号登录
  17. wordpress与微信公众号对接
  18. BloomFilter布隆过滤器
  19. 汽车文化-实用与实在-车身结构
  20. 类与类之间的几种关系

热门文章

  1. 深入剖析java迭代器以及C#迭代器!
  2. 关于DIV+CSS和XHTML+CSS的理解
  3. 基于Altium Designer的4层PCB板的绘制
  4. 用gallery展示图片,实现中间图片稍大,两边较小的效果
  5. [转载] Java获取嵌套的json串里的返回结果
  6. [转载] Java static关键字详解
  7. 用反射实现简单的框架
  8. Java8-2-Lambda表达式实战-一句话实现Map中按照Value排序
  9. tensorflow 模型权重导出
  10. 修改ORACLE RAC的字符集(记录一下)