Skip to main content

*args and **kwargs in Python

 

*args and **kwargs in Python 

🔍 What are *args and **kwargs in Python?

In Python, *args and **kwargs allow you to pass a variable number of arguments to a function.

  • 👉 *args → accepts multiple positional arguments
  • 👉 **kwargs → accepts multiple keyword arguments

✨ Why Use *args and **kwargs?

  • ✅ Makes functions flexible
  • ✅ Handles unknown number of inputs
  • ✅ Useful in real-world applications
  • ✅ Reduces code repetition

🧠 Understanding *args (Non-keyword Arguments)

*args allows a function to accept any number of positional arguments.

📌 Example:

def add_numbers(*args):
total = 0
for num in args:
total += num
return total

print(add_numbers(1, 2, 3, 4))

👉 Output:

10

🔍 How *args Works

  • *args collects arguments into a tuple
  • You can loop through it like a list
def show_args(*args):
print(args)

show_args(10, 20, 30)

👉 Output:

(10, 20, 30)

🧠 Understanding **kwargs (Keyword Arguments)

**kwargs allows a function to accept any number of keyword arguments.

📌 Example:

def display_info(**kwargs):
for key, value in kwargs.items():
print(key, ":", value)

display_info(name="Prathima", age=22)

👉 Output:

name : Prathima
age : 22

🔍 How **kwargs Works

  • **kwargs collects arguments into a dictionary
  • Keys = parameter names, Values = parameter values

🔄 Using *args and **kwargs Together

def demo(*args, **kwargs):
print("Args:", args)
print("Kwargs:", kwargs)

demo(1, 2, 3, name="Python", level="Beginner")

👉 Output:

Args: (1, 2, 3)
Kwargs: {'name': 'Python', 'level': 'Beginner'}

⚠️ Order of Parameters

When using all types together, follow this order:

def function_name(normal, *args, **kwargs):
pass

🚀 Real-World Example

def student_details(*args, **kwargs):
print("Subjects:", args)
print("Details:", kwargs)

student_details("Math", "Science", name="Ravi", grade="A")

📦 Advantages

  • ✔ Flexible function definitions
  • ✔ Clean and readable code
  • ✔ Useful in APIs and frameworks
  • ✔ Supports dynamic arguments

📝 Conclusion

Understanding *args and **kwargs in Python is essential for writing flexible and scalable functions. These concepts are widely used in advanced Python programming, including frameworks and libraries.


🔑 SEO Keywords

*args and **kwargs in Python, Python variable arguments, Python functions tutorial, Python keyword arguments, Python programming basics, Python advanced functions

Comments

Popular posts from this blog

APSET 2026 – Artificial Intelligence (AI) MCQs (50 Questions)

  APSET 2026 – Artificial Intelligence (AI) MCQs (50 Questions) (Computer Science & Applications – As per APSET / UGC NET Syllabus) 2. The father of AI is: A) Alan Turing B) John McCarthy C) Marvin Minsky D) Herbert Simon 👉  Answer:  B Below are 50 important MCQs for APSET 2026 preparation. 🔹 PART A: Basics of Artificial Intelligence 1. Artificial Intelligence was formally introduced in: A) 1943 B) 1956 C) 1965 D) 1972 👉 Answer: B 2. The father of AI is: A) Alan Turing B) John McCarthy C) Marvin Minsky D) Herbert Simon 👉 Answer: B 3. The Turing Test was proposed by: A) John McCarthy B) Alan Turing C) Newell D) Simon 👉 Answer: B 4. AI primarily deals with: A) Numerical computation B) Intelligent agents C) Hardware design D) Databases 👉 Answer: B 5. Rational agent acts to: A) Maximize performance measure B) Minimize input C) Reduce memory D) Increase speed 👉 Answer: A 🔹 PART B: Search Techniques 6. Breadth First Search...

Central Bank of India Recruitment 2026 – 275 Specialist Officer (SO) Posts

Central Bank of India Recruitment 2026 – 275 Specialist Officer (SO) Posts The Central Bank of India has officially invited applications for 275 Specialist Officer (SO) positions across various technical and risk-related streams. Online applications began recently and interested candidates can apply directly through the official portal. 📅 Key Dates 🟢 Application Start: 28 February 2026 ⏰ Last Date to Apply: 23 March 2026 🖥️ Online Exam: April 2026 (Tentative) 🎤 Interview: April 2026 (Tentative) 📌 Vacancy & Roles There are 275 vacancies in the Specialist Officer category, covering roles such as: IT Officer Software Developer (Java / .NET) Mobile Application Developer Database Administrator Cyber Security & Information Security Specialist Data Scientist / Data Engineer Cloud Manager Risk Manager Taxation / CA Specialist DevSecOps and more technical/risk disciplines 📋 Eligibility (General Overview) Educational Qualification: Relevant full-time degree like B.E./B....

Reserve Bank of India RBI Assistant Recruitment 2026 - Apply Online for 650 Posts

Reserve Bank of India RBI Assistant Recruitment 2026 - Apply Online for 650 Posts 🏦 Reserve Bank of India (RBI) Assistant Recruitment 2026 – 650 Posts The RBI Assistant Recruitment 2026 notification has been released for 650 Assistant vacancies across RBI offices in India. Eligible graduates can apply online through the official RBI website.  📋 Recruitment Overview Organization: Reserve Bank of India (RBI) Post Name: Assistant Total Vacancies: 650 Application Mode: Online Notification Release Date: 16 February 2026 Last Date to Apply: 8 March 2026 Job Location: Across India Official Website: rbi.org.in  🎓 Educational Qualification Candidates must have: Bachelor’s Degree in any discipline from a recognized university Minimum 50% marks (pass marks for SC/ST/PwBD candidates) Knowledge of word processing on computer Proficiency in the local language of the state/region applied for  🎂 Age Limit (as on 1 Feb 2026) Minimum Age: 20 years Maximum Age: 28 years Age ...