in , ,

Python Function Arguments

Python Function Arguments

Python Function Arguments: In Python, you can define a function that takes a variable number of contentions. In this article, you will learn to define such functions using the default, keywords, and arbitrary arguments.



Something else, the function call will result in an error. Here is an example.

def greet(name, msg):
    """This function greets to
    the person with the provided message"""
    print("Hello", name + ', ' + msg)

greet("worldofitech", "Good morning!")


Hello worldofitech, Good morning!

Here, the function greet() has two parameters.

Since we have called this function with two arguments, it runs easily and we don’t get any error.

If we call it with an alternate number of contentions, the mediator will show a blunder message. The following is a call to this capacity with one and no contentions alongside their particular error messages.

>>> greet("worldofitech")    # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
>>> greet()    # no arguments
TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'

Variable Function Arguments

As of recently, capacities had a fixed number of contentions. In Python, there are different approaches to characterize a capacity that can take a variable number of arguments.

Three different forms of this type are described below.

Python Default Arguments

Function arguments can have default values in Python.

We can provide a default value to an argument by using the assignment operator (=). Here is an example.

def greet(name, msg="Good morning!"):
    This function greets to
    the person with the
    provided message.

    If the message is not provided,
    it defaults to "Good

    print("Hello", name + ', ' + msg)

greet("this site", "How do you do?")
Hello worldofitech, Good morning!
Hello this site, How do you do?

In this function, the parameter name doesn’t have default esteem and is required (obligatory) during a call.

Then again, the parameter msg has a default value of “Good Morning!”. Along these lines, it is discretionary during a call. On the off chance that worth is given, it will overwrite the default esteem.

Any number of arguments incapacity can have default esteem. Be that as it may, when we have a default contention, all the contentions to its privilege should likewise have default values.

This means to say, the non-default argument can’t follow default contentions. For instance, in the event that we had characterized the function header above as:

def greet(msg = "Good morning!", name):

We would get an error as:

SyntaxError: non-default argument follows default argument

Python Keyword Arguments

When we call a function with some values, these values get assigned to the arguments according to their position.

For example, in the above function greet(), when we called it as greet("worldofitech", "How do you do?"), the value "worldofitech" gets assigned to the argument name and similarly "How do you do?" to msg.

Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed. Following calls to the above function are all valid and produce the same result.

# 2 keyword arguments
greet(name = "worldofitech",msg = "How do you do?")

# 2 keyword arguments (out of order)
greet(msg = "How do you do?",name = "worldofitech") 

1 positional, 1 keyword argument
greet("worldofitech", msg = "How do you do?")           

As should be obvious, we can mix positional contentions in with watchword contentions during a capacity call. In any case, we should remember that catchphrase contentions must follow positional contentions.

Having a positional contention after catchphrase contentions will bring about mistakes. For instance, the function call as follows:

greet(name="worldofitech","How do you do?")

Will result in an error:

SyntaxError: non-keyword arg after keyword arg

Python Arbitrary Arguments

Some of the time, we don’t know in advance of the number of arguments that will be passed into a function. Python allows us to handle this sort of situation through function calls with a subjective number of arguments.

In the function definition, we use an asterisk (*) before the parameter name to mean this sort of argument. Here is an example.

def greet(*names):
    """This function greets all
    the person in the names tuple."""

    # names is a tuple with arguments
    for name in names:
        print("Hello", name)

greet("worldofitech", "this site", "online training", "python programming")


Hello worldofitech
Hello this site
Hello online training
Hello python programming

Here, we have called the function with multiple arguments. These arguments get wrapped up into a tuple before being passed into the function. Inside the function, we use a for loop to retrieve all the arguments back.

Please feel free to give your comment if you face any difficulty here.

For More Latest Articles Click on Below Link

salman khan

Written by worldofitech

Leave a Reply

Python Functions

Python Functions

Python Recursion

Python Recursion