How to write sigma in python

An efficient way to do this in Python is to use reduce().

To solve

3
Σ i
i=1

You can use the following:

from functools import reduce
result = reduce(lambda a, x: a + x, [0]+list(range(1,3+1)))
print(result)

reduce() will take arguments of a callable and an iterable, and return one value as specified by the callable. The accumulator is a and is set to the first value (0), and then the current sum following that. The current value in the iterable is set to x and added to the accumulator. The final accumulator is returned.

The formula to the right of the sigma is represented by the lambda. The sequence we are summing is represented by the iterable. You can change these however you need.

For example, if I wanted to solve:

Σ π*i^2
i

For a sequence I [2, 3, 5], I could do the following:

reduce(lambda a, x: a + 3.14*x*x, [0]+[2,3,5])

You can see the following two code lines produce the same result:

>>> reduce(lambda a, x: a + 3.14*x*x, [0]+[2,3,5])
119.32
>>> (3.14*2*2) + (3.14*3*3) + (3.14*5*5)
119.32

Below is a simple example of a sigma function. You can also download sigma.py and play around with it if you like.

Here are the same functions and sigma applied algebraically:

How to write sigma in python

Leone Learning Systems, Inc.

How to write sigma in python

This work is licensed under a Creative Commons Attribution 3.0 Unported License.
Terms of Use. Privacy Policy.

Unicode characters are very useful for engineers. A couple commonly used symbols in engineers include Omega and Delta. We can print these in python using unicode characters. From the Python interpreter we can type:

>>> print('Omega: \u03A9')
Omega: Ω
>>> print('Delta: \u0394')
Delta: Δ
>>> print('sigma: \u03C3')
sigma: σ
>>> print('mu: \u03BC')
mu: μ
>>> print('epsilon: \u03B5')
epsilon: ε
>>> print('degree: \u00B0')
degree: °
>>> print('6i\u0302 + 4j\u0302-2k\u0302')
6î + 4ĵ-2k̂

All of these are unicode characters. Python has support for unicode characters built in. You can check if your system supports it by importing the sys module and calling the sys.getdefaultencoding() function

>>> import sys
>>> sys.getdefaulencoding()
'utf-8'

If you see utf-8, then your system supports unicode characters. To print any character in the Python interpreter, use a \u to denote a unicode character and then follow with the character code. For instance, the code for β is 03B2, so to print β the command is print('\u03B2').

There are a couple of special characters that will combine symbols. A useful one in engineering is the hat ^ symbol. This is typically used to denote unit vectors. We can add a hat ^ (also called a circumflex) by putting the unicode escape after the letter you want to add a hat to. For example to add a hat to i the command is print('i\u0302').

Below is a list of symbols and greek letters and the corresponding unicode escape to produce the character in python.

Useful unicode symbols in engineering

unicodecharacterdescription
\u0394 Δ GREEK CAPITAL LETTER DELTA
\u03A9 Ω GREEK CAPITAL LETTER OMEGA
\u03C0 π GREEK SMALL LETTER PI
\u03F4 ϴ GREEK CAPITAL THETA SYMBOL
\u03BB λ GREEK SMALL LETTER LAMDA
\u03B8 θ GREEK SMALL LETTER THETA
\u03B1 ° DEGREE SYMBOL
i\u0302 i HAT
j\u0302 j HAT
k\u0302 k HAT
u\u0302 u HAT

Greek lower case letters

unicodecharacterdescription
\u03B1 α GREEK SMALL LETTER ALPHA
\u03B2 β GREEK SMALL LETTER BETA
\u03B3 γ GREEK SMALL LETTER GAMMA
\u03B4 δ GREEK SMALL LETTER DELTA
\u03B5 ε GREEK SMALL LETTER EPSILON
\u03B6 ζ GREEK SMALL LETTER ZETA
\u03B7 η GREEK SMALL LETTER ETA
\u03B8 θ GREEK SMALL LETTER THETA
\u03B9 ι GREEK SMALL LETTER IOTA
\u03BA κ GREEK SMALL LETTER KAPPA
\u03BB λ GREEK SMALL LETTER LAMDA
\u03BC μ GREEK SMALL LETTER MU
\u03BD ν GREEK SMALL LETTER NU
\u03BE ξ GREEK SMALL LETTER XI
\u03BF ο GREEK SMALL LETTER OMICRON
\u03C0 π GREEK SMALL LETTER PI
\u03C1 ρ GREEK SMALL LETTER RHO
\u03C2 ς GREEK SMALL LETTER FINAL SIGMA
\u03C3 σ GREEK SMALL LETTER SIGMA
\u03C4 τ GREEK SMALL LETTER TAU
\u03C5 υ GREEK SMALL LETTER UPSILON
\u03C6 φ GREEK SMALL LETTER PHI
\u03C7 χ GREEK SMALL LETTER CHI
\u03C8 ψ GREEK SMALL LETTER PSI
\u03C9 ω GREEK SMALL LETTER OMEGA

Greek upper case letters

unicodecharacterdescription
\u0391 Α GREEK CAPITAL LETTER ALPHA
\u0392 Β GREEK CAPITAL LETTER BETA
\u0393 Γ GREEK CAPITAL LETTER GAMMA
\u0394 Δ GREEK CAPITAL LETTER DELTA
\u0395 Ε GREEK CAPITAL LETTER EPSILON
\u0396 Ζ GREEK CAPITAL LETTER ZETA
\u0397 Η GREEK CAPITAL LETTER ETA
\u0398 Θ GREEK CAPITAL LETTER THETA
\u0399 Ι GREEK CAPITAL LETTER IOTA
\u039A Κ GREEK CAPITAL LETTER KAPPA
\u039B Λ GREEK CAPITAL LETTER LAMDA
\u039C Μ GREEK CAPITAL LETTER MU
\u039D Ν GREEK CAPITAL LETTER NU
\u039E Ξ GREEK CAPITAL LETTER XI
\u039F Ο GREEK CAPITAL LETTER OMICRON
\u03A0 Π GREEK CAPITAL LETTER PI
\u03A1 Ρ GREEK CAPITAL LETTER RHO
\u03A3 Σ GREEK CAPITAL LETTER SIGMA
\u03A4 Τ GREEK CAPITAL LETTER TAU
\u03A5 Υ GREEK CAPITAL LETTER UPSILON
\u03A6 Φ GREEK CAPITAL LETTER PHI
\u03A7 Χ GREEK CAPITAL LETTER CHI
\u03A8 Ψ GREEK CAPITAL LETTER PSI
\u03A9 Ω GREEK CAPITAL LETTER OMEGA
\u03F4 ϴ GREEK CAPITAL THETA SYMBOL

How do you define a sigma in Python?

The Σ operator has three parts to it. Below it is a bound variable, i and the starting value for the range, written as i =0. Above it is the ending value for the range, usually something like n . To the right is some function to execute for each value of the bound variable.

How do you write summation in Python?

Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.