目录
2、默认参数:给参数设置一个默认值,当这个参数不存在的时候,它会直接赋值为当前的默认值。
4、不定长参数:args和kwargs就相当于一个容器,在这个容器中会有很多值,长度是不固定的。
1、普通参数:按照顺序依次赋值。
def say_hi(name):
print(f'hello,{name}')
print('欢迎来到大熊课堂')
say_hi('Aaron')
def create_window(width,height):
print(f'窗口的宽是{width};高是{height}')
create_window(2.1,1)
运行结果:
2、默认参数:给参数设置一个默认值,当这个参数不存在的时候,它会直接赋值为当前的默认值。
def total(hour,salary=8):
print(f'今天的薪水是{hour*salary}元')
total(8)
total(8,10)
运行结果:
3、关键字参数:可以改变参数的顺序,提高代码的可对性。
def student(firstname,lastname):
print(f'firstname is {firstname};lastname is {lastname}')
student(firstname='Aaron',lastname='Ye')
student(lastname='Ye',firstname='Aaron')
运行结果:
4、不定长参数:args和kwargs就相当于一个容器,在这个容器中会有很多值,长度是不固定的。
①一个“*”加一个变量名(通常默认为args),*args表示元组。
②两个“*”加一个变量名(通常默认为kwargs),**kwargs表示字典。
def my_function(width,height,*args,**kwargs):
print(width)
print(height)
print(args)
print(kwargs)
my_function(2,3,'hello','welcome','to','daxiong','thankyou',firstname='Aaron',lastname='Ye')
运行结果: