Operators and Expressions

In this lesson, we will learn how to use operators with variables and literal constants to build expressions. Operators behave differently depending on the data type of the operands.

for example the expression 1 + 1 uses the + operator to add two integers together. Here are some examples of how the + operator behaves for different data types:

# integer addition 
print(1 + 1) # 2
print(2 + 300) # 302

You can see that the + operator for float and integers performs addition. You can even mix the types, but the result will always be a float. However, the + operator performs concatenation for strings, which is the process of joining two strings together.

Arithmetic Operators

When we think about operators, we normally think about the operators that we use in math: + addition, - subtraction, * multiplication, and / division. These are the same operators that we use in Python to work with numbers.

print(1 + 1) # 2
print(1.5 + 1.5) # 3.0
print(1.5 + 1) # 2.5

There are also a few other operators that also work with numbers, but you might not be familiar with: // integer division(floor), % modulus(remainder), and ** exponentiation(power).

# divide the first operand by the second then round down
print(4 // 2) # 2
print(1.5 // 1.1) # 1.0
print(1.5 // 1) # 1.0
print(10 // 3) # 3

There is one operator that we have seen already. The = operator is used to assign a value to a variable. We can expand on our expression using assignment to store the result of an expression in a variable.

one_plus_one = 1 + 1

and then we can use the variable in other expressions.

two_plus_two = one_plus_one + one_plus_one
print(two_plus_two) # 4

Exercise

Given the variable x and the variable y which have been set for you, write the following

  1. Print the value of x multiplied by y
  2. Print the value of the output of step 1 divided by 1.5
  3. Print the value of the output of step 2 plus by 20
  4. Print the value of the output of step 3 minus 10

Arithmetic Operators Exercise

Loading...

Assignment Shortcuts

An operation and assignment can be combined into a single step. For example, we can use the += operator to add a value to a variable and assign the result to the variable.

x = 1
x += 1
print(x) # 2

This is equivalent to the following code:

x = 1
x = x + 1
print(x) # 2

This shortcut works for all of the math operators we have seen so far: +, -, *, /, //, %, and ** by adding = to the end of the operator.

Exercise

Given the variable x, use the assignment shortcuts to modify its value with the following operations:

  1. Multiply x by 3
  2. Divide x by 2
  3. Add 10 to x

Assignment Shortcuts Exercise

Loading...

Order of Operations

Operations inside an expression are evaluated in the order of their precedence. If there are multiple operations with the same precedence, they are evaluated from left to right. It's important to know this order because it can affect the result of an expression. The order of operations is as follows:

  1. Parentheses
  2. Exponents
  3. Multiplication and Division
  4. Addition and Subtraction
  5. Assignment

In the expression 2 + 3 * 4, the multiplication happens before the addition. However, We can use parentheses to change the order of operations.

print(2 + 3 * 4) # 14
print((2 + 3) * 4) # 20
print(2 * 3 ** 2) # 18
print((2 * 3) ** 2) # 36

Exercise

Using parentheses change the order of operations in the following expressions to get the desired result.

Exercise

Loading...

You can run help('+') in Python to see more details on operator precedence.