In this tutorial we are going to learn :

  • Number Of Arguments
  • Keyword Arguments
  • Default Arguments
  • Arbitrary Arguments – xargs
  • Arbitrary Keyword Arguments – xxargs

 

 

 

Number Of Arguments

 

In our other programs, we used just one parameter. Now we are going to work on arguments in detail.

Example:

This program is an example of passing two-parameter and works with them.

def addition(first_number, second_number):
    return first_number + second_number
    

print(addition(2,3)) 

 

 

 

Keyword Arguments

 

We can also use keyword arguments to pass parameters with key = value syntax.

Example :

The syntax of the keyword argument is key = value .

def addition(first_number, second_number):
    return first_number + second_number
    

print(addition(2,second_number=3))  # keyword argument 

 

 

 

Default Argument

 

We can also call the function without arguments. This is called default argument. Let’s look
at the example below.

Example :

def addition(first_number, second_number = 3):     # default argument
    return first_number + second_number
    

print(addition(2))
print(addition(3,5)) 

 

 

 

 

Arbitrary Arguments – xargs

 

If we don’t know how many arguments we pass to our function, we can use arbitrary arguments. We use * before the parameter name in the function definition.

Example :

In our addition function, as you can see we used arbitrary argument using * .

def addition(*numbers):
    last = 0
    for x in numbers:
        last += x
        
    return last
    
    
first_addition = addition(1,2,3,4,5)
second_addition addition(1,4,6)

print(f"First addition : {first_addition}")
print(f"Second addition : {second_addition}") 

 

 



Arbitrary Keyword Arguments – xxargs


If we don’t know how many keyword arguments we pass to our function, we can use arbitrary keyword arguments. We use ** before the parameter name in the function
definition.

Example :

def person(**intro):    # arbitrary keyword arguments
    print(intro)
    
    
person(name="John", age=23, status="Student") 

 

  • To print our keyword arguments :
def person(**intro):    # arbitrary keyword arguments
    print(intro["name"])
    
    
person(name="John", age=23, status="Student") 
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments