Types of Arguments in Python
Last Updated :
23 Dec, 2024
Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma. There are many types of arguments in Python .
In this example, we will create a simple function in Python to check whether the number passed as an argument to the function positive, negative or zero.
Python
def checkSign(a):
if a > 0:
print("positive")
elif a < 0:
print("negative")
else:
print("zero")
# call the function
checkSign(10)
checkSign(-5)
checkSign(0)
Outputpositive
negative
zero
Types of Arguments in Python
Python provides various argument types to pass values to functions, making them more flexible and reusable. Understanding these types can simplify your code and improve readability. we have the following function argument types in Python:
- Default argument
- Keyword arguments (named arguments)
- Positional arguments
- Arbitrary arguments (variable-length arguments *args and **kwargs)
- Lambda Function Arguments
Let’s discuss each type in detail.
Default Arguments
Default Arguments is a parameter that have a predefined value if no value is passed during the function call. This following example illustrates Default arguments to write functions in Python.
Python
def calculate_area(length, width=5):
area = length * width
print(f"Area of rectangle: {area}")
# Driver code (We call calculate_area() with only
# the length argument)
calculate_area(10)
# We can also pass a custom width
calculate_area(10, 8)
OutputArea of rectangle: 50
Area of rectangle: 80
Keyword arguments
Keyword arguments are passed by naming the parameters when calling the function. This lets us provide the arguments in any order, making the code more readable and flexible.
Python
def fun(name, course):
print(name,course)
# Positional arguments
fun(course="DSA",name="gfg")
fun(name="gfg",course="DSA")
Positional arguments
Positional arguments in Python are values that we pass to a function in a specific order. The order in which we pass the arguments matters.
This following example illustrates Positional arguments to write functions in Python.
Python
def productInfo(product, price):
print("Product:", product)
print("Price: $", price)
# Correct order of arguments
print("Case-1:")
productInfo("Laptop", 1200)
# Incorrect order of arguments
print("\nCase-2:")
productInfo(1200, "Laptop")
OutputCase-1:
Product: Laptop
Price: $ 1200
Case-2:
Product: 1200
Price: $ Laptop
Arbitrary arguments (variable-length arguments *args and **kwargs)
In Python Arbitrary arguments allow us to pass a number of arguments to a function. This is useful when we don't know in advance how many arguments we will need to pass. There are two types of arbitrary arguments:
- *args in Python (Non-Keyword Arguments): Collects extra positional arguments passed to a function into a tuple.
- **kwargs in Python (Keyword Arguments): Collects extra keyword arguments passed to a function into a dictionary.
Example 1 : Handling Variable Arguments in Functions
Python
# Python program to illustrate
# *args for variable number of arguments
def myFun(*argv):
for arg in argv:
print(arg)
# Driver code with different arguments
myFun('Python', 'is', 'amazing')
Example 2: Handling Arbitrary Keyword in Functions
Python
# Python program to illustrate
# **kwargs for variable number of keyword arguments
def fun(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
# Driver code
fun(course="DSA", platform="GeeksforGeeks", difficulty="easy")
Outputcourse: DSA
platform: GeeksforGeeks
difficulty: easy
Lambda Function Arguments
Lambda functions work like regular functions, taking arguments to perform task in one simple expression. we can pass any number of arguments. Here are the common ways to pass arguments in lambda function:
- Single Argument
- Multiple Arguments
Example 1: Passing single argument
Python
# Lambda function with one argument
square = lambda x: x ** 2
print(square(5))
Example 2: Passing multiple arguments
Python
# Lambda function with two arguments
add = lambda a, b: a + b
print(add(3, 4))
Similar Reads
Type Hints in Python
Python is a dynamically typed language, which means you never have to explicitly indicate what kind of variable it is. But in some cases, dynamic typing can lead to some bugs that are very difficult to debug, and in those cases, Type Hints or Static Typing can be convenient. Type Hints have been int
3 min read
Tuple as function arguments in Python
Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read
Unpacking arguments in Python
If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python. Unpacking without storing the values: You might encounter a situation where you
3 min read
Python function arguments
In Python, function arguments are the inputs we provide to a function when we call it. Using arguments makes our functions flexible and reusable, allowing them to handle different inputs without altering the code itself. Python offers several ways to use arguments, each designed for specific scenari
3 min read
type() function in Python
The type() function is mostly used for debugging purposes. Two different types of arguments can be passed to type() function, single and three arguments. If a single argument type(obj) is passed, it returns the type of the given object. If three argument types (object, bases, dict) are passed, it re
5 min read
Default arguments in Python
Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value. Default Arguments: Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argu
7 min read
Types of inheritance Python
Inheritance is defined as the mechanism of inheriting the properties of the base class to the child class. Here we a going to see the types of inheritance in Python. Types of Inheritance in Python Types of Inheritance depend upon the number of child and parent classes involved. There are four types
3 min read
Variable Length Argument in Python
In this article, we will cover about Variable Length Arguments in Python. Variable-length arguments refer to a feature that allows a function to accept a variable number of arguments in Python. It is also known as the argument that can also accept an unlimited amount of data as input inside the func
4 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
10 min read
Command Line Arguments in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are: Table of Content Using sys.argvUsing getopt moduleUsing
5 min read