How to create a numerical list in python

Release: 2020-06-15 17:37:33
forward
2607 people have browsed it

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)
Copy after login

Result:

1
2
3
4
******************************
1
3
Copy after login

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)
Copy after login

Result:

[1, 2, 3, 4]
******************************
[1, 3]
Copy after login

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)
Copy after login

Result:

[1, 4, 9, 16]
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template