python的循环,pass和DocString

网友投稿 610 2022-08-27

python的循环,pass和DocString

python的循环,pass和DocString

先来说说最简单的while循环和for循环:

while循环和C的相似性更高:

while 1>0: guess=int(input('enter a number: ')) if guess==23: print 'guess right!' break elif guess>23: print 'higher' else : print 'lower'print 'the loop1 is over'

注意for的写法:

for i in range(1,10,3): # equal to C: for(int i=1;i<10;i+=2) print ielse: # else statement can follow loop print 'the loop2 is over'

它们的输出:

>>> enter a number: 2lowerenter a number: 44higherenter a number: 23guess right!the loop1 is over147the loop2 is over>>>

pass在python函数中代表不返回值,或者说是返回None,类似于C的return ;

def maxnum(a,b): if a>b: return a else : pass #return none statement print maxnum(23,45)print maxnum(45,23)>>> None45>>>

接下来聊聊DocString:

其全称是documentation strings。它放在函数的第一行,能输出提示信息,帮助函数更容易理解,使用函数的__doc__(双下划线)来输出信息。

def midfind(a,lx,rx,goal): ''' this is a half find for ourneed number, the return is goal number's place.''' low=lx high=rx while low<=high: mid=(low+high)>>1 if a[mid]>goal: high=mid-1 elif a[mid]>> 2 this is a half find for ourneed number, the return is goal number's place.>>>

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如果编程语言是超级英雄……
下一篇:mysql 常见问题:ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost' (10061)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~