How to create a numerical list in python

Release: 2020-06-19 17:10:59
forward
2784 people have browsed it

How to create a numerical list in python

How to create a list of values in python:

1. range

range() can produce a series of values

Syntax :range(start subscript (inclusive), end subscript (exclusive)[, 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

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]

3. List parsing: quickly generate a numerical list

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]

For more related knowledge, please pay attention to thepython video tutorialcolumn

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!