Function Arguments in Python

Function Arguments in Python

Function Arguments in Python

Function Arguments in Python

In Python, there are four types of function arguments:

  1. Positional arguments: These are the most common type of arguments and are passed to a function in the order they appear in the function definition.
				
					def greet(name, message):
    print(f"{message}, {name}!")

greet("Alice", "Hello")   # Output: Hello, Alice!

				
			

In this example, we define a function greet that takes two positional arguments, name and message. Inside the function, we print a greeting message that includes the name and message arguments. We then call the greet function with the values “Alice” and “Hello” as the first and second arguments, respectively.

  1. Default arguments: These are arguments that have a default value specified in the function definition. If the argument is not passed to the function, the default value is used.
				
					def greet(name, message="Hello"):
    print(f"{message}, {name}!")

greet("Alice")       # Output: Hello, Alice!
greet("Bob", "Hi")   # Output: Hi, Bob!

				
			

In this example, we define a function greet that takes two arguments, name and message, with a default value of “Hello” for the message argument. Inside the function, we print a greeting message that includes the name and message arguments. We then call the greet function with the value “Alice” as the name argument (using the default value for message) and with the values “Bob” and “Hi” as the name and message arguments, respectively.

  1. Keyword arguments: These are arguments that are passed to a function using the name of the argument.
				
					def greet(name, message):
    print(f"{message}, {name}!")

greet(message="Hello", name="Alice")   # Output: Hello, Alice!

				
			

In this example, we define a function greet that takes two arguments, name and message. Inside the function, we print a greeting message that includes the name and message arguments. We then call the greet function with the values “Alice” and “Hello” as the name and message arguments, respectively, but using the keyword syntax to specify the order of the arguments.

  1. Variable-length arguments: These are arguments that allow you to pass an arbitrary number of arguments to a function. There are two types of variable-length arguments in Python:
  2. *args: This is used to pass a variable number of positional arguments to a function.
				
					def add(*args):
    total = 0
    for arg in args:
        total += arg
    return total

result = add(1, 2, 3, 4)
print(result)   # Output: 10

				
			

In this example, we define a function add that takes a variable number of positional arguments using the *args syntax. Inside the function, we loop over each argument and add it to the total variable. We then call the add function with the values 1, 2, 3, and 4 as the positional arguments.

  1. **kwargs: This is used to pass a variable number of keyword arguments to a function.
				
					def print_values(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_values(a=1, b=2, c=3)   # Output: a: 1, b: 2, c: 3

				
			
Join To Get Our Newsletter
Spread the love