0% found this document useful (0 votes)
2 views2 pages

Arguments in Python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Arguments in Python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arguments in python

In Python, arguments are the values you pass to a function when calling it.
1. Positional Arguments
• Passed in order (position matters)
def add(a, b):
print(a + b)

add(2, 3) # a=2, b=3


✔ Order must be correct

2. Keyword Arguments
• Passed using parameter names
• Order does not matter
def display(name, age):
print(name, age)

display(age=16, name="Riya")
✔ More readable
✔ Order can change

3. Default Arguments
• Parameters have default values
• Used if no value is provided
def greet(name="Guest"):
print("Hello", name)

greet() # Hello Guest


greet("Aman") # Hello Aman
✔ Default arguments must come after non-default arguments

4. Variable-Length Arguments
(a) *args (Non-keyword variable arguments)
• Accepts multiple positional arguments
def total(*numbers):
print(sum(numbers))

total(1, 2, 3, 4)
✔ Stored as a tuple

(b) **kwargs (Keyword variable arguments)


• Accepts multiple keyword arguments
def info(**data):
print(data)

info(name="Riya", age=16)
✔ Stored as a dictionary
Note:
Type Key Feature
Positional Order matters
Keyword Name-based, order doesn't matter
Default Has default value
*args Multiple positional values
**kwargs Multiple keyword values

A keyword parameter is simply a parameter in the function definition that can be given a value using its
name when calling the function.
How to identify (or “find”) keyword parameters
1. Look at the function definition
All parameter names can act as keyword parameters.
def display(name, age):
pass
Here, name and age are keyword parameters
You can call them like:
display(name="Riya", age=16)
2. Check the function call
If arguments are passed using parameter=value, they are keyword arguments.
display(age=16, name="Riya")
age and name are being used as keyword parameters
3. Look for default values
Parameters with default values are commonly used as keyword parameters.
def greet(name="Guest"):
print(name)
name is a keyword parameter (you can call: greet(name="Aman"))
4. Special keyword-only parameters
If you see * in the function definition, parameters after it are only keyword parameters
def func(a, *, b, c):
pass
b and c must be given as keywords:
func(1, b=2, c=3)

Quick Trick (Exam Tip)


If you see = in function call → keyword argument
If you see parameter names → keyword parameters

Simple Example
def add(a, b, c):
return a + b + c

add(1, 2, c=3)
• a, b, c → keyword parameters
• c=3 → keyword argument

You might also like