PYTHON LESSON 15 OF 32

Defaults & keyword arguments

Default parameters make an argument optional. Keyword arguments name the parameter explicitly. Avoid using a mutable list as a default; use None and create a new list inside the function.

हिन्दी में समझें

डिफॉल्ट पैरामीटर से तर्क वैकल्पिक होता है। कीवर्ड तर्क पैरामीटर का नाम देता है। बदलने योग्य लिस्ट को डिफॉल्ट न रखें; None लें और अंदर नई लिस्ट बनाएँ।

अभ्यास: greet में message="Welcome" और name="Asha" दें।

Hinglish mein samjhein

Default parameter se argument optional ho jata hai. Keyword argument naam se value deta hai. Mutable list ko default na rakho; None use karke andar list banao.

Practice: greet mein message="Welcome",name="Asha" do.

A small Python example

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

print(greet("Asha"))
print(greet(name="Asha", message="Namaste"))

The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.

Try this example in the Python playground →

Practise this idea

Use greet with message="Welcome" and name="Asha".

Check answer & explanation
def greet(name, message="Hello"):
    return f"{message}, {name}!"

print(greet("Asha"))
print(greet(name="Asha", message="Namaste"))
print(greet(name="Asha", message="Welcome"))

Keyword arguments explicitly choose message and name. They override the default message without changing the reusable function.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

Which message is used when omitted?

  1. Hello
  2. Namaste
  3. None
Check the quiz answer

Hello

The default value of message is "Hello".

References & corrections

Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.