The Complete Guide to args kwargs for Python Beginners: Arguments Parameter Differences

hello, Python Hey, newbie developers! 🐍.
If you've ever come across weird things like args kwargs while writing functions and wondered, "What is this and why do I need it?", today's post is for you!
Let's dive into the world of args and kwargs and arguments and parameters together! Don't worry, it's not hard, I'll make it easy and fun for you, and by the end of this post, you'll be a Python master with args and kwargs at your disposal! 😎
What are args kwargs?
First, let's look at what args kwargs are. In a nutshell, they're a special syntax for passing multiple arguments to a function. They're used with a *, as shown below.
- *args: Pass multiple positional arguments to Tuplesin the file.
- **kwargs: multiple keyword arguments to Dictionariesin the file.
Doesn't sound familiar? Don't worry, we'll walk you through it with an example!
Example of using args kwargs
Example of using *args
def print_args(*args):
for arg in args:
print(arg)
print_args('hello', 'python', 'world')Code commentary:
def print_args(*args):: *args to define a function named print_args.for arg in args:: traverses all arguments in args.print(arg): Print each argument.print_args('hello', 'python', 'world')Call the : function and pass multiple arguments.
This will print "hello", "python", and "world" on a new line each. Cool, right?

Example of using **kwargs
Let's try kwargs this time.
def print_kwargs(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_kwargs(name="Python", age=30, occupation="Programmer")Code commentary:
def print_kwargs(**kwargs):: **Define a function named print_kwargs using kargs.for key, value in kwargs.items():: Traverse all key-value pairs in kwargs.print(f"{key}: {value}"): Print each key-value pair.print_kwargs(name="Python", age=30, occupation="Programmer"): Call the function with a keyword argument.
This will output "Name: Python", "Age: 30", and "Occupation: Programmer", each on a new line. How convenient, right?

Argument parameter differences
Now, let's talk about the argument parameter difference, which looks similar, but is actually a different concept.
- Parameter: The DefinitionWhen you use Variables you useAnswer.
- Argument: The callWhen you use The actual value you passAnswer.
Here's an example of creating a function named greet and then calling it.
def greet(name): # where name is a parameter
print(f"Hello, {name}!")
greet("Python") # where "Python" is the argumentDoes this make sense? Think of parameters as the "frame" of a function, and arguments as the "actual values" you put into that frame.
How do we actually utilize args kwargs?
args and kwargs can be useful in a lot of different situations, especially when the number of arguments is fluid.
def make_pizza(*toppings, **details):
print(f"Pizza toppings: {', '.join(toppings)}")
for key, value in details.items():
print(f"{key}: {value}")
make_pizza("cheese", "olives", "green peppers", size="large", price=20000)Code commentary:
def make_pizza(*toppings, **details):: Use args and kwargs at the same time.print(f"Pizza toppings: {', '.join(toppings)}"): Print all toppings separated by commas.for key, value in details.items():: Navigate to more information.print(f"{key}: {value}"): Print each additional information.make_pizza("cheese", "olives", "green peppers", size="large", price=20000): Call a function by passing various arguments.
This gives you the freedom to pass on toppings and additional information, which is really handy, right?

So, that's it for args kwargs and argument parameter differences. What do you think? It's not as hard as you think, right? You should be able to put these concepts to good use by now.







