3 Python *args and **kwargs
1. What are *args and **kwargs?
*args and **kwargs are used in functions
when we don’t know how many arguments we will pass.
In simple words:
|
0 1 2 3 4 5 6 7 |
👉 *args = many values 👉 **kwargs = many key–value pairs |
2. What is *args?
*args allows a function to accept any number of values.
Example
Output:
👉 args is a tuple.
3. Use *args to Add Numbers
Example
4. Loop Through *args
Example
5. What is **kwargs?
**kwargs allows a function to accept any number of key–value arguments.
Example
Output:
👉 kwargs is a dictionary.
6. Loop Through **kwargs
Example
7. Using *args and **kwargs Together
Example
8. Order of Arguments (Very Important)
Correct order:
Example
9. Common Beginner Mistakes
❌ Using Wrong Order
❌ Error.
❌ Forgetting * or **
👉 This is just a normal parameter.
10. Real-Life Example
Example
11. Simple Practice Examples
Example 1: Multiply Numbers
Example 2: User Profile
12. Summary (Python *args and **kwargs)
✔ *args takes many values
✔ **kwargs takes many key–value pairs
✔ args is tuple
✔ kwargs is dictionary
✔ Very useful for flexible functions
📘 Perfect for Beginner eBook
This chapter is ideal for:
-
Python beginner books
-
School & college students
-
Self-learners
If you want next, I can write:
-
Lambda Functions
-
Recursion
-
Function Exercises
-
Real-life Function Examples
Just tell me 😊
