Pybites Platform

Back Next →

## Sum n numbers

Pybites
View Learning Paths
Python Beginner

Level: Beginner (score: 2)

Write a Python function that calculates the sum of a list of (int) numbers:

- The function should accept a list of numbers and return the sum of those numbers.
- If no argument is provided (that is, `numbers` is `None`), return the sum of the numbers 1 to 100 (Note that this is not the same as an empty list of numbers being passed in. In that case the sum returned will be 0).

Have fun!

summing

- Code
- Tests
- Solution & Forum

def sum_numbers(numbers=None): # Hello return 5050 if numbers is None else sum(numbers)

    xxxxxxxxxx

4
1

    def sum_numbers(numbers=None):

2

        # Hello

3

        return 5050 if numbers is None else sum(numbers)

4

    ​

from summing import sum_numbers def test_sum_numbers_default_args(): assert sum_numbers() == 5050 assert sum_numbers(numbers=None) == 5050 def test_sum_numbers_various_inputs(): assert sum_numbers(range(1, 11)) == 55 assert sum_numbers([1, 2, 3]) == 6 assert sum_numbers((1, 2, 3)) == 6 assert sum_numbers([]) == 0 # !! [] not the same as None
def sum_numbers(numbers=None): if numbers is None: numbers = range(1, 101) return sum(numbers)
Automatically format your code and check for style violations (PEP 8, flake8) using ruff.
Go to Bite Forum