None or Default

Easy ⏱ 8 min 74% acceptance ★★★★★ 4.6
Write a function first_present(value, default) that returns value if it is not <code>None</code>, otherwise returns default. This mirrors a common config-loading pattern where a setting may be unset.

Examples

Example 1
Input
first_present(None, 10)
Output
10
Explanation

value is None, so default is used.

Example 2
Input
first_present(0, 10)
Output
0
Explanation

0 is not None, so it is kept (even though it is falsy).

Constraints

  • `value` may be any type, including 0, "", or False.
  • Do not use plain `or`, since that would wrongly replace falsy-but-present values like 0.

Topics

Variables & Data TypesNone handling

Companies

UberAirbnb

Hints

Hint 1

Check explicitly with `value is None`.

Hint 2

A one-line conditional expression works well here.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.