1. Position parameters: General Parameters
2. Default parameters:
1 def Power (x, n=2):2 s = 13while n > 0:4 n = n-15 s = S * x6 return s
There is a default assignment in the parameter, and the Power (5) is the square when called, if Power (5, 3) is cubic.
If you have more than one default parameter, the call is preceded by a default parameter, followed by a default argument, you need to write out "parameter name = parameter value" in the calling statement, such as the following case
1 defEnroll (name, Gender, age=6, city='Beijing'):2 Print('Name:', name)3 Print('Gender:', gender)4 Print('Age :', age)5 Print('City :', city)
Enroll (' Bob ', ' M ', 7) so no parameter name is allowed, city is using the default parameter ' Beijing ', age is not used, enroll (' Sary ', ' F ', ' Hangzhou ') This is wrong, because ' Hangzhou ' Not age, it's city, so it needs to be used this way: Enroll (' Sary ', ' F ', city= ' Hangzhou '), so age=6, city= ' Hangzhou '
3. Variable parameters
1 def Calc (numbers) 2 sum = 03for in numbers:4 sum = SUM + n * n 5 return sum
If you use the above code, for a list or a tuple input value, such as [------], the call needs to write this: Calc ([+]) or calc ((1, 2, 3), very troublesome
Therefore, the variable parameter needs to be added in front of numbers *
1 def Calc (*numbers)2 sum = 03 for in Numbers:4 sum = SUM + n * n5 return sum
This can be called in this way: Calc (1, 2, 3).
For nums = [1, 2, 3] This list, if using the above variable parameter, it can not be written as Calc (nums), Need to write Calc (nums[0], nums[1], nums[2]) or calc (*nums)
4. Keyword parameters
1 def person (name, age, * *kw)2 print("' ", kw)
**KW is a dict that invokes the statement for person (' Jackson ', ' gender= ', ' m ', city= ' Beijing '), resulting in Name:jackson age:30 other:{' gender ': ' m ', ' City ': ' Beijing '}
For extra = {' Gender ': ' M ', ' City ': ' Beijing '} This already defined dict, can be called by: Person (' Jackson ', ' A ', ' **extra ')
5. Named keyword parameters
It's not a very useful feeling.
Python Learning notes: function parameters