defuna():print("i love una")print("i love guoguo")print("i love yangyu")print("i love gpc")
una()# i love una# i love guoguo# i love yangyu# i love gpc
defuna():print("i love una")print("i love guoguo")returnprint("i love yangyu")print("i love gpc")
una()# i love una# i love guoguo
defuna():print("i love una")print("i love guoguo")print("i love yangyu")print("i love gpc")return"大爱无疆"
love = una()print("你得返回值是",love)# i love una# i love guoguo# i love yangyu# i love gpc# 你得返回值是 大爱无疆
defsuzi():
a =int(input(">>>"))
b =int(input(">>>"))if a>b:return a
else:return b
c = suzi()print(c)# >>>123# >>>456# 456#--------------# >>>888# >>>555# 888
# 4 、 函数的参数defuna(love):# 形参print("i love %s"%love)print("i love guoguo")print("i love yangyu")print("i love gpc")return"大爱无疆"
una("雨雨")#实参# i love 雨雨# i love guoguo# i love yangyu# i love gpc
a =10# 全局变量deffunc():
b =20# 局部变量:内部的变量,外界无法直接访问print(locals())#查看当前作用域中内容
func()# {'b': 20}print(a)# 10print(globals())#查看全局作用域中的变量:变量,函数名,模块名,类名,对象名。。。# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000028E5E1880B8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/py/函数/函数.py', '__cached__': None, 'a': 10, 'func': <function func at 0x0000028E5E476378>}
a =10deffunc():print(a)
func()#10
# 6、global和 nonlocal
# global 把全局变量引入到局部,可以改变全局变量的值
# nonlocal 把局部变量引入到局部,就近原则
# 局部修改全局
a =10deffunc1():global a # 把全局变量引入局部(函数内部)
a = a +10
func1()print(a)# 20
a =[]deffunc1():global a # 把全局变量引入局部(函数内部)
a =10
func1()print(a)# 10
a =10deffunc1():
a =20deffunc2():nonlocal a #把局部变量引入到局部,就近原则
a = a +10
func2()print(a)
func1()# 30print(a)# 10
a =1deffunc1():
a =2deffunc2():nonlocal a #把局部变量引入到局部,就近原则
a =3deffunc3():
a =4print(a)print(a)
func3()print(a)print(a)
func2()print(a)print(a)
func1()print(a)# 1# 2# 3# 4# 3# 3# 1