A parameter with a default, def greet(name, greeting="Hello"):, can be left out by the caller. Parameters without defaults must come first. Arguments can be passed by position or by name: greet("Ada", greeting="Hi"). Naming them makes calls readable and lets you skip earlier optional parameters.
Use immutable defaults (strings, numbers, None). A mutable default like [] is created once and shared between calls, a famous surprise; write items=None and create the list inside when it is None.
def repeat(text, times=2, sep=" "):
return sep.join([text] * times)
print(repeat("ho")) # ho ho
print(repeat("ho", 3, sep="-")) # ho-ho-hoYour task
Write greet(name, greeting="Hello", punctuation="!") returning "<greeting>, <name><punctuation>", and power(base, exponent=2) returning base raised to exponent.
Hint 1
def greet(name, greeting="Hello", punctuation="!"):
Hint 2
return f"{greeting}, {name}{punctuation}"
Show the solution
def greet(name, greeting="Hello", punctuation="!"):
return f"{greeting}, {name}{punctuation}"
def power(base, exponent=2):
return base ** exponent