×

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

Python Prime factorization

Python Prime factorization In this tutorial, we will design a program where we will find all the prime factors of a number. Then, we will print all these prime factors of...

3 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.

Python program to find the area of a circle

Python program to find the area of a circle This article will discuss how to find the area of a circle in Python with a given radius. The area of a...

2 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Sentiment Analysis using NLTK

Introduction Data is being produced at an astounding rate and volume in the field of the internet and other digital services nowadays. Researchers, engineers, and data analysts often work with tabular...

7 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.