Mathematical Operations in Python

Arithmetic Operators are usesd with numeric types (ints and floats) to perform basic mathematical operations.

In [1]:
# How to add numbers together?
print('Addition: 5 + 2 =', 5 + 2)

# How to subtract one number from another?
print('Subtraction: 5 - 2 =', 5 - 2)

# How to muliply two numbers together?
print('Multiplication: 5 * 2 =', 5 * 2)

# How to divide a number with another?
print('Division: 5 / 2 =', 5 / 2)

# How to find the Modulo (the reminder from a division)?
print('Modulo: 5 % 2 =', 5 % 2)

# Floor division : How to find the whole number from a division?
print('Floor division: 5 // 2 =', 5 // 2)

# How to find the Exponent (or power)?
print('Exponentiation: 5 ** 2 =', 5 ** 2)
Addition: 5 + 2 = 7
Subtraction: 5 - 2 = 3
Multiplication: 5 * 2 = 10
Division: 5 / 2 = 2.5
Modulo: 5 % 2 = 1
Floor division: 5 // 2 = 2
Exponentiation: 5 ** 2 = 25

Comparison Operators are used to compare two values.

In [3]:
# How to ask if two numbers are equal?
print('Equal to: 5 =+ 2:', 5 == 2)

# How to ask if two numbers are not equal?
print('Not equal to: 5 != 2:', 5 != 2)

# How to ask if a number is greater than another?
print('Greater than: 5 > 2:', 5 > 2)

# How to ask if a number is less than another?
print('Less than: 5 < 2:', 5 < 2)

# How to ask if a number is greater than or equal to another? 
print('Greater than or equal to: 5 >= 2:', 5 >= 2)

# How to ask if a number is less than or equal to another?
print('Less than or equal to: 5 <= 2:', 5 <= 2)
Equal to: 5 =+ 2: False
Not equal to: 5 != 2: True
Greater than: 5 > 2: True
Less than: 5 < 2: False
Greater than or equal to: 5 >= 2: True
Less than or equal to: 5 <= 2: False

Logical Operators are used to combine comparison (or conditional) statements.

In [4]:
# Logical and: True if and only if both statements are true 
print('Logical and: (5 == 2) and (5 > 2): ', (5 == 2) and (5 > 2))
print('False and True = ', False and True)
print('False and False = ', False and False)
print('True and True = ', True and True)

# Logical or: False only when bothe statements are false
print('Logical or: (5 == 2) or (5 > 2): ', (5 == 2) or (5 > 2))
print('False or True = ', False or True)
print('False or False = ', False or False)
print('True or True = ', True or True)

# Logical not: Invert true to false and vice versa
print('Logical not: not (5 > 2): ', not (5 > 2))
print('not True = ', not True)
print('not False = ', not False)
Logical and: (5 == 2) and (5 > 2):  False
False and True =  False
False and False =  False
True and True =  True
Logical or: (5 == 2) or (5 > 2):  True
False or True =  True
False or False =  False
True or True =  True
Logical not: not (5 > 2):  False
not True =  False
not False =  True

Membership Operators are used to test if an object is present in a sequence.

In [6]:
# How to test if an item is a member of a sequence?
print('Item present?: 1 in [1, 2, 3, 4, 5]: ', 1 in [1, 2, 3, 4, 5])
print('Item present?: "a" in ["a", "b", "c", "d", "e"]: ', "a" in ["a", "b", "c", "d", "e"])

# How to test if an item is not a member of a sequence?
print('Item absent?: 1 not in [1, 2, 3, 4, 5]: ', 1 not in [1, 2, 3, 4, 5])
Item present?: 1 in [1, 2, 3, 4, 5]:  True
Item present?: "a" in ["a", "b", "c", "d", "e"]:  True
Item absent?: 1 not in [1, 2, 3, 4, 5]:  False