Math Operators

There are several math operators that can be used to perform calculations:
  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Exponent **
  • Parentheses ()
Python follows the rules of PEMDAS, meaning that parentheses, exponents, multiplication, division, addition, and subtraction are performed in that order, from left to right. Example of calculating the area of a trapezoid:
# area of a trapezoid
base1 = 8
base2 = 10
height = 20
area = (base1 + base2) * height / 2
print(area)
# 180
The values of base1 and base2 are first added together to get 18 since they are in parentheses. That value of 18 is then multiplied by height and divided by 2, in order from left to right. The resulting value of 180 is then saved in the variable area and printed to the console.

Exercise 1 of 5

Which of these converts the following into code: x equals 2 plus 3

Exercise 2 of 5

Which of these converts the following into code: x equals 6 minus 3

Exercise 3 of 5

Convert the following into code: x equals 13 times 6

Your code

Exercise 4 of 5

Create a variable named x that is equal to Sally's age plus 4.
sally_age = 12

Your code

Exercise 5 of 5

Fix the error in the line of code.
x == 6 / 5

Your code