python的循环，pass和DocString

362人阅读 评论(0)

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


>>>
enter a number: 2
lower
enter a number: 44
higher
enter a number: 23
guess right!
the loop1 is over
1
4
7
the 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)

>>>
None
45
>>> 

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]<goal:
low=mid+1
else: return mid
return -1
a=[3,5,7,10,23,45]
print midfind(a,0,5,5)+1
print (midfind.__doc__) #midfind.__doc__ is the document strings,
#note: multi-line for __

>>>
2
this is a half find for ourneed number,
the return is goal number's place.
>>>


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：329671次
• 积分：8834
• 等级：
• 排名：第2518名
• 原创：575篇
• 转载：13篇
• 译文：0篇
• 评论：36条
我的链接
评论排行
最新评论