Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Parameters & Arguments
Parameters are variables listed in the function definition. Arguments are the actual values passed when calling the function. Python supports positional, keyword, default, and variable-length arguments.
Parameters & Arguments
# Positional arguments (order matters!)
def introduce(name, age, city):
print(f"I'm {name}, {age} years old from {city}")
introduce("Alice", 25, "Mumbai")
# Keyword arguments (order doesn't matter)
introduce(city="Delhi", name="Bob", age=30)
# Mixing positional and keyword
introduce("Charlie", age=22, city="Bangalore")
# Default parameters
def power(base, exponent=2):
return base ** exponent
print(power(5)) # 25 (uses default exponent=2)
print(power(5, 3)) # 125 (overrides default)
# Important: default parameters must come AFTER required ones
def create_user(name, role="student", active=True):
print(f"User: {name}, Role: {role}, Active: {active}")
create_user("Alice")
create_user("Bob", "teacher")
create_user("Charlie", active=False)Tip
Tip
Use keyword arguments for functions with many parameters. send_email(to='alice@x.com', urgent=True) is clearer than send_email('alice@x.com', True).
Functions are first-class objects in Python - pass them, return them, store them
Common Mistake
Warning
Putting default parameters before required ones causes SyntaxError. def func(a=10, b) is invalid. Required params must come first.
Practice Task
Note
(1) Write a function with 2 required and 1 default parameter. (2) Call it using positional and keyword args. (3) Test skipping the default.