Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Type Hints in Python
Next article icon

Types of Arguments in Python

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 

Output
positive 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) 

Output
Area 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") 

Output
gfg DSA gfg 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") 

Output
Case-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') 

Output
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") 

Output
course: 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)) 

Output
25 

Example 2: Passing multiple arguments

Python
# Lambda function with two arguments add = lambda a, b: a + b print(add(3, 4)) 

Output
7 

Next Article
Type Hints in Python

V

vishakshx339
Improve
Article Tags :
  • Python
  • Python-Functions
Practice Tags :
  • python
  • python-functions

Similar Reads

  • Type Hints in Python
    Type hints are a feature in Python that allow developers to annotate their code with expected types for variables and function arguments. This helps to improve code readability and provides an opportunity to catch errors before runtime using type checkers like mypy. Using Type Hints in Python1. Vari
    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
  • 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
  • Packing and Unpacking Arguments in Python
    Python provides the concept of packing and unpacking arguments, which allows us to handle variable-length arguments efficiently. This feature is useful when we don’t know beforehand how many arguments will be passed to a function. Packing ArgumentsPacking allows multiple values to be combined into a
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences