python函数之作用域:
>>> def times(x,y):
return x*y
>>> times(2,4)
8
>>> times(3.1,4)
12.4
>>> times('eop',4)
'eopeopeopeop'
>>> def inset(s1,s2):
res=[]
for x in s1:
if x in s2:
res.append(x)
return res
>>> s1='jacck'
>>> s2='jimmk'
>>> inset(s1,s2)
['j', 'k']
>>> [x for x in s1 if x in s2]
['j', 'k']
>>> x=inset([1,1,2,3],(1,1,3))
>>> x
[1, 1, 3]
>>> x=99
>>> def func(y):
z=x+y
return z
>>> func(1)
100
>>> x=88
>>> def func():
x=99
>>> func()
>>> print(x)
88
>>> x=88
>>> def func():
global x
x=99
>>> func()
>>> print(x)
99
>>> y,z=1,2
>>> def glo():
global x
x=y+z
>>> glo()
>>> print(x)
3