Unique python loop statements and python statements

Source: Internet
Author: User

Unique python loop statements and python statements

1. Local Variables

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

Running result:

0 1 2 3 4 4

I is the local variable in the for statement. However, in python, a local variable is defined in the same method body. The scope of the variable is to define the row and end with the method body.

In other programming languages, the "print I" clause is incorrect because I is not defined

Example 1:

def func():  a = 100  if a > 50:    b = True  print bif __name__ == '__main__':  func()

Result:

True

Example 2:

def func():  a = 100  if a > 50:    b = True  print bif __name__ == '__main__':  func()  print b

The last row is incorrect. Because B is not defined, B in the func () method is a local variable in the function body, so the "print B" in main is incorrect.

2. python for loop control statements

Example 1:

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

Running 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:

Calculate the prime number between [50,100]

Import mathcout = 0for I in range (50,100 + 1): for j in range (2, int (math. sqrt (I) + 1): if I % j = 0: break else: print I, cout + = 1 if cout % 10 = 0: cout = 0 print # break # The break cannot be added here; otherwise, the outer forbreak is used because the else at this level is in a side-by-side relationship with the second

Running result:

53 59 61 67 71 73 79 83 89 97

Resolution:

A for statement is a loop control statement in python. It can be used to traverse an object and has an optional else block. It is mainly used to process for statements containing break statements.

If the for loop is not terminated by break, the else statement is executed. For ends the for loop as needed.

The format of the for statement is as follows:

For <> in <object set>: if <condition 1>: break if <condition 2>: continue <other statements> else: <...>

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.