2.3 Arithmetic

Many programs perform arithmetic calculations. The following table summarizes the arithmetic operators, which include some symbols not used in algebra.

A table summarizes the arithmetic operators, which include some symbols not used in algebra.

Multiplication (*)

Rather than algebra’s center dot (·), Python uses the asterisk (*) multiplication operator:

In [1]: 7 * 4
Out[1]: 28

Exponentiation (**)

The exponentiation (**) operator raises one value to the power of another:

In [2]: 2 ** 10
Out[2]: 1024

To calculate the square root, you can use the exponent 1/2 (that is, 0.5):

In [3]: 9 ** (1 / 2)
Out[3]: 3.0

True Division (/) vs. Floor Division (//)

True division (/) divides a numerator ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.