If statement
Format of an If statement
The statement block must have the same indentation.
Statement block must be indented more than if,elif,else
# If conditions are true, EXECUTE statement block 1,# Otherwise, if condition 2 is established, EXECUTE statement block 2# Other cases EXECUTE statement block 3# Elis and else part are optional if condition 1: statement block 1ELIF Condition 2: statement block 2else: statement block 3
Second, the example
i = 10if i = = 3:print ' i is 3. ' Print "I also execute after if." "Elif i < 3:print ' I < 3 ' else:print ' other cases. ' Print ' prints end. '
III. Matters of note
1. There is no switch statement in Python, and switch can use If...elif...else to implement 2. If, elif, the else must have a colon (:), after which the code needs to add a layer of indentation
While loop
As long as a condition is true, the while statement allows you to repeat the execution of a single statement. A while statement is an example of a so-called circular statement. The while statement has an optional ELSE clause.
Using the While statement
Example 6.2 using the while statement
| The code is as follows |
Copy Code |
| #!/usr/bin/python # Filename:while.py Number = 23 running = True While running: guess = Int (raw_input (' Enter an Integer: ')) If guess = = number: print ' Congratulations, you guessed it. ' running = False # This causes the while loop to stop Elif Guess < number: print ' No, it is a little higher than ' Else print ' No, it is a little lower than ' Else print ' The while loop are over. ' # do anything else your want to did here print ' Done ' (source file: code/while.py) Output $ python while.py Enter an integer:50 No, it is a little lower than. Enter an integer:22 No, it is a little higher than. Enter an integer:23 Congratulations, you guessed it. The while loop are over. Done |
For statement
For.. In is another loop statement that uses each item in the queue recursively on the object of a sequence. We will study the sequence in more detail in the later chapters.
Use for statement
Example 6.3 uses a for statement
| The code is as follows |
Copy Code |
#!/usr/bin/python # Filename:for.py For I in range (1, 5): Print I Else print ' For loop are over ' Output $ python for.py 1 2 3 4 The For loop are over |
Break and Continue statements
A break statement is used to terminate a circular statement, even if the loop condition is not called false or the sequence is not fully recursive, and the execution of the loop statement is stopped.
An important comment is that if you terminate from a for or a while loop, any corresponding loop else block will not execute.
Use the break statement
Example 6.4 using the break statement
| The code is as follows |
Copy Code |
#!/usr/bin/python # Filename:break.py While True: s = raw_input (' Enter something: ') if s = = ' quit ': Break print ' Length ' of the string is ', Len (s) print ' Done ' |
The continue statement is used to tell Python to skip the remaining statements in the current loop block and then proceed to the next round of loops.
Using the Continue statement
Example 6.5 using the Continue statement
| code is as follows |
copy code |
| #!/usr/bin/python # Filename:continue.py While True: & nbsp; s = raw_input (' Enter something: ') if s = = ' quit ': & nbsp; break If Len (s) < 3: continue print ' sufficient length ' # do kinds's processing here ... (source file: code/continue.py) Output $ python continue.py Enter something:a Enter something:12 Enter SOMETHING:ABC Input is of Sufficient length Enter something:quit |