博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python continue, else and pass
阅读量:4031 次
发布时间:2019-05-24

本文共 3228 字,大约阅读时间需要 10 分钟。

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.

转载地址:http://zehbi.baihongyu.com/

你可能感兴趣的文章
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
POJ 2363 Blocks(我的水题之路——立方体体积和表面积,暴力)
查看>>
POJ 2390 Bank Interest(我的水题之路——double和floa计算差别)
查看>>
POJ 2459 Feed Accounting(我的水题之路——英文题啊!!!)
查看>>
POJ 2470 Ambiguous permutation(我的水题之路——位置和值的队列)
查看>>
POJ 2498 StuPId(我的水题之路——from back to front- -!)
查看>>
POJ 2535 Very Simple Problem(我的水题之路——看错题)
查看>>
POJ 2538 WERTYU(我的水题之路——键盘错位)
查看>>
POJ 2551 Ones(我的水题之路——重点,末尾有几个1)
查看>>
POJ 2562 Primary Arithmetic(我的水题之路——模拟加法进位)
查看>>
POJ 2575 Jolly Jumpers(我的水题之路——数组绝对差值为1到n-1)
查看>>