Python continue, else and pass

来源:互联网 发布:逍遥模拟器清理数据 编辑:程序博客网 时间:2024/05/21 12:05

The continue Statement:

The continue statement in Python returns the control to the beginning of the while loop. The continuestatement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

The continue statement can be used in both while and for loops.

Example:

#!/usr/bin/pythonfor letter in 'Python':     # First Example   if letter == 'h':      continue   print 'Current Letter :', lettervar = 10                    # Second Examplewhile var > 0:                 var = var -1   if var == 5:      continue   print 'Current variable value :', varprint "Good bye!"

This will produce following result:

Current Letter : PCurrent Letter : yCurrent Letter : tCurrent Letter : oCurrent Letter : nCurrent variable value : 10Current variable value : 9Current variable value : 8Current variable value : 7Current variable value : 6Current variable value : 4Current variable value : 3Current variable value : 2Current variable value : 1Good bye!

The else Statement Used with Loops

Python supports to have an else statement associated with a loop statements.

  • If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.

  • If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

Example:

The following example illustrates the combination of an else statement with a for statement that searches for prime numbers from 10 through 20.

#!/usr/bin/pythonfor num in range(10,20):  #to iterate between 10 to 20   for i in range(2,num): #to iterate on the factors of the number      if num%i == 0:      #to determine the first factor         j=num/i #to calculate the second factor         print '%d equals %d * %d' % (num,i,j)         break #to move to the next number, the #first FOR   else:        # else part of the loop      print num, 'is a prime number'

This will produce following result:

10 equals 2 * 511 is a prime number12 equals 2 * 613 is a prime number14 equals 2 * 715 equals 3 * 516 equals 2 * 817 is a prime number18 equals 2 * 919 is a prime number

Similar way you can use else statement with while loop.

The pass Statement:

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):

Example:

#!/usr/bin/pythonfor letter in 'Python':    if letter == 'h':      pass      print 'This is pass block'   print 'Current Letter :', letterprint "Good bye!"

This will produce following result:

Current Letter : PCurrent Letter : yCurrent Letter : tThis is pass blockCurrent Letter : hCurrent Letter : oCurrent Letter : nGood bye!

The preceding code does not execute any statement or code if the value of letter is 'h'. The passstatement is helpful when you have created a code block but it is no longer required.

You can then remove the statements inside the block but let the block remain with a pass statement so that it doesn't interfere with other parts of the code.


0 0