Python Loop statement

Source: Internet
Author: User

Like many other high-level programming languages, Python also provides looping statements, which makes it easy to do many of the same things. The loop statement in Python includes a for loop and a while loop.

1. for Loops

The For statement is used to traverse the elements within a sequence object and run a block of code on each element. The format is as follows:

for variable name in sequence:

statement block ...

Case: Print a number that is larger than a single digit in a two-digit number within 100.

Code:

Results:

In the preceding code, a loop is used to traverse all two digits from 10 to 100. There are two if judgment statements in the loop body, where the first statement is used to check whether the number satisfies the 10-bit number is smaller than the digit number, and the other Judgment statement is written to facilitate us to view the results of the operation, which is to wrap every 10 numbers. In this program we use the range () function, and in the Pycharm Edit program interface, enter help (range) and run to get the following result:

As you can see, the range (I,J) function returns a sequence object starting with I, but not including J.

2. while Loops

Statement format:

while Judging Condition:

EXECUTE statement

The execution statement can be a single statement or a block of statements. The execution statement executes once when the condition is true, and then the condition is judged again. The loop ends until the condition false false is judged.

Case code:

Results:

When the judgment condition in the while statement is true, the print "Hello" statement is executed, flag minus 1 after printing, and when flag=0, the condition is false and the loop body is no longer executed. Finally, the end of the loop is printed.

in Python Medium, while .. else perform the else after the loop execution finishes statement block. is as follows:

Code:

Results:

3. Break and Continue Statement

The break is used to end the statement in which the loop is located, and thecontinue statement jumps out of the loop of the body, making the next loop directly. As shown below:

Code:

Results:

the loop in which the break is located is the outermost for loop statement, so the outer loop executes only once and the print result is a1,a2,a3.

We will make simple changes to the above code as follows:

Code:

Results:

The break statement in the inner loop has a value of only 1 for each time, so the print result is a1,b1,c1.

Make the above code a simple modification again, as follows:

Code:

Results:

continue statement directly out of the loop body of this cycle, that is, x=b, into the next cycle, that is, x=c, so the above printing results.

Python Loop statement

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.