局部变量 和 全局变量
(1)、局部函数:
#!/usr/bin/python
def fun():
x = 100 ##定义一个内部的函数是 x = 100,只在fun() 内部有效。
print x
fun()
执行结果:
[[email protected]zhdya01 python]# python 1.py
100
(2)、全局函数可被局部函数调用
[[email protected]zhdya01 python]# vim 1.py
#!/usr/bin/python
x= "global var"
def fun():
print x
fun()
执行结果:
[[email protected]zhdya01 python]# python 1.py
global var
(3、)定义某个值为全局变量
[[email protected] python]# vim 1.py
#!/usr/bin/python
x= 200
def fun():
x += 11
print x
fun()
[root@zhdya01 python]# python 1.py
Traceback (most recent call last):
File "1.py", line 8, in <module>
fun()
File "1.py", line 5, in fun
x += 11
UnboundLocalError: local variable 'x' referenced before assignment
如上报错是因为x虽然是全局变量,但是仅限于在函数内打印,不可以直接调用,我们需要声明下:
[[email protected] python]# vim 1.py
[[email protected] python]# python 1.py
211
[[email protected] python]# cat 1.py
#!/usr/bin/python
x= 200
def fun():
global x ##宣称x为全局变量
x += 11
print x
fun()
(4)、定义局部变量为全局变量
#!/usr/bin/python
x= 200
def fun():
global x ##强制声明为全局变量
x += 11
print x
global y
y = 1
fun()
print x
print y
输出结果:
211
211
1
(5)、函数变量
#!/usr/bin/python
x= 200
def fun():
x = 11
y = 1
print locals() ##以字典的形式返回变量的值
fun()
输出结果:
{'y': 1, 'x': 11}
[[email protected]zhdya01 python]# cat 1.py
#!/usr/bin/python
def fun():
print "hello python!"
print fun()
输出结果:
不但输出了 hello python! 而且还输出了一个返回值 None
[[email protected]zhdya01 python]# python 1.py
hello python!
None
自定义返回值:
[[email protected]zhdya01 python]# cat 1.py
#!/usr/bin/python
def fun():
print "hello python!"
return True
print fun()
输出结果:(返回值可以为任意,例如:字符串,数字,等)
[[email protected]zhdya01 python]# python 1.py
hello python!
True
一旦return执行后,函数终止:
[[email protected]zhdya01 python]# cat 1.py
#!/usr/bin/python
def fun():
print "hello python!"
return True
print "abc"
print fun()
输出结果:(返回值可以为任意,例如:字符串,数字,等)
[[email protected]zhdya01 python]# python 1.py
hello python!
True
打印linux系统的进程:
#!/usr/bin/python
import os
import sys
def isNum(p):
for i in p:
if i not in '0123456789':
return False ##如果不是数字,则返回值为 False
else:
return True ##返回值为 True
for i in os.listdir('/proc'):
if isNum(i): ## 相当于 if Ture:
print i
简写一下如上的脚本:(不需要for循环来遍历了)
#!/usr/bin/python
import os
import sys
def isNum(p):
if p.isdigit(): ##如果p这个序列里面全部是 数字 则返回一直值:
return True
return False
for i in os.listdir('/proc'):
if isNum(i):
print i
使用到了一个 方法: S.isdigits
#s为字符串
# s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False。
# s.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False。
# s.isdigit() 所有字符都是数字,为真返回 Ture,否则返回 False。
# s.islower() 所有字符都是小写,为真返回 Ture,否则返回 False。
# s.isupper() 所有字符都是大写,为真返回 Ture,否则返回 False。
# s.istitle() 所有单词都是首字母大写,为真返回 Ture,否则返回 False。
# s.isspace() 所有字符都是空白字符,为真返回 Ture,否则返回 False。
# 习题
#
# 1. 设计一个程序,从终端接收10个数字,并使用自己编写的排序函数,对10个数字排序后输出
def sss():
n = 0
list1 = []
while n < 5:
i = raw_input("pls input some numbers: ")
n+=1
list1.append(i)
len1 = len(list1)
print len1
for i in xrange(0, len1):
for j in xrange(i+1, len1):
if int(list1[i]) > int(list1[j]):
list1[i], list1[j] = list1[j], list1[i]
return list1
print sss()
# 2. 设计一个函数,接收一个英文单词,从文件中查询该单词的汉语意思并返回