easy +5 pts

Average of a list

Compute the arithmetic mean of a list of numbers with a clear edge case for empty lists.

Implement the function `average(nums)` that takes a list of numbers (ints or floats) and returns their arithmetic mean as a float. If the list is empty, return `0.0`. Do not import any modules. **Input:** A list `nums` of numbers. The list may be empty. **Output:** The average as a float. For non-empty lists, the result must equal `sum(nums) / len(nums)`. **Examples:** - `average([1, 2, 3, 4, 5])` returns `3.0`. - `average([2.5, 3.5])` returns `3.0`. - `average([])` returns `0.0`.

Constraints

Input list length: 0 ≤ len(nums) ≤ 10^5. Each element is an int or float. The sum of elements will not exceed the range of Python floats.

Example

>>> average([1, 2, 3, 4, 5])
3.0
>>> average([2.5, 3.5])
3.0
>>> average([])
0.0
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Consider the case when the list is empty.
Use the built-in `sum` and `len` functions.
Remember that division with `/` returns a float in Python 3.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.