当前位置:网站首页>Default parameters of function & multiple methods of function parameters

Default parameters of function & multiple methods of function parameters

2022-07-05 13:01:00 South Lake Fishing Song

#  Keyword parameters of function :

def introduce(name, gender):
    """  Kindergarten self introduction  """
    print(f" Hello everyone , My name is :{
      name}, It's a little {
      gender}")

#  You need to provide parameters in order , The order must not be disordered 
introduce(" Xiao Ming ", " schoolboy ")
introduce(" The small white ", " girl student ")

#  You can use formal parameter names  =  value   To provide arguments 
introduce(name = " Xiao Ming ", gender=" schoolboy ")
introduce( gender=" schoolboy ",name = " Xiao Ming ")

#  It's fine too   Positional arguments   And keyword parameters   coordination 
introduce( " schoolboy ", gender= " Xiao Ming ")

#  The default parameter of the function :

def introduce(name, gender,age = 6):
    """  Kindergarten self introduction  """
    print(f" Hello everyone , My name is :{
      name}, It's a little {
      gender}, This year, {
      age} Year old ")

#--------------------  Function parameters in various forms  ------------------------

#  When you call a function , The default parameter can be left blank 

#  How not to set age, So that is 6 year 
introduce(" Xiao Ming ", " schoolboy ")

#  If you pass a new value , Override default 
introduce(" Xiao Ming ", " schoolboy ",age=5)
introduce(" Xiao Ming ", " schoolboy ",5)

#  Position arguments need to be in order 、 The keyword parameter uses  key=value You can change the order 、 Default parameters can be left blank 
introduce(age=8,name=" Xiao Ming ", gender=" schoolboy ")


原网站

版权声明
本文为[South Lake Fishing Song]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051240052818.html