3 control flow statements in python: if, for, while

1.if statement

if statement structure is as follows: 

if
the condition:   condition is True operation of the else :   conditions for the operation of the False

 For example 1:

= 23 Number The 
GUESS = int (the INPUT ( " Enter an integer: " )) 

IF GUESS == Number The:
     Print ( " ! Congratulations, you guessed it " ) 
        
elif GUESS < Number The:
     Print ( " NO, NO, small small a " ) 

the else :
     Print ( " NO, big big " ) 

Print ( " game Over " )

 

The output is:

>>>
Enter an integer: 5
NO, NO, a little smaller
end of the game
>>> ============================ RESTART ================================ ====
>>>
enter an integer: 25
NO, big big
game is over
>>> ================================ RESTART ========== ======================
>>>
enter an integer: 23
Congratulations, you guessed it!
game over

2.for statement

structure for statement is as follows: 

for
the target in the expression: loop

Example 2:

Example 1: # traversing the name of all the character 
name = ' Kite123 ' 
for I in name:
     Print (I, End = '  ' )
 Print ( " \ n- " ) 

Example 2: # of elements to print the list, and the element length 
Member = [ ' red ' , ' white ' , ' flower mushroom cool ' ]
 for i in Member:
     Print (i, len (i))

The output is:

ITE. 3 2 1 K   # Example 1 record 

red 2   # Example 2 Results 
White 2 
spent mushroom cool 4

3.while statement

Results while statement as follows: 
while
the condition: condition is True perform operation (loop)
= 23 Number The 
running = True 

the while running: 
    GUESS = int (the INPUT ( " Enter an integer: " )) 

    IF GUESS == Number The:
         Print ( " Congratulations, you guessed it! " ) 
        running = False # guessed it, the game end 
        
    elif GUESS < Number the:
         Print ( " NO, NO, a little small " ) 

    the else :
         Print ( " NO, big big " ) 

the else :
     Print ( "Game Over " )

The output is:

>>>  
Enter an integer: 5 
NO, NO, a little smaller 
Enter an integer: 30 
NO, big big 
Enter an integer: 23 
Congratulations, you guessed it! 
game over

4.break和continue

break out of the current cycle, continue terminates the current round of circulation, begin the next cycle

4.1 break Statement

Or for termination from the while loop, any corresponding loop else block is not executed.

E.g:

the while True: 
    S = (INPUT ( " the Enter something: " ))
     IF S == ' quit ' :
         BREAK 
    Print ( ' the length of the output string ' , len (S))
 Print ( ' the Done ' )
        

Output:

>>>  
the Enter something: quit 
Done
 >>> ================================ RESTART ====== ========================== 
>>>  
the Enter something: 123 
length of the output string . 3 
the Enter something: quit 
the Done

4.2 contiue statement

the while True: 
    S = (INPUT ( " the Enter something: " ))
     IF S == ' quit ' :
         BREAK 
    IF len (S) <. 3 :
         Print ( ' Too Small ' )
     Continue 
    Print ( " length of the output string ' , len (S))
 Print ( ' the Done ' )

The output is:

>>> ================================ RESTART ================================
>>>
Enter something:12
Too small
Enter something:123
Enter something:1234
Enter something:quit
Done

 

Guess you like

Origin www.cnblogs.com/kite123/p/11357164.html