1.1
def x():
for i in range(3):
print("python")
print(x())
1.2
def x(name):
for i in range(3):
print(f"python {name}。")
print(x("I LOVE"))
1.3
def a(name, times):
for i in range(times):
print(f"i love {name}.")
print(a("Python",6))
2.1
def div(x,y):
if y == 0:
return "除数不能为0!"
else:
z = x / y
return z
print(div(4,4))
3.1
def abc(a,*,b,c): //限制关键字参数
print(a,b,c)
print(abc(1,b=2,c=3))
3.2
def myfunc(s,v,o):
return "".join((o,v,s))
print(myfunc("我","是","你爸爸"))
4.1
def myfunc(*args):
print("有{}个参数。".format(len(args)))
print("第2个参数是:{}".format(args[1]))
print(myfunc("nicai",2,3,4,5))
输出结果:有5个参数.
第2个参数是:2
4.2
def myfunc(kwargs):
print(kwargs)
print(myfunc(a=1,b=2,c=3))
输出结果:{'a': 1, 'b': 2, 'c': 3}
4.3
kwargs = {'a':1,'b':2,'c':3,'d':4}
print(myfunc(kwargs))
输出结果:1 2 3 4
4.4
args = (1,2,3,4)
def myfunc(a,b,c,d):
print(a,b,c,d)
print(myfunc(*args))
输出结果:1 2 3 4