Python creates a number list __python

Source: Internet
Author: User

"One" range () function

You can use the range () function in Python to produce a series of numbers

For W in Range (1,11):
	print (W)

Output:

1
2
3
4
5
6
7
8
9
10

#注意: This is over by 10, not including 11.

"Two" list () function

If you pass range () as a parameter to the list () function, the number is output as a list

Num=list (range (1,11))
print (num)
Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

You can also specify the step size when using the range () function

Num=list (range (1,11,2))
print (num)
Output:
[1, 3, 5, 7, 9]

The "three" min () function, the max () function, and the sum () function

Min () function for minimum value, max () function for maximum value, sum () function sum

Num=list (range (1,11,2) print (num) print (max) print (min.) Print (
"sum is:", sum (num))
Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10
1
Sum is:55

"Four" list resolution

List resolution is to reduce code raw code:

Num=[] for
W in range (1,11):
	num4=w**2
	num.append (num4)
print (num)
Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

List Resolution code:

num1=[num2**3 for num2 in range (1,11)
print (NUM1)
Output:

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.