Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 68 ยท Getting Started

Mathematical Notation and Python

Translate mathematical symbols into Python: sigma and pi as sums and products, quantifiers as all() and any(), set-builder notation, floor, ceiling and divmod.

Mathematics is written in a compact shorthand. A single line with a big sigma, a few subscripts and a curly brace can describe a computation that takes ten lines of code. The good news is that almost every symbol you meet in discrete mathematics has a direct Python translation. Once you know the dictionary, formulas stop looking like hieroglyphs and start looking like short programs.

This lesson builds that dictionary. You will translate sums and products into generator expressions, quantifiers into all() and any(), set-builder notation into comprehensions, and rounding and remainder notation into math functions and divmod.

Sums and products

The capital Greek letter sigma means "add up". Written under and over it are the starting and ending values of an index. The formula "sigma, i from 1 to 5, of i squared" means 1 + 4 + 9 + 16 + 25. In Python that is a loop that accumulates, or more compactly a generator expression passed to sum:

Python
total = 0
for i in range(1, 6):
    total += i ** 2

total = sum(i ** 2 for i in range(1, 6))

Watch the end point. Math ranges are usually inclusive on both sides, but Python's range(a, b) stops before b. The math range 1 to 5 is range(1, 6).

The capital pi means "multiply together". The product of i for i from 1 to n is n factorial, written n!. Python has math.prod for this.

Python Sigma and pi as code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The empty cases matter. A sum with nothing in it is 0, because 0 is the identity for addition. A product with nothing in it is 1, the identity for multiplication. This is the same convention that makes 0! equal to 1, and it will keep appearing in proofs.

Quantifiers as all() and any()

Two symbols from logic describe statements about collections:

Symbol Read as Python
the upside-down A "for all" all(...)
the backwards E "there exists" any(...)

"For every x in S, x is positive" becomes all(x > 0 for x in S). "There is an x in S that is even" becomes any(x % 2 == 0 for x in S).

On a finite collection, this translation is exact, and it shows two details worth remembering. First, all of an empty collection is True (nothing can break the rule), and any of an empty collection is False (there is nothing to be the witness). Second, the statement "there is no x with property P" is not any(...), which equals all(not P ...). You will meet this as a negation rule in Predicates and Quantifiers.

Python Quantifiers over a finite set

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Quantifiers over infinite sets, such as "for every integer", cannot be checked by a loop that finishes. Python can test a finite slice, which is useful for finding counterexamples, but only a proof covers every case.

Set-builder notation versus comprehensions

Mathematicians describe sets by a rule: the set of all x in some collection such that a condition holds. A common way to write it is { x in A | x is even }, or with a colon in place of the bar. Python's set comprehension has the same three parts: where the elements come from, the condition and the expression.

Python
A = range(1, 21)
evens = {x for x in A if x % 2 == 0}
squares = {x * x for x in A}

The pieces map one to one:

Math Python
{ xยฒ : x in A } {x ** 2 for x in A}
{ x in A : x is even } {x for x in A if x % 2 == 0}
{ (x, y) : x in A, y in B } {(x, y) for x in A for y in B}

One difference: a Python set forgets order and ignores duplicates, exactly like a mathematical set, while a list comprehension keeps both. Choose the container that matches the object you are describing. See the Python lessons on comprehensions and sets for the language details.

Python Building sets with a rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that twenty values of x * x % 5 collapse to only three distinct values, 0, 1 and 4. A set records that fact for free.

Floor, ceiling and division

Three bracket-like symbols appear all the time:

  • The floor of x (a bracket with only the bottom corners) is the largest integer not above x.
  • The ceiling of x (only the top corners) is the smallest integer not below x.
  • The absolute value bars give the distance from zero.

In Python these are math.floor, math.ceil and abs. Pay attention to negative numbers: the floor of -2.5 is -3, not -2. Python's // operator is floor division, so -7 // 2 is -4.

The division algorithm says that for integers a and d with d positive, there are unique integers q (quotient) and r (remainder) with a = dยทq + r and 0 โ‰ค r < d. Python gives you both at once with divmod(a, d), and % is the remainder. For a positive divisor, Python's % always lands in the range 0 to d - 1, even when a is negative, which matches the math definition (many other languages do not).

Python Floor, ceiling and the division algorithm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows a handy trick for exact integer ceiling division. It avoids floating point, which matters for very large integers.

Reading other common symbols

A short field guide for the rest of the course:

  • Membership x โˆˆ S is x in S; x โˆ‰ S is x not in S.
  • Subset A โІ B is A <= B for sets; a proper subset is A < B.
  • Union and intersection A โˆช B and A โˆฉ B are A | B and A & B. Set difference A \ B is A - B.
  • Cardinality |A| is len(A).
  • Divides a | b means b is a multiple of a, which is b % a == 0 (for a not zero).
  • Congruence a โ‰ก b (mod m) is (a - b) % m == 0.
  • Logic โˆง, โˆจ, ยฌ are and, or, not. Implication has no direct operator; it is (not p) or q.
  • Implication p โ†’ q and equivalence p โ†” q are covered in Implication and Biconditional.

A tiny symbol dictionary

Sigma sum(... for ...)
For all all(...)
There exists any(...)
Set builder A set comprehension.
1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Python expression computes the sum of i cubed for i from 1 to 10, inclusive?

    Math ranges include both endpoints, but range stops before its second argument, so 1 to 10 inclusive is range(1, 11). The product version multiplies instead of adding.
  2. 2. What does all(x > 5 for x in []) return?

    With no elements, nothing violates the condition, so a universal statement over an empty collection is vacuously true.
  3. 3. What are divmod(-17, 5)?

    Python uses floor division, so the quotient is -4 and the remainder is 3, because 5 * (-4) + 3 equals -17 and the remainder is between 0 and 4.
  4. 4. Which translation of 'there is no x in S with x > 50' is correct?

    'No x has the property' is the negation of 'some x has the property', so it is not any(...). It equals all(x <= 50 for x in S).

Summary

  • Sigma and pi become sum(...) and math.prod(...); remember that range excludes its end, and that empty sums are 0 and empty products are 1.
  • "For all" is all() and "there exists" is any(); both work exactly on finite collections, and an empty collection makes all true and any false.
  • Set-builder notation maps to set comprehensions: source, condition and expression.
  • Floor, ceiling, //, % and divmod express the division algorithm; Python's remainder stays non-negative for a positive divisor.
  • Keep a symbol dictionary and test every formula on small values.

Next: Propositional Logic, where true and false statements are combined with and, or and not.