Python Default Arguments

PythonPythonBeginner
Practice Now

Introduction

Envision the sands of ancient Egypt, the pyramids towering overhead, casting long shadows over a landscape steeped in mystique and history. In this lab, we venture inside a newly-unearthed chamber within the great Pyramid of Khufu. Hieroglyphics on the chamber walls tell of a lost deity whose powers would be restored once their avatar is summoned with the correct incantation. It is said this god had the power to grant wisdom and knowledge, particularly in the arcane art of Python programming.

Our goal is to decode the incantation needed to summon this deity’s avatar by solving Python puzzles laid out in the chamber. To do this, we must master the use of default arguments in Python functions—a concept as timeless as the pyramids themselves.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") subgraph Lab Skills python/default_arguments -.-> lab-271545{{"`Python Default Arguments`"}} end

Create a Function with Default Arguments

In this step, we'll create a function that will help us unlock the first seal to the deity's chamber. Python functions can have default arguments, which are used if no specific value is provided when the function is called. Let's practice this by creating a function that generates an ancient spell with default power levels.

Now, open ~/project/incantation.py using your favorite text editor and define a function named cast_spell which accepts two parameters: spell (a string) and power_level (an integer with a default value of 5).

Example code in incantation.py:

def cast_spell(spell, power_level=5):
    return f"Invoking {spell} with a power level of {power_level}!"

## Test the function by calling it with and without specifying the power_level
print(cast_spell('Heka'))
print(cast_spell('Seshat', power_level=9))

Run your code to see the spells being invoked.

python3 incantation.py

Expected output:

Invoking Heka with a power level of 5!
Invoking Seshat with a power level of 9!

Handling Multiple Default Arguments

Next, we shall work with a function that takes multiple default arguments to unravel the second seal. We'll create a function that describes the offerings to be made to the deity.

Still within the ~/project directory, add a new function to our incantation.py file called make_offering, which will take three parameters: food (defaulting to "grain"), incense (defaulting to "frankincense"), and gemstone (defaulting to "lapis lazuli").

Example code in incantation.py:

def make_offering(food="grain", incense="frankincense", gemstone="lapis lazuli"):
    return f"Offering {food}, {incense}, and {gemstone} to please the gods."

## Test the function with default and custom arguments
print(make_offering())
print(make_offering(food="dates", gemstone="turquoise"))

Run the updated file:

python3 incantation.py

Expected output:

Offering grain, frankincense, and lapis lazuli to please the gods.
Offering dates, frankincense, and turquoise to please the gods.

Summary

In this lab, we delved into the power of Python default arguments through the backdrop of an ancient Egyptian pyramid adventure. Not only did you learn to define functions with default parameters and customize them on demand, but you also harnessed the magic of an Egyptian deity by combining Python syntax with the allure of Egyptology.

By completing these tasks, you've gained a valuable skill that will serve you well in writing flexible and reusable Python code. Now that you've mastered default arguments, you're ready to uncover more secrets in the world of Python—the language that is as enigmatic as the pyramids, yet as accessible as the open desert under the clear, moonlit sky.

Other Python Tutorials you may like