Arithmetic

The simple arithmetic operators for Python are summarized below:

Arithmetic Operator Example
Parentheses ( ) a = (b + c) * d
Exponent ** a = b ** c
Division / a = b / c
Integer Division // a = b // c
Multiplication * a = b * c
Remainder (modulo) % a = b % c
Addition + a = b + c
Subtraction - a = b - c

Integer Division

Typically, division will give the normal value after division, regardless of the data types of b and c.

# The following will result in a floating point value.
a = 1 / 2
# The following will result in an integer value.
b = 1 // 2
# Print out the floating point and integer values.
print(a, b)

The code above will print: 0.5 0

Integer division means that everything to the right of the decimal point is truncated, meaning it is dropped--no rounding! If you want to round, use math.round().

Operator Precedence

You will notice that multiplication and division, as well as addition and subtraction have the same precedence. In these cases, they are evaluated left-to-right. For example a + b - c evaluates a + b and then (a + b) - c.

To force an ordering, use parentheses:

# Force addition first, then multiplication
result = (a + b) * c
# Force subtraction, then addition, and then multiplication
result = (a + (b - c)) * d

Example

# Set the variables (x1, x2) and (y1, y2)
x1 = 15
x2 = 7
y1 = 10
y2 = 25
# Now, calculate the slope
slope = (y2 - y1) / (x2 - x1)
# Print out the calculated value
print(slope)

The code above will calculate the slope of four hardcoded points, x1, x2, y1, and y2.

When you run the code, you should see the following:

-1.875

Non-primitive Arithmetic

Some arithmetic requires separate function calls, in which someone has written the Python code for you. One such arithmetic function is sqrt for square root. This function takes a parameter and returns the square root of that number. To be able to use this, you need to import the math library.

Math Library Example

# We need the math library to be able to call sqrt
import math

# Hard code some number
a = 49
# Get the square root of 49 and put the result into b
b = math.sqrt(a)
# Print out the result
print(b)

The code above should print the following:

7.0

In the code above, we need to include import math. This allows Python to go out and retrieve those functions already written for you in the math library. There are more functions inside, such as floor, factorial, fabs, exp, pi, pow, sin, sinh, tan, tanh, cos, cosh, atan, atan2, asin. Even more exist. Like most other programming languages, knowing which library to import is very important. Furthermore, knowing the parameters that each of these functions require and what they return is also very important.

Other Math Library Examples

import math

# The math.sin() function takes a value in radians. So, convert
# 90 into radians and see what the sin of that value is.
print(math.sin(90 * math.pi / 180))

# We can use math.pow() instead of the ** operator, but they perform
# the same operation.
# math.pow() automatically assumes a floating-point data type. It will always
# return a floating point data type, too. math.pow(10, 2) returns 100.0.
print(math.pow(10, 2))
print(10.0 ** 2)

# The exp() function will raise e to the given parameter
# These two functions should give the same result
print(math.exp(2))
print(math.e ** 2)

# The gcd() function returns the greatest common divisor between two values
# In this case, the GCD of 100 and 1024 is 4, so math.gcd(100, 1024) should return 4.
print(math.gcd(100, 1024))

# Rounding functions

# The ceil() function takes a floating-point number and returns an integer.
# This should print 11
print(math.ceil(10.225))
# This should print 20
print(math.ceil(19.001))

# The floor() function takes a floating-point number and returns an integer.
# This should print 10
print(math.floor(10.225))
# This should print 19
print(math.floor(19.001))

# The round() function is a more traditional rounding function using "round to the nearest"
# The round function will round anything > .5 up and anything <= .5 down.
# This should print 10
print(math.round(10.225))
# This should print 11
print(math.round(10.50001))
# This should print 10 (> .5 rounds up, <= .5 rounds down.)
print(math.round(10.5))

Library Reference

Again, knowing where each function lives and what its parameters are makes your life easier. You may find all of this information at the Python Library Reference page.


Stephen Marz (2019)