Home  >  Article  >  Backend Development  >  How to create a numerical list in python

How to create a numerical list in python

尚
forward
2020-06-15 17:37:332606browse

How to create a numerical list in python

1. range

range() can produce a series of values ​​

Syntax: range(start subscript (inclusive), end subscript ( Excluding)[, step size])

Example:

for value in range(1,5):
    print(value)

print("*"*30)

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

Result:

1
2
3
4
******************************
1
3

2. Use list() to convert the values ​​generated by range into a list

Example:

nums=list(range(1,5))
print(nums)
print("*"*30)

nums=list(range(1,5,2))
print(nums)

Result:

[1, 2, 3, 4]
******************************
[1, 3]

3. List parsing: quickly generate a list of values ​​

Syntax example: list=[value**2 for value in range (1,11)]

Example:

list=[value**2 for value in range(1,5)]
print(list)

Result:

[1, 4, 9, 16]

For more related knowledge, please pay attention to the python video tutorial column

The above is the detailed content of How to create a numerical list in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete