1 局部变量和全局变量
pro='计算机信息管理'
name='吴老师'
def printInfo():
name='peter'
print('{}.{}'.format(name,pro))
pass
def TestMethos():
name='刘德华'
print(name,pro)
pass
printInfo()
TestMethos()
def changeGlobal():
"""
修改全局变量
pro='市场营销'
"""
global pro
pro='市场营销'
pass
changeGlobal()
print(pro)
2 函数的参数传入值
a=1
def func(x):
print('x的地址{}'.format(id(x)))
x=2
print('x的地址{}'.format(id(x)))
pass
print('a的地址:{}'.format(id(a)))
func(a)
print(a)
li=[]
def testRenc(parms):
print(id(parms))
li.append([1,3,4,5,3,53])
print(id(parms))
print('内部的{}'.format(parms))
pass
print(id(li))
testRenc(li)
print('外部的{}'.format(li))
3 匿名函数
def computer(x,y):
"""
计算数据和
:param x:
:param y:
:return:
"""
return x+y
pass
computer(10,55)
print(computer(10,55))
M=lambda x,y:x+y
print(M(23,19))
result=lambda a,b,c:a*b*c
print(result(12,23,3))
age=15
print('可以参军'if age>18 else '继续上学' )
funcTest=lambda x,y:x if x>y else y
print(funcTest(12,2))
rs=(lambda x,y:x if x>y else y)(16,12)
print(rs)
varRs=lambda x:(x**2)+890
print(varRs(10))
4 递归函数
def jiecheng(n):
result=1
for item in range(1,n+1):
result*=item
pass
return result
print('5的阶乘{}'.format(jiecheng(5)))
def diguiJc(n):
"""
递归实现
:param n:阶乘参数
:return:
"""
if n==1:
return 1
else:
return n*diguiJc(n-1)
pass
diguiJc(5)
print('5的阶乘{}'.format(diguiJc(5)))
import os
def findFile(file_Path):
listRs=os.listdir(file_Path)
for fileItem in listRs:
full_path=os.path.join(file_Path,fileItem)
if os.path.isdir(full_path):
file_Path(full_path)
else:
print(fileItem)
pass
pass
else:
return
pass
findFile('F:\\Python1\\Day_1')
|