Skip to main content
🐍 Python Basics 🐍

Default Arguments βš™οΈ

A parameter can have a default value, used when the caller leaves it out. You set it with = right in the def line:

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

greet("Sam")            # "Hello, Sam!"  (used the default)
greet("Sam", "Hi")      # "Hi, Sam!"     (gave our own)

We also need ** here β€” that's Python's power symbol. base ** exp means "base multiplied by itself exp times":

2 ** 3   # 2 * 2 * 2 = 8
3 ** 2   # 3 * 3 = 9

Your task

Write power(base, exp=2) that returns base raised to exp. Called with one argument it should square the number: power(3) returns 9, and power(2, 3) returns 8.

⚑ 40 XP

🧱 Code blocks

Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.

solution.pyRuns in your browser (Pyodide)
Loading editor…
Tests3 to pass
power(3) == 9
power(2, 3) == 8
power(5, 0) == 1

Run your code to check these.