[] #1. Do not pass parameters
Def fun1 ():
Print (' cannot pass parameter ')
>> fun1 (' Q ')
Traceback (most recent):
File "<pyshell#7>", line 1, in <module>
FUN1 (' Q ')
TYPEERROR:FUN1 () takes 0 positional arguments but 1 was given
>> fun1 ()
Cannot pass parameters
#2. Required Parameters
Def fun2 (a):
Print (' must pass parameter: ', a)
>> fun2 (2)
Parameters must be passed: 2
#3. Default parameter parameters can be passed or not
def fun3 (b=2):
Print (' Default parameter: ', b)
>> Fun3 ()
Default parameter: 2
>> Fun3 (4)
Default parameter: 4
>> Fun3 (b=10)
Default parameter: 10
# #4. Optional parameters can be passed 0-multiple, packaging Cheng Yuanju
Def fun4 (Arg):
Print (' can wear 0 to multiple ', ARG)
>> Fun4 () #返回一个空元祖
Can wear 0 to multiple ()
>> fun4 (1) #返回一个元祖
Can wear 0 to multiple (1,)
>> Fun4 (2,3)
Can wear 0 to multiple (2, 3)
>> Fun4 (4,5,6,7,8)
Can wear 0 to multiple (4, 5, 6, 7, 8)
>> Fun4 ([up])
Can wear 0 to multiple ([1, 2],)
>> fun4 (' SDF ')
Can wear 0 to multiple (' SDF ',)
>> fun4 ({' Q ': 123})
Can wear 0 to multiple ({' Q ': 123},)
>> Fun4 (())
Can wear 0 to multiple ((1, 2),)
#可选参数, when you send a parameter, add, remove the inner shell (unpack)
>> Fun4 (())
Can wear 0 to multiple (1, 2)
>> Fun4 ({' Q ': 123})
Can wear 0 to multiple (' Q ',)
>> Fun4 ([+])
Can wear 0 to multiple (1, 2)
>> Fun4 (' SDF ')
Can wear 0 to multiple (' s ', ' d ', ' F ')
# #5. Keyword parameters
Def fun5 (A, b): #定义的时候跟必备参数一样
Print (A, b) #必须放到最后
>> Fun5 (a=1,b=2)
1 2
def fun6 (**kwarg): print (' keyword parameter: ', Kwarg) #包装成字典 (can pass 0-more) >>> fun6 () keyword parameter: {} >&G T;> fun6 (a=1,b=2) #遵循变量名规则 keyword parameter: {' A ': 1, ' B ': 2} >>> fun6 (**{' a ': 123, ' B ': ' Wer '}) keyword parameter: { ' A ': 123, ' B ': ' Wer '} >>> fun6 (**{1:123,2: ' Wer '}) #key必须是字符串 Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> fun6 (**{1:123,2: ' Wer '}) typeerror:fun6 () keyword s must be strings # #参数混合时 keyword parameters must be followed, according to the defined book order, to ensure that the required parameters can get the value and only one #1. Required parameters + default parameters: The default parameter must be the Def fun7 (b,a=1) after the required parameters: Print (b) >>> fun7 (1,) 1 1 >>> fun7 (' Q ', a= ' we ') we Q >>> fun7 (a= ' we ' , b= ' ert ') we ert >>> fun7 (' Q ', ' W ') w q #2 def fun8 (b,m=1,*a): print (b) pri NT (m) print (a) ================== >>> fun8 (1,2,3,4,5,6,7,8) 1 2 (3, 4, 5, 6, 7 , 8) >>> #3 def fun9 (*a,b,m): #b, M key self-parameter print (a,b,m) print (b) print (m) >>> Fun9 ( , 3,4,5,b= ' Q ', m= ' W ') (1, 2, 3, 4, 5) Q W q W