Day 04 Process Control

1. if determined:

  1.if...else...:  

if the condition is determined:   # Condition True or False is determined 
    statement 1     # code block. Indent 4 spaces 
    statement 2 
    statement 3 
the else :     # can not be used alone. Must be used with if while for 
    statement 1 
    statement 2 
    statement 3

  Such as:

. 1 = A 
B = 2
 IF A> B:    # is not satisfied, to False, the block does not enter 
    Print (A)
 the else :    # established, is True, for internal program execution 
    Print (B)   # 2

  2. When a plurality of if ... elif ... else conditional

if Condition 1:     
    statement 1 
    statement 1 
elif Condition 2:   # can have multiple elif judgment statement, given the number of conditional 
    sentence 1 
    sentence 2 
the else : 
    statement 1 
    statement 2 

#   at the same level no matter how many conditions only for the implementation of a conditional true inside the statement, program execution from top to bottom

Such as:

. 1 = A 
B = 2
 IF A - B <0:    # established to True, the block of code into the 
    Print (A)    # . 1 
elif A - B ==. 1:   # Although the establishment, but since only one execution condition determination is true inside statement, the program executed from top to bottom, it does not enter the code block 
    Print (0)
 the else :    # is not established, to False, the block does not enter 
    Print (B)   # 2

2. while loop

  1: while the format:

while conditional:   # If it is determined the conditions is True while entering in the statement are not met out of the loop 
    statement 1 
    statement 2

Such as:

= 1 A
 the while A <= 10 :
     Print (A) 
    A + 1 =    # increments by 1 when a = 11 when the condition is not satisfied, out of the loop

  2: continue: out of the loop for the next cycle judgment

= A 0
 the while A <10 : 
    A + 1 =    # increments by one, when the condition is not satisfied a = 10, out of the loop 
    IF a == 4 :
         Continue   # when a == 4, the direct out of the loop , re-entered the loop judgment condition is not performed the print statements, 
    print (A)

  3: break: terminate the loop

= A 0
 the while . 1:   # when True all non-empty, the data type corresponds to non-zero, i.e. constant equal to True endless loop. 
    Print (A) 
    A + =. 1
     IF a == 10 :
         BREAK   # when a == 10 execution break, the loop is terminated

    PS: continue and break are only the same level with him entry into force of the loop

  4: while ... else ... When the cycle is terminated break will not be executed else the following statement, if the program out of the loop will execute normal

= A 0
 the while A <10 :
     Print (A, End = '  ' ) 
    A +. 1 =
     IF A ==. 4 :
         BREAK     # BREAK interrupted. Go else statement 
else :
     Print ( " non-normal interrupt cycle " ) 
  
  
  # 0123
= A 0
 the while A <10 :
     Print (A, End = '  ' ) 
    A + =. 1 else :    # cycles BREAK not interrupted, terminated normally, the result is executed else statement: 0123456789 interrupted abnormally cycle Print ( " non-normal interrupt cycle " )


    

3 . for

  1.for cycle format:

for the variable name in container types: 
    statement 1 
    statement 2 
    statement 3

Such as:

list1 = [1, 2, 3, 4]
for i in list1:
    print(i)

   2: len () Method: Get Type of vessel length (number of elements), the dictionary is to obtain the number of key-value pairs, the string is to obtain the number of characters in the string

  

list1 = [1, 2, 3, 4]
dict1 = {
    'name': 'yanglingyao',
    'age': 18,
    'hobby': ['sleep', 'play']
}
str1 = 'ni2u9_1%k'
print(len(list1))  # 4
print(len(dict1))  # 3
print(len(str1))  # 9

   . 3: range () Method: range function is an iterator can save memory space, care regardless tail

    In python2.x versions range function will directly generate a list, take up memory space large

             xrange () python3 in the range () function is the same

 

Guess you like

Origin www.cnblogs.com/yanglingyao/p/11121759.html