Arguments in Python Functions

Anuradha GuptaAnuradha Gupta
2 min read

In Python, functions are blocks of reusable code that perform a specific task. They can take inputs (arguments) and produce outputs (return values). Here's an overview of how arguments work in Python functions:

  1. Defining Functions with Arguments: When defining a function in Python, you can specify parameters (placeholders for arguments) inside the parentheses following the function name. These parameters will receive values when the function is called.

    Example:

     pythonCopy codedef greet(name):
         print("Hello, " + name + "!")
    
  2. Passing Arguments to Functions: When calling a function, you can pass values (arguments) to it by providing them inside the parentheses. These values will be assigned to the corresponding parameters defined in the function's signature.

    Example:

     pythonCopy codegreet("Alice")
    
  3. Positional Arguments: Arguments passed to a function in the order they are defined in the function's parameter list are called positional arguments. The values are assigned to parameters based on their positions.

    Example:

     pythonCopy codedef add_numbers(x, y):
         return x + y
    
     result = add_numbers(3, 5)  # x=3, y=5
    
  4. Keyword Arguments: You can also pass arguments to a function using the parameter names (keywords) explicitly. This allows you to specify arguments in any order, regardless of their positions in the function's parameter list.

    Example:

     pythonCopy coderesult = add_numbers(y=5, x=3)  # Keyword arguments
    
  5. Default Arguments: You can specify default values for parameters in a function's signature. If the caller doesn't provide a value for a parameter with a default value, the default value is used.

    Example:

     pythonCopy codedef greet(name="Guest"):
         print("Hello, " + name + "!")
    
  6. Arbitrary Arguments: You can define functions that accept a variable number of arguments using the *args syntax. The arguments are packed into a tuple and can be accessed inside the function using this tuple.

    Example:

     pythonCopy codedef sum_numbers(*args):
         total = 0
         for num in args:
             total += num
         return total
    
     result = sum_numbers(1, 2, 3, 4, 5)
    
  7. Arbitrary Keyword Arguments: You can also define functions that accept a variable number of keyword arguments using the **kwargs syntax. The arguments are packed into a dictionary and can be accessed inside the function using this dictionary.

    Example:

     pythonCopy codedef print_info(**kwargs):
         for key, value in kwargs.items():
             print(key + ": " + value)
    
     print_info(name="Alice", age="30", city="New York")
    

These are some common ways to use arguments in Python functions, providing flexibility and versatility in function definitions and calls. Learn more about Arguments in Python Functions and check out the free online Python tutorial to excel in your tech career!

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