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)
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 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)
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!