C++猿的Python笔记02控制流

注意冒号  

  if else  

if ... :

缩进

else :

缩进 

if guess == number:
    
print 'Congratulations, you guessed it.' # New block starts here
    running = False
elif guess < number:
    
print 'No, it is a little higher than that' # Another block
    # You can do whatever you want in a block ...
else:
    
print 'No, it is a little lower than that'
    
# you must have guess > number to reach here

 

  switch  

没有switch,用if..: elif ..: else: 代替

  while   

while xx:

缩进 

else:

缩进 

while done == False:
    i 
= i+1;
    
print 'frame', i
else:
    
print 'The while loop is over.'

  

  for  

for element in arrays:

缩进

else:

缩进 

for i in range(15):
print i
else:

print 'The for loop is over'  


  break   

终止循环。

注意:如果你从for或while循环中终止 ,任何对应的循环else块将不执行。 

  continue   

逃过本次循环后面的语句,进入下一轮循环。

注意:continue语句对于for循环也有效。 

原文地址:https://www.cnblogs.com/mumuliang/p/2130977.html