List Sum in Python

Level 239 keystrokes

This creates a list of numbers and prints their sum (15). The sum() function adds all elements in the list.

Python code

# Create a list with square brackets []
# Lists can hold multiple values separated by commas
1nums = [1, 2, 3, 4, 5]
# sum() is a built-in function that adds all numbers in an iterable
# 1 + 2 + 3 + 4 + 5 = 15
2print(sum(nums))

Output

15
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • ,× 4
  • (× 2
  • )× 2
  • [× 1
  • ]× 1
  • =× 1

More Python examples at level 2

← Python typing practice