Skip to content

Instantly share code, notes, and snippets.

@tnibert
Last active May 4, 2019 23:33
Show Gist options
  • Save tnibert/a0da09699f001d5e56cf82d1875f5ef1 to your computer and use it in GitHub Desktop.
Save tnibert/a0da09699f001d5e56cf82d1875f5ef1 to your computer and use it in GitHub Desktop.
Summation (sigma) notation in python 3
#! /usr/bin/env python3
#
# Σ i
# i=s
from functools import reduce
# the sequence to sum, you can use range(blah) as well
s = [1, 2, 3, 5]
# alter this lambda for the right side of the sigma
right_side_of_sigma = lambda a, x: a + x
result = reduce(right_side_of_sigma, [0] + s)
print(result)
# another example:
result = reduce(lambda a, x: a + 3.14*x*x, [0]+[2,3,5])
assert result == ((3.14*2*2) + (3.14*3*3) + (3.14*5*5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment