Python looping statements differ from other programming languages

Source: Internet
Author: User

1. Local Variables

 for  in range (5):    print  i,print I,

Operation Result:

0 1 2 3 4 4

I is a local variable inside the for statement. In Python, however, within the same method, a local variable is defined, and the scope of the variable is defined as the beginning of the line to the end of the method body.

In other programming languages, the phrase "print I" is wrong, because I is not defined

Example 1:

def func ():     =    if a >        :=    Trueprint  bif__ name__'__main__':    func ()

Results:

True

Example 2:

def func ():     =    if a >        :=    Trueprint  bif__ name__'__main__':    func ()    print b

The last line is wrong because the B in the B,func () method is not defined as a local variable in the function body, so the "print B" inside the main is wrong.

2, Python for loop control statement

Example 1:

 for  in range (5):    A in range (6):         print  (i,j),     Print

Operation Result:

(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5)
(1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5)
(4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5)

Example 2:

Prime numbers between [50,100]

ImportMathcout=0 forIinchRange (50,100+1):     forJinchRange (2,int (math.sqrt (i)) +1):        ifI% J = =0: Break    Else:        PrintI, cout+=1ifcout% 10 = =0:cout=0Print        #Break #此处不能加break, otherwise the forbreak will be outside because the else at this level is side -by-side with the second for

Operation Result:

53 59 61 67 71 73 79 83 89 97

Analytical:

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

If the For loop is not terminated by a break, the statement in else is executed. For to terminate the For loop when needed.

The format for the For statement is as follows:

 for  in < objects collection >:    if < condition 1>:        break    if < Condition 2>:        Continue    < other statements >else:    <...>

Python looping statements differ from other programming languages

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.