Python From Scratch #33 - Default Arguments
Python From Scratch #33 - Default Arguments
Learn how to give function parameters default values in Python.
Hey 👋
In the last lesson, we learned how to pass arguments to functions using positional arguments and keyword arguments.
But what if you want a function to have a value ready to use when the caller doesn’t provide one?
That’s where default arguments come in.
What Are Default Arguments?
A default argument is a value you give to a parameter when you define a function.
For example:
def greet(name="Mark"):
print("Hello", name)Here, "Mark" is the default value for the name parameter.
If we call the function without an argument:
greet()
# Output:
# Hello MarkPython uses the default value because we didn’t provide one.
Providing a Different Value
You can still provide your own value when calling the function.
def greet(name="Mark"):
print("Hello", name)
greet("John")
# Output:
# Hello JohnWhen you provide an argument, Python uses that value instead of the default.
So greet() uses Mark while greet("John")uses John.
Why Use Default Arguments?
Default arguments are useful when a parameter usually has a common value, but you still want to give the caller the option to change it.
For example:
def greet(name="there"):
print("Hello", name)Now both of these work:
greet()
greet("Shefali")
# Output:
# Hello there
# Hello ShefaliYou don’t have to provide a value every time.
Multiple Default Arguments
A function can have more than one parameter with a default value.
For example:
def greet(name="there", message="Hello"):
print(message, name)Now:
greet()
# Output:
# Hello thereYou can also provide different values:
greet("Shefali", "Welcome")
# Output:
# Welcome ShefaliYou can also provide only the first value:
greet("John")
# Output:
# Hello JohnHere, "John" replaces the default value for name, while "Hello" remains the default value for message.
Default Arguments and Positional Arguments
Default arguments become especially useful when a function has some parameters that are optional.
For example:
def introduce(name, city="Delhi"):
print("Name:", name)
print("City:", city)The name parameter doesn’t have a default value, so we need to provide it.
But city has a default value.
This works:
introduce("Shefali")
# Output:
# Name: Shefali
# City: DelhiAnd we can also provide a different city:
introduce("Shefali", "Jaipur")
# Output:
# Name: Shefali
# City: JaipurA Rule to Remember
When defining a function, parameters without default values should come before parameters with default values.
This works:
def greet(name, message="Hello"):
print(message, name)But this does not:
def greet(name="Shefali", message):
print(message, name)Python raises a SyntaxError because a parameter without a default value comes after a parameter with a default value.
For now, the simple rule to remember is: Required parameters first, default parameters after them.
Default Values Can Be Different Types
A default value doesn’t have to be a string.
It can be a number too:
def show_age(age=18):
print("Age:", age)
show_age()
show_age(27)
# Output:
# Age: 18
# Age: 27You can also use other values you’ve already learned about.
Default Arguments vs Required Arguments
Compare these two functions:
def greet(name):
print("Hello", name)Here, name is required.
You need to provide it: greet("Shefali")
Now compare:
def greet(name="there"):
print("Hello", name)Here, name has a default value.
You can call the function without providing it: greet()
The main difference is whether the parameter already has a value to use when no argument is provided.
That’s all for today!
Tomorrow, we’ll learn about the return statement and how a function can give a value back to the code that called it.
If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️
Want to sponsor my work? Let’s talk!
See you tomorrow ✨
- Shefali
Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.
If you enjoyed this edition of DevBits, you can support my work here ☕
Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.
© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104