Best post on types of function arguments in python- 4

Types of function arguments in python

All parameters and arguments has specific way to be used in function.We will be studying function arguments and their types in this post.

Various types of function arguments in python are as follows:

  • Default Arguments
  • Required Arguments
  • Keyword Arguments
  • Variable-length Arguments

Default Arguments

A default argument is used to provide a default value if user do not provide input value to the function parameter by passing actual parameter in function call.
We can set a default value to the parameter in function definition itself.
We can choose a parameter which can have default or fixed value in absence of input from user.

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :7th September 2020

Description : Program to show use of default argument

"""
# DEFAULT ARGUMENTS
#here classname is set as default value
def display(name,classname="FYCS"):
    print("name:",name)
    print("classname:",classname)
    return

# here we are providing both values
print("Output for default argument")
print("----------------------------------")
display(name="rohit",classname="SYIT")

""" here we are providing only name ,
FYCS will come automatically because
we have set it as defalut value"""

display(name="ronit") 

Output

Output of function arguments : default argument
<strong>Output of function arguments : default argument</strong>

Required Arguments

A required argument is indicating that it is necessary to provide a value or actual argument in function call if parameters are mentioned in function definition.
If a user do not provide input value to the function parameter by passing actual parameter in function call in correct positional order than an exception arises.
In required arguments the number of arguments passed in a function call must be equal to the number of parameters defined in function definition

Example

"""
Author : ITVoyagers (itvoyagers.in)

Date :7th September 2020

Description : Program to show use of required argument

"""
# REQUIRED ARGUMENTS
value="Team ITVoyagers welcome you!!"
def show(value):
    print(value)
    return
show()

NOTE: As in above example one parameter is present in function definition but no actual argument is passed so we get and error.So we can check by passing one value in function call i.e. show(value).

Output

Output of function arguments : required argument
<strong>Output of function arguments : required argument</strong>

Keyword Arguments

The keyword arguments can be mentioned in function all as key = value.
As other arguments are positional this type of arguments allows user to provide value in non – positional manner.
**kwargs can be used in function definition to allow keyword arguments in function.
**kwargs works just like *args, but instead of accepting positional arguments it accepts keyword or named arguments.
In a function call we can directly mention keyword argument with above mentioned syntax.

Example 1

"""
Author : ITVoyagers (itvoyagers.in)

Date :7th September 2020

Description : Program to show use of keyword argument

"""
#KEYWORD ARGUMENTS
# Example 1
print("Output of example 1")
print("--------------------------------")
def show(value): 
    print (value)
    return
show(value="Team ITVoyagers welcome you!!nn")

# Example 2

print("Output of example 2")
print("--------------------------------")
def join_me(**kwargs):
    result = ""
    # Iterating over the Python kwargs dictionary
    for arg in kwargs.values():
        result += arg
    return result

print(join_me(p="cat  ",q="pat  ",r="sat  ",s="rat  ", t="mat  "))

Output

Output of function arguments : keyword argument
<strong>Output of function arguments : keyword argument</strong>

Variable – length Arguments

Variable – length arguments allow user to pass any number of arguments in function call.
In this type of arguments we can pass values apart from default and required arguments present in function definition.
To define variable – length argument ” * ” asterisk is used before parameter name, i.e *args .
This enables user to pass multiple actual arguments in single parameter.

Example 1

"""
Author : ITVoyagers (itvoyagers.in)

Date :7th September 2020

Description : Program to show use of variable - length argument

"""
# VARIBLE - LENGTH ARGUMENTS
print("Output of variable - length arguments")
print("-------------------------------------------")
def show(name,*argss):
    print("result is:")
    print(name)
    for var in argss:
        print(var)
    return
show("tony",90)
show("captain",85)
show("banner",85,80,75)

Output

Output of function arguments : variable length argument
<strong>Output of function arguments : variable length argument</strong>

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......

For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment