PythonDefining FunctionsPractice Problems

Practice Problems

Strengthen your understanding of def with these auto-graded scenarios.

No input() calls—run each snippet directly and compare with the expected output.


1. Function with Default Parameter

Pyground

Write a greet function that defaults the greeting to 'Hello' but allows overrides.

Expected Output:

Hello, Riya!
Hi, Kabir!

Output:


2. Variadic Sum

Pyground

Implement add_all(*numbers) that returns the sum of all arguments.

Expected Output:

6
0

Output:


3. Keyword-Only Parameter

Pyground

Define send_email(to, *, subject, body) and call it with keywords for subject and body.

Expected Output:

To: team@example.com
Subject: Standup
Daily update.

Output:


4. Closure for Counters

Pyground

Create counter(start) that returns a function incrementing from start by 1 each call.

Expected Output:

6
7

Output:


5. Decorator Practice

Pyground

Write a decorator that prefixes 'DEBUG:' to the string returned by a function.

Expected Output:

DEBUG: Service healthy

Output:


6. Positional-Only Parameter

Pyground

Define slugify(text, /) that replaces spaces with hyphens and lowercases the text.

Expected Output:

feature-flag

Output: