Python常见函数详解

在这里插入图片描述

今天给大家介绍的是Python的range() 函数和enumerate()函数。

1、range() 函数
这个BIF(Built-in functions)有三个参数,其中用中括号括起来的两个表示这两个参数是可选的。
step 步长,默认为1。
range 这个BIF的作用是生成一个从start参数的值开始到stop参数的值结束的数字序列,该序列包含start的值但不包含stop的值。
例:
range([start,] stop[, step=1])for i in range(1, 10, 2):print(i)#输出结果# 1# 3# 5# 7# 9

2、enumerate()函数
sequence – 一个序列、迭代器或其他支持迭代对象。
start – 下标起始位置。
返回 enumerate(枚举) 对象
用 enumerate(A) 不仅返回了 A 中的元素,还顺便给该元素一个索引值 (默认从 0 开始)。此外,用 enumerate(A, j) 还可以确定索引起始值为 j。
enumerate(sequence, [start=0])seasons = [‘Spring’, ‘Summer’, ‘Fall’, ‘Winter’]print(list(enumerate(seasons)))#[(0, ‘Spring’), (1, ‘Summer’), (2, ‘Fall’), (3, ‘Winter’)]#与for结合seq = [‘one’, ‘two’, ‘three’]for i, element in enumerate(seq, start = 1): #下标从 1 开始 print i, element
#1 one#2 two#3 three

以上就是关于Python的range() 函数和enumerate()函数的相关信息了,希望能够给大家带来帮助。
文章部分内容源于网络,联系侵删*
文章转载自:http://http.taiyangruanjian.com/news/93363.html

猜你喜欢

转载自blog.csdn.net/zhimaHTTP/article/details/113752599