Python control statements are different from control statements in other languages. some philosophical ideas of python are embodied in their control statements. 1. for example, python advocates simple and practical ideas, it does not have a switch statement. if you want to implement a switch statement
You can write
(1) implemented through the if elif statement
If condition:
...
Elif condition:
...
Else:
...
(2) pass
The code is as follows:
Info = {}
Info = {'a': '1', 'B: 2, 'C: 3, 'default': SS '}
C = info. get ('A', default ')
The 2while statement has an else statement.
A = 2
While a> 1:
Print 'success'
Else:
Print 'error'
When the while loop condition changes to False, the else block is executed -- this may even be the first time the condition is verified.
3. the for statement has an else statement.
The code is as follows:
For I in xrange (5 ):
Print I
Else:
Print 'loop is end'
The result is 0 1 2 3 4 loop is end.
Remember, the else part is optional. If else is included, it is always executed once after the for loop ends,
If you encounter a break statement, the else statement after for will not be executed.
The code is as follows:
For I in xrange (5 ):
If I = 3:
Print 'success'
Else:
Print 'loop is end'
The result is success.
This article is from: Rootexp