Python Arbitrary Arguments

Python Arguments will function to pass the data elements or the information to the functions. Hence, arguments will be enclosed in the parentheses after the function name. It is possible to enter as many parameters. This article has listed Python’s Arbitrary Arguments.

What are Python Function Arguments?

In the Python Function arguments, the user will pass their data values for running the functions. Functions consist of a set of arguments up until now. There is another way to define a function in Python and it has a variable number of arguments. Different types of function arguments are listed below:

What are the Different Types of Python Default Arguments?

  1. Python Default
  2. Python Keyword Argument
  3. Python Arbitrary Argument

In Python, function arguments have default values that are assigned to them. This will be operated using the assignment operator (=) in the functional argument.

In programming, when you create a function, you can set default values for some of its inputs. This means that if someone uses the function but forgets to provide a value for a specific input, the function will use a predefined default value instead.

Note: The Python Default Arguments are known as the Python Optional arguments.

Example

def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

# Calling the function without providing a value for the 'greeting' parameter
greet("John")  # Output: Hello, John!

# Calling the function with a custom greeting
greet("Alice", "Hi")  # Output: Hi, Alice!

Output

Hello, John!
Hi, Alice!
  1. In the first function call (greet("John")), since no specific greeting is provided, the default value “Hello” is used. So, it prints “Hello, John!”.
  2. In the second function call (greet("Alice", "Hi")), a custom greeting “Hi” is provided. Therefore, it prints “Hi, Alice!”.

What are Python Keyword Arguments?

The arguments in Python are arguments that will be passed in the fixed positional order to function. Moreover, the Python Keyword Arguments are considered a contradiction to the Required Arguments and will offer the exact opposite function.

The example for the Python Keyword Argument is given below:

def person_info(name, age, city):
    print(f"Name: {name}, Age: {age}, City: {city}")

# Calling the function using keyword arguments
person_info(name="John", age=25, city="New York")

# Calling the function with a different order of keyword arguments
person_info(city="San Francisco", age=30, name="Alice")

Output

Name: John, Age: 25, City: New York
Name: Alice, Age: 30, City: San Francisco

In the example given above, the ‘person_info has three parameters such as ‘name’, ‘age’, and ‘city’. while calling the function, we will give the parameter names with their values using the keyword arguments. However, the order of arguments will be different from the order of parameters in the function definition.

What are Python Arbitrary Arguments?

In Python Arbitrary arguments, the programmer won’t know the number of arguments that are required to be passed into the function. Meanwhile, they will also use an asterisk (*) to indicate the method before the parameter in the function. This is known as the Python *args.

Python * args will provide a function that will accept any number of positional arguments and these arguments are non-keyword arguments and variable-length argument lists.

An example of Python Arbitrary arguments is given below:

def display_items(*items):
    print("Items:")
    for item in items:
        print(f"- {item}")

# Calling the function with different numbers of arguments
display_items("Apple", "Banana", "Orange")
display_items("Laptop", "Mouse")
display_items("Book", "Pen", "Notebook", "Pencil")

Output

Items:
- Apple
- Banana
- Orange

Items:
- Laptop
- Mouse

Items:
- Book
- Pen
- Notebook
- Pencil

Conclusion

To conclude, this article has described the arbitrary arguments and the different types of Python default arguments. This will help beginners to improve their skills and knowledge regarding Python arbitrary arguments.

Python Arbitrary Arguments- FAQs

Q1. What is type with 3 arguments in Python?

Ans. When the 3 arguments are passed to the type (name, bases, dict) function. This will form a class dynamically and then will return with a new type object.

Q2. What is arbitrary keywords?

Ans. The arbitrary keyword arguments are referred to as the **kwargs that will work the same as *args buy other than accepting those positional arguments. Hence, it will accept keyword arguments from the dictionary.

Q3. What is the full form of args?

Ans. args is considered short for arguments.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment