DEV Community

Poornima Ravi
Poornima Ravi

Posted on

Python Function Type Arguments

""" Function Type Arguments"""
"""1. Positional Arguments

    - Values get assigned based on order/position of arguments passed""" 

def add(no1, no2, a, b):
    print(a,b,(no1+no2))
add(4,6,'Good','Morning')
add('Good','Morning',4,6)
print()


"""2. Keyword Arguments

    - Values are assigned by specifying the Fn. Argument's Keyword during the function call.
    - Positional Argument is followed by Keyword argument}"""

def kwargs(Str1, no1, List):
    print(Str1, no1)
    print(Str1, no1, List)
    print(List)
kwargs('Menu','e',[3,4,2,5])
kwargs(4,List = ['Apple', 'Mango', 'Melon'], no1 = 3)
print()

"""3. Default Arguments

    - Assigning a default value to the function parameter makes it a default argument for the function
    - if a postional/keyword arg value is passed to a default arg, the default gets overwritted with the value
    - Note : if the fn. has a default argument , it should be defined after all the positional arguments"""

def default_arg (user, job, salary = 10000):
    print(user, job, salary)
default_arg('Ken','Tech Engineer')
default_arg('Kenny','Field Engineer',5000)
default_arg('Ken_Z',45000)
default_arg('Ken_B','Tech Engineer',salary =9000)
print()

"""4. Variable Length Arguments

    - Can accept any number of positional arguments(*args)/Keyword Arguments(**args) passed to the function.
    - Positional Variable length Argument forms a tuple of all passed inputs defined under one variable
    - Keyword variable length Argument forms a Dictionary of all passed inputs defined under one variable"""

"""Positional Variable length arguments"""
def variable_length(def_argument= 'Test',*args, def_argument1='Yes'):
    print(type(args))
    print(args)
    print(args, def_argument,def_argument1)
    print(def_argument,args,def_argument1)


variable_length('My','Name','is','Henry','Ford','1995', True, 4.5)
print()
"""Keyword Variable length arguments"""

def keyword_variable_args(*args,**kw_args):
    print(type(args),type(kw_args))
    print(args)
    print(kw_args)

keyword_variable_args(4,3,'Time',True,Name='Bharat',Age = 30, Salary= 5000, Employer='TCS')
print()

"""5. Mixed Arguments

- A Function Arguments can have mix of all the argument types discussed above.
- In that case, Positional Arguments comes first , followed by keyword args, Default Arg / Keyword Variable length Arguments"""


def mixed_args(Num, *args,Str, Time = 'Fan', **Kargs):
    print(Num, Str, Time, args,Kargs)

mixed_args(4,5,4,5,3,7,'Key',5.6,Str='test',Name = 'Peacock')


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
dshaw0004 profile image
Dipankar Shaw

I will suggest you to keep normal text separated from code. Instead of putting every thing inside the code block use other semantic tags. Like -

Function Type Arguments

1. Positional Arguments

Values get assigned based on order/position of arguments passed

def add(no1, no2, a, b):
    print(a,b,(no1+no2))
add(4,6,'Good','Morning')
add('Good','Morning',4,6)
print()
Enter fullscreen mode Exit fullscreen mode

It will make your post more readable.