Python notes (circular statements)

Source: Internet
Author: User

There are two basic loops, for loops and which loops

For Loop basic format:

The For statement is a circular control statement in Python. Can be used to traverse an object and also have an optional else block attached, which is used primarily to handle the break statements contained in a for statement.

If the For loop is not terminated by a break, the statement in the else block is executed.

Break terminates the For loop when needed

Continue skips the following statement and starts the next round of loops.

The For statement is formatted as follows:

>>>for <> in < object collection:

.. if < conditions:

.. break

.. if < conditions:

... continue

... < other statements >

. else:

... <>

...


Instance:

For i in [1,2,3,4,5]:
Print (i)
The output is: 12345

An If statement can be nested in a For statement, terminated or skipped using break and continue when execution criteria are judged.
The above instance is rewritten:
For i in [1,2,3,4,5]:
If i==1:
Break
Else
Print (i)
Returns the result is not output any content, because executes to i=1 the time terminates, if changes the statement to i==5, the output result is 1234.
Instance Overwrite:
For i in [1,2,3,4,5]:
If i==5:
Break
Elif i==2:
Continue
Else
Print (i)
The return result is 134.

The collection of objects in a for statement can be a list, a dictionary, or a tuple, or you can generate a list of integers by using the range () function. The Range function prototype is: Range (start,stop,step) is the start, end, step.

Instance:

For I in Range (1,10,2):
Print (i)
The output result is 13579. If you change to range (1,10), the default step is 1, and the output is 1-9.


While statement

The while statement is also a circular control statement, while the statement terminates only when the condition is false.

Basic format:

While condition:
EXECUTE statement
changing conditions

Instance:

N=1
While n<10:
Print (n)
N=n+1
Output is 123456789

About the For loop control statement in Python
# First: Ask for prime numbers between 50-100

Import Math
For I in range (50, 100 + 1):
For j in range (2, int (math.sqrt (i)) + 1):
If I% j = 0:
Break
Else
Print I

# Second: Put else's position in the same indent as if.

Import Math
For I in range (50, 100 + 1):
For j in range (2, int (math.sqrt (i)) + 1):
If I% j = 0:
Break
Else
Print I

# Third: Add a break statement after else.

Import Math
for I in range (+ 1):
    to J in range (2, int (math.sqrt (i)) + 1):
  ;       if I% j = 0:
             Break
        Else:
             print I
            break

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.