This repository will have the python function concepts and programs to implement those concepts as example.
This repository will have the python function concepts and programs to implement those concepts as example.
Parameters are the local variables in a function which are written inside the paranthesis while defining any function.
Arguments are the values which are passed to the functions when it is called.
In python, arguments are passed by reference. Technically the memory address of arguments are passed.
Positional Arguments:
This is the most common way of assigning arguments to parameters: via order in which they are passed i.e. their position.
Example:
Let us consider a function my_func as below:
def my_func(a,b):
#code
Now let’s call my_func in two ways as mentioned below:
Hence these are the positional arguments, where with the change in the position of the arguments the value of the parameters changes.
Default Values:
A positional argument can be made optional by specifying a default value for corresponding parameter.
def my_func(a, b=100):
#code
The above function my_func(a, b=100) can be called by passing only one argument also, as shown below:
Making one positional argument optional out of three:
Let us consider below function:
def my_func(a, b=10, c):
#code
How to call function my_func(a, b=10, c) without specifying value for the second parameter?
Hence defining function in this way in the present example is not right neither the calling is right.
Rules for defining a default value for positional arguments:
If a Positional Parameter is defined with a defult value, then every positional parameter after it must be given a default value.
Therefore the valid function definition for the above case or scenario will be:
def my_func(a, b=10, c=20):
#code
Now function my_func(a, b=10, c=20) can be called in follwing ways:
Keyword argument:
Now another case if i want to specify the 1st and 3rd arguments but ommit the second argument. i.e. i want to specify values for parameters a and c but let b take on it default value.
This case can be achieved using keyword argument. Using keyword argument positional argument can, optionally, be specified by using the parameter name, whether or not the parameter have default value.
Let us consider a function having defaut values to its parameters:
def my_func(a, b=10, c=30):
#code
The above function can be called in following ways using keyword arguments
Let us consider a function having all positional parameters:
def my_func(a, b, c):
#code
The above function can be called in following ways using keyword arguments
NOTE: Once you use a named argument, all the arguments thereafter should be named too.
Example:
In both the above case we tried to put possitional argument after keyword arguemnt, which is inavlid syntax. Hence exception will be thrown.
Prior to this lets understand:
>>Basics Of Tuple:
Example:
1,2,3 and (1,2,3) both are a valid tuple. () is optional in (1,2,3)
Example:
1, or (1, ) is a valid syntax to create tuple with single element. However (1) will not create a tuple with single element rather it will create an integer data.
>> What is Packed Values?
Values that are bundled in some way are said to be Packed Values.
Example:
List [1,2,3], Tuples (1,2,3), String (“python3”), Sets {1,2,3} and Dictionaries {‘a’:1, ‘b’:2, ‘c’:3}. Hence we can say that any iterables are packed values.