What are operators in Python?

Completed

Typically, math involves about four core operations: addition, subtraction, multiplication, and division. Python supports these four operators and a few others. Let's explore the most common operators you'll use in your programs.

Python uses + to indicate addition. Using + between two numbers adds them together and provides the total.

answer = 30 + 12

# Output: 42


Note

Operators behave the same when using literal numbers (such as 42) or variables.

Subtraction

Similarly, Python uses - for subtraction. Using - between two numbers subtracts the two numbers and provides the difference.

difference = 30 - 12
print(difference)

# Output: 18


Multiplication

In Python, * is the multiplication operator. It provides the product of two numbers:

product = 30 * 12
print(product)

# Output: 360


Division

Finally, / is used for division. It provides the quotient of two numbers:

quotient = 30 / 12
print(quotient)

# Output: 2.5


Work with division

Imagine you need to convert a number of seconds into minutes and seconds for display.

seconds = 1042


The first step is to determine the number of minutes in 1042 seconds. With 60 seconds in a minute, you can divide by 60 and get an answer of 17.3666667. The number you're interested in is simply 17. You always want to round down, by using what's known as floor division. To perform floor division in Python, you use //.

seconds = 1042
display_minutes = 1042 // 60
print(display_minutes)

# Output: 17


The next step is to determine the number of seconds. This is the remainder of 1042 if you divide by 60. You can find the remainder by using the modulo operator, which is % in Python. The remainder of 1042 / 60 is 22, which is what the modulo operator will provide.

seconds = 1042
display_minutes = 1042 // 60
display_seconds = 1042 % 60

print(display_minutes)
print(display_seconds)

# Output:
# 17
# 22


Order of operation

Python honors the order of operation for math. The order of operation dictates that expressions should be evaluated in the following order:

1. Parentheses
2. Exponents
3. Multiplication and division
result_1 = 1032 + 26 * 2