Learn Python in 30 Days — Day 16: Arguments & Defaults
Learn Python in 30 Days — Day 16: Arguments & Defaults
Keyword Arguments, Defaults, Docstrings & Cleaner Code Organisation
Welcome to Day 16 of the Learn Python in 30 Days series!
Yesterday you learned how to define and call functions, return values, and write small reusable pieces of logic. Today, we’re going deeper into how you pass information into your functions to make them easier to read, use, and organise as your programs grow.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
By the end of this lesson, you’ll understand:
-
Positional vs keyword arguments
-
Default values (super useful for beginner-friendly code!)
-
Docstrings (the “official description” inside your functions)
-
How to organise your code into logical chunks
Let’s get into it.
Step 1 — Positional vs Keyword Arguments
When you call a function, you can pass arguments in two ways:
1. Positional arguments
Ordered arguments, meaning the position of the variable matters.
2. Keyword arguments
You name the parameter, so order does not matter.
Why it matters:
Keyword arguments make your code more readable and harder to break.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 2 — Default Arguments
Default arguments let you give parameters a built-in fallback value.
Why use defaults?
-
They simplify function calls
-
They make your functions flexible
-
They prevent errors if the user forgets a parameter
Perfect for configuring difficulty, settings, modes, units, themes, etc.
Step 3 — Keyword-Only Arguments
You can force some arguments to be keyword-only:
This stops bugs like calling the function with positional arguments, and keeps your function design clean and safe.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 4 — Docstrings
Every proper Python function should have a docstring, it is the instruction manual to the function you have made.
A docstring is a text description placed directly under the function definition:
Why docstrings matter:
-
Helps future-you remember what the function does
-
Required for automatic documentation tools
-
Shows up when you use
help(add_numbers)
This is a professional habit. Start now.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Step 5 — Basic Code Organisation
As your programs grow, you don’t want everything in one giant file. It is time to start thinking about using modules, where we organise code into sections:
Group related functions together
Keep your main program clean
You’ll go deeper into modules on Day 20, but today is your first taste of “structured code”.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub main.py and temperature_functions.py and run it yourself.
Mini-Project: Character Bio Generator
Let’s build something fun.
We’ll create a function that generates a simple RPG character bio.
What you’ve used:
-
Positional argument for name
-
Default argument for role
-
Keyword-only arguments for hp, mp
-
Docstring
-
Dictionary return (from earlier lessons!)
You're now writing cleaner, more advanced, future-proof Python functions.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Challenge
Upgrade the character generator:
-
Add a default weapon
-
Add an optional boolean
is_npcargument -
Add a keyword-only argument for
faction -
Add a docstring explaining all of the above
-
Return the data as a nicely formatted multiline string
Hint: You can use triple-quoted strings to return multiple lines.
You can also download this example from my GitHub here and run it yourself.
Next Up — Day 17 – Error Handling
You’ll learn how to use try and except functions and introduce a debugging mindset.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty






Comments
Post a Comment