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.

def greet(name, age): print(f"Hello {name}, age {age}") greet("Matty", 32) # works greet(32, "Matty") # totally wrong order

2. Keyword arguments

You name the parameter, so order does not matter.

greet(name="Matty", age=32) greet(age=32, name="Matty") # also works

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.

def welcome(name, level="beginner"): print(f"Welcome {name}, you are set to {level} mode!") welcome("Matty") # uses default: beginner welcome("Matty", level="pro") # overrides the default

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.

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:

def make_profile(name, *, age, country): print(name, age, country) make_profile("Matty", age=32, country="UK") # must use keywords!
make_profile("Matty", 32, "UK") # NOT allowed

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:

def add_numbers(a, b): """ Adds two numbers together and returns the result. Parameters: a (int or float): First number. b (int or float): Second number. Returns: int or float: Sum of a and b. """ return a + b

print(add_numbers(5,3))

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

# temperature_functions.py def c_to_f(c): return (c * 9/5) + 32 def f_to_c(f): return (f - 32) * 5/9

Keep your main program clean

# main.py from temperature_functions import c_to_f print(c_to_f(30))

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.

def create_character(name, role="Adventurer", *, hp=100, mp=50): """ Creates a simple character profile. Parameters: name (str): Character name. role (str, optional): Character class. Defaults to "Adventurer". hp (int, keyword-only): Health points. mp (int, keyword-only): Magic points. Returns: dict: A character dictionary. """ return { "name": name, "role": role, "hp": hp, "mp": mp } # Examples print(create_character("Elyra")) print(create_character("Thorne", role="Warrior", hp=150, mp=20))

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:

  1. Add a default weapon

  2. Add an optional boolean is_npc argument

  3. Add a keyword-only argument for faction

  4. Add a docstring explaining all of the above

  5. 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

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test