Python control statement

  • 2020-04-02 09:43:33
  • OfStack

For example, python advocates the simple and practical idea that it does not have a switch statement if the effect of the switch statement is to be realized
There are two ways to write it
(1) through if elif statement to achieve
If conditions:
...
Elif conditions:
...
The else:
...
(2)
 
info = {} 
info = { ' a':'1 ' ,'b:2,'c:3,'default':ss'} 
c = info.get( ' a',default') 
2while Statements are else statements  
a = 2 
while a > 1: 
print  ' success' 
else: 
print  ' error' 

When the while loop condition becomes False, the else block is executed -- this may even be when the condition is first checked.
The 3 for statement has an else statement
 
for i in xrange(5): 
print i 
else: 
print  ' loop is end' 

So 0, 1, 2, 3, 4 is the end
Remember, the else part is optional. If you include else, it always executes once after the for loop ends,
If a break statement is encountered, the else statement after for will not execute
 
for i in xrange(5): 
if i == 3: 
print  ' success' 
else: 
print  ' loop is end' 

The result is success

This article is from Rootexp

Related articles: