Arguments in Python Functions

Anuradha GuptaAnuradha Gupta
2 min read

In Python, functions are defined using the def keyword followed by the function name and a set of parentheses containing any arguments the function requires. Arguments are values that are passed to the function when it is called, and they can be used within the function to perform certain operations or calculations.

Several types of arguments in Python functions:

  1. Positional Arguments: These are arguments that are passed to a function in the order they are defined in the function signature. The function receives them in the same order and assigns them to corresponding parameter names.

     pythonCopy codedef greet(name, age):
         print(f"Hello, {name}! You are {age} years old.")
    
     greet("Alice", 30)  # "Alice" is assigned to name, and 30 is assigned to age
    
  2. Keyword Arguments: These are arguments that are passed to a function with their corresponding parameter names, allowing the arguments to be passed in any order.

     pythonCopy codegreet(age=30, name="Alice")  # Keyword arguments can be passed in any order
    
  3. Default Arguments: These are arguments that have default values specified in the function signature. If a value is not provided for a default argument when the function is called, the default value is used.

     pythonCopy codedef greet(name, age=18):  # age has a default value of 18
         print(f"Hello, {name}! You are {age} years old.")
    
     greet("Bob")  # Output: "Hello, Bob! You are 18 years old."
    
  4. Variable-Length Positional Arguments (Arbitrary Argument Lists): These are arguments that allow a function to accept an arbitrary number of positional arguments. They are denoted by an asterisk (*) before the parameter name.

     pythonCopy codedef sum_values(*args):
         total = sum(args)
         print(f"The sum of the values is: {total}")
    
     sum_values(1, 2, 3)  # Output: "The sum of the values is: 6"
    
  5. Variable-Length Keyword Arguments (Arbitrary Keyword Argument Lists): These are arguments that allow a function to accept an arbitrary number of keyword arguments. They are denoted by two asterisks (**) before the parameter name.

     pythonCopy codedef print_values(**kwargs):
         for key, value in kwargs.items():
             print(f"{key}: {value}")
    
     print_values(name="Alice", age=30)  # Output: "name: Alice" and "age: 30"
    
  6. Unpacking Arguments: This technique involves passing the elements of an iterable (e.g., list, tuple) as individual arguments to a function using the asterisk (*) operator.

     pythonCopy codedef greet(name, age):
         print(f"Hello, {name}! You are {age} years old.")
    
     person = ("Alice", 30)
     greet(*person)  # Unpacking the tuple and passing its elements as arguments
    

These are the various ways arguments can be used in Python functions to make them versatile and flexible in handling different types and numbers of input data. Learn Python for more!

0
Subscribe to my newsletter

Read articles from Anuradha Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anuradha Gupta
Anuradha Gupta