Operators and Expressions#

What you will learn in this lesson:

  • What are operators, what they are used for and the type of operators.

  • What do we mean by expressions.

Operators#

If variables are nouns, and values meanings, then operators are verbs.

Operators are special symbols that are expressed in sequential syntax.

Each data type is associated with a set of operators that allow you to manipulate the data in way that makes sense for its type. Numeric data types are subject to mathematical operations, booleans to logical ones, and so forth.

There are also operations appropriate to structures. For example, list-like things have membership (We will see this soon).

In this lesson we will just concentrate on the operators acting on the basic data types we saw in the previous lessons. We will see how operators act in more complicated cases.

The w3schools site has a good summary.

Arithmetic Operators#

Common: +, -, *, /

Other operators:

floor division //

returns the largest integer less than or equal to the result

5 // 2
2
-5 // 2
-3

modulus %

returns the remainder after a division operation

5 % 2
1
print(5.5 / 2) 
print(5.5 // 2)
print(5.5 % 2)
2.75
2.0
1.5

exponentiation **

5**2
25

Comparison Operators#

comparisons are questions and return boolean values

equality ==

# numeric
25 == 4**2 + 9
True

inequality !=

# numeric
25 != 20 + 6
True

greater than >, less than <

20 > 10
True
2*10 >= 20
True
3%2 < 5
True
-3 <= 0
True

Logical Operators#

conjunctions and, or, not

Note that we group comparisons with parentheses

x = 10

(x % 10 == 0) or (x < -1)
True
(x % 10 == 0) and (x < -1)
False
not x == 5
True

Identity Operators#

Identity is

The is keyword is used to test if two variables refer to the same object in the memory. In other words, it tests if the IDs of two objects are the same.

The test returns True if the two objects are the same object.

The test returns False if they are not the same object, even if the two objects are 100% equal.

Use the == operator to test if two variables are equal.

– from W3Schools on Identity Operators

# This will give a True
x = 10
y = 10

print(x is y)
True
# However, this will not
x = 300
y = 300

print(x is y)
False

Unary Operators#

Python offers a short-cut for most operators. When updating a variable with an operation to that variable, such as:

my_var = my_var + 1  # Incrementing

You can do this:

my_var += 1

Python supports many operators this way. Here are some:

a -= a
a \= a
a \\= a
a %= a
a *= a
a **= a

Expressions#

Variables, literal values, and operators are the building blocks of expressions.

For example, the following combines three operators and four variables:

1 + 2 * 3 / 2
4.0

Python employs operator precedence when evaluating expressions:

P – Parentheses
E – Exponentiation
M – Multiplication
D – Division
A – Addition
S – Subtraction
(1 + 2) * (3 / 2)
4.5

Variables and literal values can be combined:

y = 5
m = 2.5
b = 10
y = m * 10 + b
y
35.0
y = m * 5 + b
y
22.5

Expresssions can be very complex.

Expressions evaluate to a value, just as single variables do.

Therefore, they can be put anywhere a value is accepted.

int((y + 10) ** 8)
1244706300354

Practice exercises#

Exercise 6

Create two variables, x and y, and assign them the values 15 and 4, respectively. Then, calculate and print the result from each of the following steps:

1 - The sum of x and y.
2 - The product of x and y.
3 - The result of x divided by y using integer division.
4 - The remainder when x is divided by y.
5 - A boolean expression that checks if x is greater than y.
6 - A boolean expression that checks if x is equal to y.

# Your answers here

Exercise 7

Starting with a variable z assigned with the value 10, perform the following operations using unary operators. Print the value of z after each step:

1 - Add 5 to z.
2 - Subtract 3 from z.
3 - Multiply x by z.
4 - Divide x by z.

# Your answers here