Arithmetic Operators are usesd with numeric types (ints and floats) to perform basic mathematical operations.
# 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)
Comparison Operators are used to compare two values.
# 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)
Logical Operators are used to combine comparison (or conditional) statements.
# 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)
Membership Operators are used to test if an object is present in a sequence.
# 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])