Zman's study notes Python article:
1. Decorative Device
2. Function "Variable length parameter"
This time to talk about the "variable length parameters" of functions in Python, which is often used in practice.
First, what is "variable length parameter"
A variable length parameter is one that passes an indefinite number of arguments to a function. For example, I write a function: Pass in a student to participate in each subject examination results, seek average score, such as two students to pass (92, 91, 88) and (88, 95), because each student to participate in the number of exams different, so the number of incoming parameters is different, encountered this situation, when we define the function, You can use variable length parameters.
Second, use "variable length parameter" when defining function
#variable parameters for a full print functiondefFunc (*args, * *Kwargs):Print(args)Print(Kwargs) func (1,'Hello', a=2, c=' World')>>>(1,'Hello'){'a': 2,'C':' World'}
The above code defines one of the simplest functions to receive two "variable length parameters", one is "non-keyword parameter", the data structure is: tuple ( Note: Tuple cannot be modified ); the other is "keyword function", The data structure is: dictionary .
At this point, we can sequentially pass in a number of non-keyword parameters and several key parameters, can not mix and match incoming ~ We can use the following code to print out the received parameters:
#to print the variable parameters of a function sequentiallydefFunc (*args, * *Kwargs): forArginchargs:Print('ARG:', Arg) forValueinchKwargs:Print('%s:'%value, Kwargs[value]) func (1,'Hello', a=2, c=' World')>>>ARG:1Arg:helloa:2C:world
Third, use "variable length parameter" when calling function
We can also pass in a "variable length parameter" when calling a function, as follows:
deffunc (name1, Name2, Name3):Print('First Person:%s'%name1)Print('Second Person:%s'%name2)Print('Third Person:%s'%name3) Name_tuple1= ('Mary','John','Jane') Name_tuple2= ('Rose','Mike') func (*name_tuple1) func ('Luna', *name_tuple2)>>>First Person: Mary Second: John the third Person: Jane first: Luna second: Rose Third: Mike
In this code, I define the number of arguments for the Func function to be 3, so make sure that the number of arguments passed in when the function is called is also 3 yo ~
Iv. Practical Application
#: Suppose the name of a class, the honor of the class, and the name and age of the instructordefWelcome (name, *args, * *Kwargs):Print('%s has received%d honors, namely:'%(name, Len (args))) forIinchRange (len (args)):Print('%d.%s'% (i+1, Args[i])) Age_sum= 0#Age and Print('There are%d teachers in the class, their names and ages are as follows:'%Len (Kwargs)) forValueinchKwargs:age_sum+=Kwargs[value]Print('%s:%d years'%(value, Kwargs[value]))Print('The average age of teachers is%.1f years old \ n'% (age_sum/Len (Kwargs))) Welcome ('Computer Class 1','School Excellent class','Best Cohesion', mary=27, peter=35, john=32) Welcome ('Software Class 2','Excellent class in Shanghai city','School Chorus Competition champion','Best Cohesion', peter=34, rose=28, hans=33)>>>The Computer 1 class has received 2 honors, namely:1. School Excellent class2best cohesion The class has a total of 3 teachers whose names and ages are as follows: The average age of peter:35-john:32-year-old mary:27 teachers is 31.3 years old software 2 classes have received 3 honors, respectively:1. Shanghai Excellent class2. School Chorus Competition Champion3best cohesion The class has a total of 3 teachers whose names and ages are as follows: Peter:34 rose:28-year-old hans:33 the average age of teachers is 31.7 years old
This example is also self-made, can help you understand the topic content is good ~
Zman's study notes python: function variable length parameter