Python Loop statement (while, for)

Source: Internet
Author: User

#use of while#To be aware of the loops, consider the end of the loop.#There are 2 ways to consider the end of the loop:#1. Consider changing the condition of while in the loop body#2. Jump out of loop through break statement in the loop body#application of Method 1, the condition in which the loop body changes whilei =0 whileI < 5 :    Print("1.hello World") I+ = 1#Method 2: The loop body jumps out of the loop through the break statementi =0 whileTrue:Print("2.hello World") I+ = 1ifI >= 5 :         Break#while else used,i =0 whileI! = 5:    Print("3.hello World") I+ = 1#mostly here, if it's not plus 1, it's going to be a dead loop.Else:    Print("Print Complete")#request 1. 10 of andsum =0;i= 1; whileI <=10: Sum+=I i+ = 1Else:    Print("the calculation is complete and is:", sum)#Use for Loops#used primarily in traversing collections, such as traversing strings, lists, etc.Notice="Hello, China"#Name can be any valid variable name forNameinchNotice:Print(name)#The For loop can also be used with else#For as long as the smooth execution, will go to execute else,break except, while also pay attention to this problemPets = ["Little Black","Xiao Huang","Small white"] forPetinchPets:Print(PET)Else:    Print("For Loop complete")#using A For loop#Range Function forNinchRange (1,101):    if(n% 2 = =0):Print(n)#Break jumps out of the loop body and ends the entire loop.#continue end this loop and continue with the next loop#Pass represents a statement, other programs do not have this implementation forNuminchRange (1,11):    ifnum = = 6:         Break    Print(num) forNuminchRange (1,11):    ifnum = = 6:        Continue    Print(num)

Python Loop statement (while, for)

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.