- 创建与调用
- 内存
- 返回值
- 有默认值的参数
- 参数个数可变
- 参数传递
- 参数传递顺序
- 变量作用域
- 递归
def cal(a,b):
c = a-b
return c
result = cal(3,2)
print(result)
result = cal(b=2,a=3)
print(result)
def fun(a,b):
a = 66
b.append(88)
print(a,b)
x = 12
y = [1,2,3]
fun(x,y)
print(x,y)
def clasify(n):
odd = []
even = []
for i in n:
if i%2==0:
even.append(i)
else:
odd.append(i)
return odd,even
lst = [1,4,7,3,8,2,9,0]
print(clasify(lst))
def fun(a,b=10):
print(a,b)
fun(100)
fun(20,30)
print('hello')
print('world')
print('hello',end='\t')
print('world')
def fun1(*args):
print(args)
fun1(10)
fun1(1,2,3)
def fun2(**args):
print(args)
fun2(a=10)
fun2(a=1, b=2, c=3)
print('hello','world')
'''def fun3(**args1, *args2):
pass # error
两种都有,只能把位置形参放前边
'''
def fun3(*args1, **args2):
pass
def fun(a,b,c,d):
print('a:',a)
print('b:',b)
print('c:',c)
print('d:',d)
fun(1,2,3,4)
fun(a=1,b=2,c=3,d=4)
fun(1,2,d=4,c=3)
def fun(a,b,*,c,d):
print('a:',a)
print('b:',b)
print('c:',c)
print('d:',d)
fun(1,2,c=3,d=4)
def fun(a,b,c):
print('a:',a)
print('b:',b)
print('c:',c)
fun(1,2,3)
fun(a=1,c=3,b=2)
lst=[1,2,3]
fun(*lst)
dict={'a':1,'b':2,'c':3}
fun(**dict)
def fun(a,b,*,c,d,**args):
pass
def fun(*args1,**args2):
pass
def fun(a,b=10,*args,**args2):
pass
def fun():
global age
age = 21
print(age)
fun()
print(age)
def fac(n):
if n==1:
return 1
else:
return n*fac(n-1)
print(fac(3))
def feibo(n):
if n==1 or n==2:
return 1
else:
res = feibo(n-1)+feibo(n-2)
return res
print(feibo(6))
for i in range(1,7):
print(feibo(i))
|