Skip to content

Arithmetic built-ins and augmented assignment [ref]

Problem

  • Compute the sum, minimum, and maximum of [3, -2, 5], plus the absolute value of -7.
  • Raise 2 to power 10.
  • Add 3 to x = 4, then multiply it by 2 using augmented assignment.
  • Compute the sum from 1 through 5 with the triangular formula.
  • Sum [True, False, True].
  • Sum every number in the rows [[1, 2], [3, 4]] using map.

Python solution

nums = [3, -2, 5]
sum(nums)  # 6
min(nums)  # -2
max(nums)  # 5
abs(-7)    # 7

2 ** 10  # 1024

x = 4
x += 3
x *= 2
x  # 14

n = 5
n * (n + 1) // 2  # 15

sum([True, False, True])  # 2

sum(map(sum, [[1, 2], [3, 4]]))  # 10