Python Loop Small Example

Source: Internet
Author: User

1. Achieve 1-100 of all and

sum = 0

forIinchxrange(1,101):

Sum+=i

Print (sum)

Results:

5050


2. Achieve 1-500 of all odd and

sum = 0

forIinchxrange(1,501):

if i%2 = = 1:

Sum+=i

Print (sum)

Results:

62500


3. Ask for 1+ 2! + 3! + 4! + ... 20! and the

sum = 0

Chengji = 1

forIinchxrange(1, +):

Chengji=chengji*i

sum = sum + Chengji

Print (Chengji)

Print (sum)

Results:

2432902008176640000

2561327494111820313


4. Sort the specified list [2,32,43,453,54,6,576,5,7,6,8,78,7,89]

list1= [2, +, +,453, Wu,6,576,5,7,6,8, +,7, the]

List1.sort ()

Print (List1)

List1.reverse ()

Print (List1)

Results:

[2, 5, 6, 6, 7, 7, 8, 32, 43, 54, 78, 89, 453, 576]

[576, 453, 89, 78, 54, 43, 32, 8, 7, 7, 6, 6, 5, 2]


Python Loop Small Example

Related Article

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.