Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

*args and **kwargs in python

Posted on October 6, 2023 by admin

In Python, *args and **kwargs are special syntax that allow you to pass a variable number of positional and keyword arguments to functions. They are often used when you want to create flexible and generic functions that can accept a varying number of arguments.

Here’s an explanation of each:

  1. *args (Arbitrary Positional Arguments):
  • The *args syntax in a function parameter allows you to pass a variable number of positional arguments.
  • These arguments are collected into a tuple and can be accessed within the function using the name args or any other name you choose.
  • *args must appear after any explicitly named positional parameters in the function definition. Example:
   def sum_all(*args):
       total = 0
       for num in args:
           total += num
       return total

   result = sum_all(1, 2, 3, 4, 5)
   print(result)  # Output: 15
  1. **kwargs (Arbitrary Keyword Arguments):
  • The **kwargs syntax allows you to pass a variable number of keyword arguments.
  • These arguments are collected into a dictionary where the keys are the argument names, and the values are the argument values.
  • **kwargs must also appear after any explicitly named positional or keyword parameters. Example:
   def print_info(**kwargs):
       for key, value in kwargs.items():
           print(f"{key}: {value}")

   print_info(name="John", age=30, city="New York")
   # Output:
   # name: John
   # age: 30
   # city: New York

When using *args and **kwargs, keep in mind the following:

  • You can use both *args and **kwargs in the same function, but *args must come before **kwargs in the parameter list.
  • You can still have regular positional and keyword parameters before or after *args and **kwargs in the function definition.
  • The names args and kwargs are conventional, but you can use any valid variable names in their place.
  • Using these features makes your functions more flexible and allows you to create functions that can handle a wide variety of input arguments.

Here’s an example that combines all three types of parameters (positional, *args, and **kwargs) in a single function:

def example_function(a, b, *args, x=0, y=0, **kwargs):
    print(f"a: {a}, b: {b}, x: {x}, y: {y}")
    print("Additional positional arguments (*args):", args)
    print("Additional keyword arguments (**kwargs):", kwargs)

example_function(1, 2, 3, 4, x=5, y=6, name="Alice", age=25)

In this example, a and b are regular positional parameters, x and y are keyword parameters with default values, *args collects additional positional arguments, and **kwargs collects additional keyword arguments.

Category: programming, python, Tutorials

Post navigation

← Parsing a string date in python
transfer data from your own server to a local server →

Recent Posts

  • Unlocking Insights: How Geospatial Reasoning Revolutionizes Data Analysis with AI
  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js

Archives

  • August 2025
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • August 2025
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme