#函数
def add(a,b):
c=a+b
return c
result=add(10,20)
print(result)
res=add(a=11,b=22)
print(res)
def fun(arg1,arg2):
print('arg1',arg1)
print('arg2', arg2)
arg1=100
arg2.append(10)
print('arg1', arg1)
print('arg2', arg2)
n1=11
n2=[22,33,44]
print('n1',n1)
print('n2',n2)
fun(n1,n2)
print('n1',n1)
print('n2',n2)
def fun(num):
odd=[]
even=[]
for i in num:
if i%2:
odd.append(i)
else:
even.append(i)
return odd,even
lst=[1,2,3,5]
print(fun(lst))
def f(x,y=100):
print(x,y)
f(20,40)
f(88)
def F1(*arg):
print(arg)
F1(1,2,346)
def F2(**n):
print(n)
F2(x1=1,x2=2,x3=346)
def fun1(n):
print(n)
def fun2(*arg1):#输出元组
print(arg1)
def fun3(**arg2):#输出字典
print(arg2)
lst=[1,2,3,4,5,6]
fun1(lst)
fun2(1,2,3,4,5,6)
fun3(a=1,b=2,c=3,d=4,e=5,f=6)
#局部变量用globle声明后会变成全局变量
#计算阶乘
def fac(n):
if n==1:
return 1
else:
return n*fac(n-1)
print(fac(4))
#斐波那契数列
def fib(n):
if n==1:
return 1
elif n==2:
return 1
else:
return fib(n-1)+fib(n-2)
for i in range(1,7):
print(fib(i))