Lesson2:Python:for Statement Simple Application

Source: Internet
Author: User

Sample1 Ball 1+2+3...+100 and

Sum=0

For I in Range (1,101):

Sum+=i

Print sum

    1. Print if indented and sum+, the value of each sum+ will be printed
    2. If print is shelf with for, it will only print the value of the final sum+ after the for loop has finished executing

Sample2:for loop application and bubble sort:

LIST=[10,12,14,19,20,22]

For I in range (1,len (list)):

For j in Range (0,len (list)-1):

If LIST[J]>LIST[J+1]:

LIST[J],LIST[J+1]=LIST[J+1],LIST[J]

Print List

    1. Loop statement Sample2

list=["1", "2", "3", "4"

, "5", "6", "7"]

For I in list:

Print I

Note:

    1. Whether it is an if statement or a for statement, you need to add ":" When you finish writing your judgment or condition, or you will get an error.
    2. assignments, data type conversions, print, and so on are not required to add any punctuation
    3. The list data type can be wrapped directly, not affected

Lesson2:Python:for Statement Simple Application

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.