Python Loop statement notes

Source: Internet
Author: User

While Loop statement

The while statement is used to loop the execution of a program that, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:

 while Judging Condition:    Execute statement ...

The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.

The loop ends when the condition false false is judged.

While statement there are two other important command Continue,break to skip the loop, continue used to skip the cycle, break is used to exit the loop, in addition, "judging condition" can also be a constant value, indicating that the loop must be set up, the following:

#-*-coding:utf-8-*-I= 1 whileI < 10: I+ = 1ifi%2 > 0:#Skip output when non-even        Continue    PrintI#output even 2, 4, 6, 8, tenI= 1 while1:#the cycle condition of 1 must be set    PrintI#Output 1~10i + = 1ifI > 10:#when I is greater than 10 o'clock jump out of the loop         Break

For Loop statement

A For loop can traverse any sequence of items, such as a list or a string.

 for inch sequence:   statements (s)
#for in'Python':   print  i

The output results are as follows:

Python

Looping with Else statements

In Python, for ... else means that the statement in for is no different from normal, while the statement in else is executed when the loop is executed normally (that is, for not breaking out by break), while ... else is the same.

Pass Statement

Pass does not do anything, generally used as a placeholder statement.

#for in'Python':   if'  H':      pass      print'Pass  '   print  i

The output results are as follows:

Pyt Pass Hon

Python Looping statement notes

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.