热线电话:13121318867

登录
2019-03-17 阅读量: 685
python如何创建数值列表?

函数range()

for value in range(1,5):
print(value)
>>>1
>>>2
>>>3
>>>4

函数range()让Python从你指定的第一个值开始数,在到达你指定的第二个值后停止,因此输出并不包含第二值。

3.2使用range()创建数字列表

  将range() 作为list() 的参数,输出将为一个数字列表。

numbers = list(range(1,6))
print(numbers)
>>>[1, 2, 3, 4, 5]

  range()函数还可指定步长:

even_numbers = list(range(1,13,2))
print(even_numbers)
>>>[1, 3, 5, 7, 9, 11]

  函数range() 从1开始数,然后不断地加2,直到达到或超过终值。

  使用函数range() 几乎能够创建任何需要的数字集。

squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
15.6347
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子