python学习笔记7-函数返回值
函数被调用后会返回一个指定的值
函数调用后默认返回None
return返回值可以是任意类型
return执行后,函数终止
return与print区别
[root@133 function]# vim return.py
#/usr/bin/python
def fun():
print "hello world"
print fun()
[root@133 function]# python return.py
hello world
None #fun()函数没有指定返回值,默认返回值是none
[root@133 function]# vim return.py
#/usr/bin/python
def fun():
print "hello world"
return True #执行return后,函数结束退出,不执行print‘abc’
print 'abc'
print fun() #执行print fun()打印返回值True
[root@133 function]# python return.py
hello world
True
使用return指定isNum()的函数返回值是true或者false,Help on built-in function isdigit:
In [1]: a = 'abc'
In [2]: a.isdigit() #isdigit 返回值是true或者false?
Out[2]: False
In [3]: b=123
In [4]: b = '123'
In [5]: b.isdigit()
Out[5]: True
In [6]: help(b.isdigit)
isdigit(...)
S.isdigit() -> bool
Return True if all characters in S are digits #s内所有的值都是数字
and there is at least one character in S, False otherwise.
(END) #s内至少一个是char类型
[root@localhost ~]# vim return.py
#!/usr/bin/python
import os
import sys
def isNum(s):
if s.isdigit():
return True #判断都是数字True,然后直接退出程序
return False #如果有字符,没有返回True,执行reurn False
for i in os.listdir('/proc'):
if isNum(i):
print i