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.
+ operator performs concatenation for strings, which is the process of
joining two strings together.
When we think about operators, we normally think about the operators that we use in math:
* 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),
# 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
Given the variable
x and the variable
y which have been set for you, write the following
- Print the value of
- Print the value of the output of step 1 divided by
- Print the value of the output of step 2 plus by
- Print the value of the output of step 3 minus
Arithmetic Operators Exercise
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:
= to the end of the operator.
Given the variable
x, use the assignment shortcuts to modify its value with the following operations:
Assignment Shortcuts Exercise
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:
- Multiplication and Division
- Addition and Subtraction
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
Using parentheses change the order of operations in the following expressions to get the desired result.
You can run
help('+') in Python to see more details on operator precedence.