Python loop structure

Source: Internet
Author: User

A For loop list or tuple can represent an ordered set. What if we want to access each element of a list in turn? Like List:l .= ['Adam','Lisa','Bart']PrintL[0]PrintL[1]PrintL[2If the list contains only a few elements, it is OK to write, and if the list contains 10,000 elements, we will not be able to write 10,000 lines of print. This is where the loop comes in handy. Python's forloops can iterate over each element of the list or tuple in turn: L= ['Adam','Lisa','Bart'] forNameinchL:PrintName NOTE: Name this variable is in the forDefined in the loop, which means that each element in the list is removed, the element is assigned to name, and then the for loop body is executed (that is, the indented block of code). This makes it very easy to traverse a list or tuple. Task class after the exam, the teacher to statistical average results, 4 students are known to list the results are as follows: L= [75, 92, 59, 68] Use the For loop to calculate the average score. 

 while Loop and  for  loop A different loop is while  Loop, while   The loop does not iterate over the elements of a list or tuple, Instead, it determines whether the loop ends with an expression. For example, to print an integer no greater than n from 0: n  = 10x  = 0  while  x < N:  print   x x  = x + 1while cycle first evaluates x  < N, if true, the The code block of the line loop body, otherwise, exits the loop. In the loop body, x  = x + 1 causes X to increase, eventually because x < <
sum =1 while x<=100:    if x%2==1:        sum+ =x;    X+ +; Print sum

Break exits the loop with forLoops or whileLoop, if you want to exit the loop directly inside the loop, you can use the Breakstatement. For example, to calculate integers from 1 to 100, we use while to implement: Sum=0x= 1 whileTrue:sum= Sum +x x= x + 1ifX > 100:         BreakPrintsum at first glance, whileTrue is a dead loop, but in the loop we also Judge X > 100when the condition is established, exit the loop with the break statement, which also enables the end of the loop. Task Utilization whileTrue Infinite Loop mates Breakstatement, calculated 1 + 2 + 4 + 8 + 16 + ... The first 20 items of the and. Sum=0x=1N=1 whileTrue:sum+=x x*=2N+=1ifN>20:         BreakPrintSum
Continue continues the loop during the loop, you can use break to exit the current loop, or you can skip the subsequent loop code with continue and proceed to the next loop. Let's say we've written the code that calculates the average score with a For loop: L= [75, 98, 59, 81, 66, 43, 69, 85]sum= 0.0N=0 forXinchL:sum= Sum +x N= n + 1PrintSum/N Now the teacher just wants to count the average score of passing scores, it's going to x< 60 of the score is removed, then the useContinue, you can do it when x <60, do not continue to execute the loop body subsequent code, directly into the next loop: forXinchL:ifX < 60:        Continuesum= Sum +x N= n + 1

multiple loops can also nest loops inside the loop, so let's take a look at an example: forXinch['A','B','C']:     forYinch['1','2','3']:        PrintX +Yx each cycle, Y will cycle3times, so that we can print out a full array: a1a2a3b1b2b3c1c2c3 for two digits within 100, use a double loop to print out all 10 digit numbers smaller than the single digit number, for example,(2 < 3) forXinch[1,2,3,4,5,6,7,8,9 ]:     forYinch[0,1,2,3,4,5,6,7,8,9]:        ifY>x:Print "%d%d\n"% (x, y)

Python loop structure

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.