In this Page, We are Providing Python Programming – Compound statement Parameter. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.
Python Programming – Compound statement Parameter
Parameter
The parameter is a named entity in a function (or method) definition that specifies the argument (or in some cases, arguments), that the function can accept. Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. For example, given the function definition:
def func ( foo , bar=None , ** kwargs ) : pass
foo, bar, and kwargs are parameters of f unc. However, when calling f unc, for example:
f unc ( 42 , bar=314 , extra=somevar )
the values 42 , 314 , and somevar are arguments.
The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object). Actually, call by object reference would be a better description, since if a mutable object is passed, the caller will see any changes the callee makes to it (items inserted into a list).
>>> def changeme ( mylist ) : . . . mylist.append ( [ 1 , 2 , 3 , 4 ] ) . . . print "Values inside the function: " , mylist . . . return . . . >>> mylist= [ 10 , 20 , 30 ] >>> changeme ( mylist ) Values inside the function: [ 10 , 20 , 30 , [ 1 , 2 , 3 , 4 ] ] >>> print "Values outside the function: " , mylist Values outside the function: [ 10 , 20 , 30 , [ 1 , 2 , 3 , 4 ] ]
In the above example, the append operation maintains the passed object reference. In the following example, the object reference is overwritten inside the function.
>>> def changeme ( mylist ) : . . . mylist= [ 1 , 2 , 3 , 4 ] . . . print "Values inside the function: ",mylist . . . return . . . >>> mylist= [ 10 , 20 , 30 ] >>> changeme ( mylist ) Values inside the function: [ 1 , 2 , 3 , 4 ] >>> print "Values outside the function: " , mylist Values outside the function: [ 10 , 20 , 30 ]
There are four types of parameters:
Positional or keyword parameter
It specifies that an argument can be passed either positionally or as a keyword argument. Note that, only those parameters which are at the end of the parameter list can be given default argument values i.e. the function cannot have a parameter with a default argument value preceding a parameter without a default argument value in the function’s parameter list. This is because the values are assigned to the parameters by position. For example, def func ( a , b=5 ) is valid, but def func ( a=5 , b ) is not valid.
Only positional parameter
It specifies that an argument can be supplied only by position.
Var-positional parameter
It specifies that an arbitrary sequence of positional arguments can be provided (in addition to any positional arguments already accepted by other parameters). Such a parameter can be defined by prepending the parameter name with *.
Var-keyword parameter
It specifies that arbitrarily many keyword arguments can be provided (in addition to any keyword arguments already accepted by other parameters). Such a parameter can be defined by prefixing the parameter name with **.
Following are few examples of functions.
>>> def person_info ( fn , In , ag , tel=5128975 , nat=' American ' ) : . . . print ' First name : ' , fn . . . print ' Last name : ' , In . . . print ' Age : ' , ag . . . print ' Telephone number : ' , tel . . . print ' Nationality : ' , nat . . . >>> person_info ( ’ Sachin ' , ' Tendulkar ' , 40 , 17896823 , ' Indian ' ) First name: Sachin Last name: Tendulkar Age: 40 Telephone number: 17896823 Nationality: Indian >>> person_info ( ' Mike ' , ' Johnson ' , 20 ) First name: Mike Last name: Johnson Age: 20 Telephone number: 5128975 Nationality: American >>> person_info ( ' Nadeem ' , ' Khan * , 54 , nat= ' Pakistani ' ) First name: Nadeem Last name: Khan Age: 54 Telephone number: 5128975 Nationality: Pakistani >>> person_info ( ' Chin ' , ' Chan ' , 15 , nat= ' Chinese ’ , tel=1894313654 ) First name: Chin Last name: Chan Age: 15 Telephone number: 1894313654 Nationality: Chinese >>> >>> def person_info ( fn , In , ag , tel , nat ) : . . . print ' First name: ' , fn . . . print ' Last name: ' , In . . . print ' Age: ' , ag . . . print ' Telephone number: ' , tel . . . print ' Nationality: ' , nat . . . >>> person_info ( ' Sachin ' , ' Tendulkar ' , 40,17896823, ' Indian') First name: Sachin Last name: Tendulkar Age: 40 Telephone number: 17896823 Nationality: Indian >>> person_info ( ' Chin ' , ' Chan ' , 15 , 1894313654 , ' Chinese ' ) First name: Chin Last name: Chan Age: 15 Telephone number: 1894313654 Nationality: Chinese >>> >>> def total ( a , b , *numbers ) : . . . tot=a+b . . . for num in numbers: . . . tot=tot+num . . . return tot . . . >>> print ' Total : ' , total ( 1 , 2 , 3 , 4 , 5 ) Total : 15 >>> print ' Total : ' , total ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) Total : 55 >>> >>> def person_info ( fn= ' Chinln = ' Chan ' , ** more_info ) : . . . print ' First name : ' , fn . . . print ' Last name : ' , In . . . if more_info.has_key ( ' ag ' ) : . . . print ' Age : ' , more_info [ ' ag ' ] . . . if more_info.has_key ( ' tel ' ) : . . . print ' Telephone number : ' , more_info [ ' tel ' ] . . . if more_info.has_key ( ' nat ' ) : . . . print ' Nationality : ' , more_info [ ' nat ' ] . . . >>> person_info ( ) First name: Chin Last name: Chan >>> person_info ( ag=40 , tel=1789 , ln= ' Tendulkar ' , nat= ' Indian ' , fn= ' sachin ' ) First name: Sachin Last name: Tendulkar Age: 40 Telephone number: 1789 Nationality: Indian >>> personl_info ( ag=15 , nat= ' Chinese ' , tel=l894313654 ) First name: Chin Last name: Chan Age: 15 Telephone number: 1894313654 Nationality: Chinese >>> >>> def total( a , b , *numbers , **kwag ) : . . . tot=a+b . . . for num in numbers: . . . tot=tot+num . . . for key in kwag: . . . tot=tot+kwag [ key ] . . . return tot . . . >>> print ' Total : ' , total ( 5 , 7 , 10 , 2 , 14 , c=100 , d=106 , e=211 ) Total: 455