×

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two.

Parameters:

While defining a function, we put some referral names that are taken as input for the function. These referral names are known as parameters. This can be explained by the following example:

def add(integer1, integer2):
    print(integer1 +integer2)

Here, integer1 and integer2 are the parameters given to the function add()

Arguments:

Arguments are the inputs that are passed into a function when it is called. This can be easily explained by the following example.

def add(integer1, integer2):
    print(integer1 +integer2)
add(3, 4)

Here, integer1 and integer2 are the parameters, and 3 and 4 are the arguments.

SYNTAX:

def function(parameter1, parameter2):
    pass




function(argument1, argument2)

Example:

def add(parameter1, parameter2):
    print(parameter1 + parameter2)




add("concat", "enation")

OUTPUT :

concatenation

Process finished with exit code 0

There are five types of parameters and two types of arguments.

Types of arguments:

The two types of arguments are:

  1. Keyword arguments
  2. Positional arguments

Keyword arguments:

The arguments that are inputted by specifying their respective parameter names are known as keyword arguments.

Syntax :

def function(parameter1, parameter2):
    pass




function(parameter1=argument1, parameter2=argument2)

( or )

def function(parameter1, parameter2):
    pass




function(parameter2=argument1, parameter1=argument2)

This can be easily understood by the following example :

def employee(name, address):
    print(f" {name} {address} ")




employee(name=" Rishika ", address=" Warangal ")

OUTPUT

  Rishika   Warangal 

Process finished with exit code 0

(or)

 def employee(name, address):
    print(f" {name} {address} ")




employee(address=" Warangal ", name=" Rishika ")

OUTPUT

  Rishika   Warangal 

Process finished with exit code 0

Example:

def division(dividend, divisor):
    print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")




division(dividend=10, divisor=2)

OUTPUT:

The quotient of 10 divided by 2 is equal to = 5.0

Process finished with exit code 0

(or)

def division(dividend, divisor):
    print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")




division(dividend=10, divisor=2)

OUTPUT:

The quotient of 10 divided by 2 is equal to = 5.0

Process finished with exit code 0

Positional arguments:

These arguments are inputted according to their positions. No assignment operators are used to input these arguments.

SYNTAX:

def function(parameter1, parameter2):
    pass




function(argument1, argument2)

This can be explained by the following example.

def employee(name, address):
    print(f" {name} {address} ")




employee(" Rishika ", " Warangal ")

OUTPUT

Rishika   Warangal 

Process finished with exit code 0

(and)

def employee(name, address):
    print(f" {name} {address} ")




employee(" Warangal ", " Rishika ")

OUTPUT

  Warangal   Rishika 

Process finished with exit code 0

Example:

def division(dividend, divisor):
    print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")




division(10, 2)

OUTPUT:

The quotient of 10 divided by 2 is equal to = 5.0

Process finished with exit code 0

(or)

def division(dividend, divisor):
    print(f" The quotient of {dividend} divided by {divisor} is equal to = {dividend / divisor} ")




division(2, 10)

OUTPUT:

The quotient of 10 divided by 2 is equal to = 2.0

Process finished with exit code 0

Types of parameters:

The five types of parameters are:

  • Positional or Keyword Parameters
  • Positional only Parameters
  • Keyword only parameters
  • Var positional parameters
  • Var keyword parameters

Positional or Keyword parameters:

These indicate the functions which have both positional as well as keyword parameters. The keyword parameters are declared after all the positional arguments.

Example:

def function( a , b , c = 29 ):
    print(f" a = { a } , b = { b } , c = { c } " )
function( 10 , 20 )
function( 10 , 20 , c = 30 )

OUTPUT:

a = 10 , b = 20 , c = 29

 a = 10 , b = 20 , c = 30

Process finished with exit code 0

Positional only parameters:

These indicate the functions that contain only positional parameters. In such functions, any arguments need not be forced to be passed in by keywords.

Example:

def function( a , b , c ):
    print(f" a = { a } , b = { b } , c = { c } " )
function( 10 , 20 , 30 )
function( 5 , 6 , 3 )

OUTPUT:

a = 10 , b = 20 , c = 30

 a = 5 , b = 6 , c = 3

Process finished with exit code 0

Keyword-only parameters:

These indicate the functions that contain only keyword parameters. To such functions, arguments can only be passed in them to the keywords of the parameters. You can create keyword-only parameters using the ' * ' symbol. All the parameters that come after the ' * ' symbol in a function definition are treated as keyword-only parameters.

Example:

def function( a , b , * , c , d ):
    print(f" a = { a } , b = { b } , c = { c } , d = { d } " )
function( 10 , 20 , c = 30 , d = 40 )

OUTPUT:

a = 10 , b = 20 , c = 30 , d = 40

Process finished with exit code 0

Example:

def function( a , b , * , c , d ):
    print(f" a = { a } , b = { b } , c = { c } , d = { d } " )


function( 10, 20, c=30 )

Output:

Traceback (most recent call last):

  File "xxx\main.py", line 4, in <module>

    function( 10, 20, c=30 )

TypeError: function() missing 1 required keyword-only argument: 'd'

Process finished with exit code 1

Var positional parameters:

When you require to pass an arbitrary number of arguments into a function, then its parameters are defined as var positional parameters. Such types of parameters are defined as *args, i.e., all the parameters passed under such conditions can be of any number and are not mandatory like other ones.  Only arguments of positional parameters can be passed in for var positional parameters

SYNTAX:

def function( parameter1 , parameter2 , *args ):
    pass

Example:

def my_fun( a , b , *args ):
    print( f" a = { a } , b = { b } , c = { args } " )
my_fun( 5 , 6 , 7 , 8 , 9 , 0 )
my_fun( " A " , " B " , " C " , " D " , " E " , " F " )

OUTPUT:

a = 5 , b = 6 , c = (7, 8, 9, 0)

 a =  A  , b =  B  , c = (' C ', ' D ', ' E ', ' F ')

Process finished with exit code 0

Example:

The built-in function max() in Python contains var keyword parameters.

Var Keyword parameters:

Var keyword parameters are similar to var positional parameters, except that we can pass only keyword arguments into the functions declared with such parameters.

In simple words, we can say that, when you require to pass an arbitrary number of keyword arguments into a function, then its parameters are defined as var positional parameters. Such types of parameters are defined as **kwargs, i.e., all the parameters passed under such conditions can be of any number and are not mandatory like other ones but have to be keyword arguments.

SYNTAX:

def function( parameter1 , parameter2 , **kwargs ):
    pass

Example:

def my_fun( a , b , **kwargs ):
    print( f" a = { a } , b = { b } , kwargs = { kwargs } " )
my_fun( 5 , 6 , c = 7 , d = 8 , e = 9 , f = 0 )

OUTPUT:

a = 5, b = 6, kwargs = {'c': 7, 'd': 8, 'e': 9, 'f': 0}

Process finished with exit code 0

Example:

The built-in function dict() in Python contains var keyword parameters.


Related Topics

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Create the First GUI Application using PyQt5 in Python

GUI: A graphical user interface, or GUI, is present on most personal computers. It provides a simple experience for individuals with various computing skill levels. GUI apps may take more resources...

3 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Python Graph

Python Graph: In Computer Science and Mathematics, a Graph is a pictorial representation of a group of objects or elements where some elements are connected using the links. A graph...

5 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.