The continue of Python continues to circulate

Source: Internet
Author: User

The continue of Python continues to circulate

During the loop, you can use break to exit the current loop, and you can use continue to skip the subsequent loop code and continue the next loop.

Let's say we've written the code that calculates the average score using the For loop:

L = [98, M, M, ba, 85]sum = 0.0n = 0 inL:    sum = sum + x    n = n + 1print sum/n

Now the teacher just want to count the average of passing scores, it is necessary to cut off the score of x < 60, then, using continue, can be done when x < 60, do not continue to execute the loop body follow-up code, directly into the next cycle:

For x in L:    if x <:        continue    sum = sum + x





Python's multiple loops

Inside the loop, you can also nest loops, let's look at an example:

In [' A ', ' B ', ' C ']: in   [' 1 ', ' 2 ', ' 3 ']:        print x + y

X cycles once,y loops 3 times, so we can print out a full array:

A1
A2
A3
B1
B2
B3
C1
C2
C3

    n = n + 1

The continue of Python continues to circulate

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.