Operators#

Arithmetic Operators#

Most programming languages allow you to perform arithmetic operations and mathematical calculations using arithmetic operators. Common arithmetic operations (with Python examples) include:

NamePython SyntaxPython ExampleDescription
Addition+5 + 6Adds values
Subtraction-5 - 6Subtracts values
Multiplication*5 * 6Multiples values
Integer division//5 // 6Divides values, integers (whole numbers)
Float division/5 / 6Divides values, floating point numbers (decimal values)
Modulo%5 % 6Returns or retrieves remainder (whole number) from division operation
Exponent**5 ** 6Calculates exponent

For more on arithmetic operators in Python:

We can run arithmetic operations directly in the console or in a script.

# sample arithmetic operation
5 + 7

But we can also assign the output of an arithmetic operation to a variable.

# sample arithmetic operation where output is assigned to variable
x = 5 + 7

# show variable value
print(x)

Python follows the PEDMAS order of operations: parenthesis, exponents, multiplication, division, addition, subtraction.

  • When in doubt, use parenthesis!

For more background on arithmetic operators:

Comparison Operators#

Relational operators, also called comparison operators, allow us to run True/False tests on specific conditions, focusing on the relationship(s) between two or more values.

Relational operators or comparison operators in Python:

NameSyntaxExampleDescription
Equal==x == yTests if values are equal
Not equal!=x != yTests if values are not equal
Greater than>x > yTests is a value is greater than another
Less than<x < yTests is a value is less than another
Greater than or equal to>=x >= yTests if a value is greater than or equal to another
Less than or equal to<=x <= yTests if a value is less than or equal to another

For more on comparison operators in Python:

  • W3Schools, Python Comparison Operators

  • “In mathematics and mathematical logic, Boolean algebra is the branch of algebra in which the values of the variables are the truth values true and false, usually denoted 1 and 0” (Wikipedia)

We’ve talked previously about the Boolean data type. The underlying True/False logic lets us test for specific conditions and then specify how our code will execute based on the truth value for those conditional statements.

  • “You can evaluate any expression in Python, and get one of two answers, True or False. When you compare two values, the expression is evaluated and Python returns the Boolean answer” (W3Schools, Python Booleans)

Boolean Logic & Comparison Operators#

We can use comparison operators with a print() statement to test whether a particular statement is True or False.

A couple Python examples:

print(4 == 5) # returns false
print(6 < 10) # returns true
# comparison operator using variables
x = 5
y = 6

print(x > y) # returns false

Comparison Operators & String Objects#

We can probably make intuitive sense of how these comparison operators would work for numeric values. But what about text characters or string objects?

Most programming languages treat the 26 characters in the English-language alphabet (a-z) as sequential values, where a is less than b, which is less than c, etc.

When working with string objects that represent words or characters, comparison operators indicate positionality in the English-language alphabet.

A few examples in Python:

print("apple" > "banana") # returns false

print("Ohio State" > "Notre Dame") # returns true

Logical Operators#

We can also compare or relate multiple conditions using logical operators: and, or, not

  • “A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, and NOT.” (Busbee and Braunschweig, “Logical Operators”)

For example, 5 < 6 and 6 < 7 evaluates whether 5 < 6 and 6 < 7 are true. This statement would return True.

Another example: 5 < 6 or 6 > 7 evaluates whether 5 < 6 or 6 > 7 is true. This statement would return True.

Logical operators in Python:

NameSyntaxExampleDescription
Andandx == y and y != zReturns True if both statements are true
Ororx != y or y == zReturns True if one of the statements is true
Notnotnot(x == y and y != z)Reverses the result, returns False if the result is true
# example using logical operators
print(5 < 6 and 6 < 7) # returns true because both statements are true

print(5 < 6 or 6 > 7) # returns true because at least one statement is true

print(not(5 < 6 and 6 < 7)) # returns false, inverse of initial true

Additional Resources#

For more background on comparison/relational and logical operators in Python: