Operators and Expressions
Master arithmetic, comparison, and logical operators
Learning Objectives
- Use arithmetic operators for calculations
- Compare values with comparison operators
- Combine conditions with logical operators
- Understand operator precedence
What are Operators?
Operators are special symbols that perform operations on values (operands). Python supports several types of operators:
Arithmetic Operators
Use these for mathematical calculations:
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 6 * 7 |
42 |
/ |
Division | 15 / 4 |
3.75 |
// |
Floor Division | 15 // 4 |
3 |
% |
Modulus (Remainder) | 17 % 5 |
2 |
** |
Exponentiation | 2 ** 3 |
8 |
Examples in Action
|
|
Division Types Explained
|
|
n % 2 == 0 means even) or for wrapping values.Comparison Operators
These compare values and return True or False:
| Operator | Name | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 7 > 3 |
True |
< |
Less than | 2 < 8 |
True |
>= |
Greater than or equal | 5 >= 5 |
True |
<= |
Less than or equal | 4 <= 3 |
False |
Don’t confuse = (assignment) with == (comparison)!
x = 5assigns the value 5 to xx == 5checks if x equals 5
Examples
|
|
Chained Comparisons
Python allows elegant chained comparisons:
|
|
Logical Operators
Combine multiple conditions:
| Operator | Description | Example | Result |
|---|---|---|---|
and |
Both must be True | True and False |
False |
or |
At least one True | True or False |
True |
not |
Inverts the value | not True |
False |
Truth Tables
Practical Examples
|
|
and, if the first condition is False, it doesn’t check the second. With or, if the first is True, it doesn’t check the second.Assignment Operators
Shorthand for updating variables:
| Operator | Example | Equivalent To |
|---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 4 |
x = x * 4 |
/= |
x /= 2 |
x = x / 2 |
//= |
x //= 3 |
x = x // 3 |
%= |
x %= 2 |
x = x % 2 |
**= |
x **= 2 |
x = x ** 2 |
|
|
Operator Precedence
When multiple operators are in one expression, Python follows this order:
Examples
|
|
(a + b) * c is easier to understand than relying on precedence rules.Practical Example: Calculator
Let’s build a simple calculator:
|
|
Summary
Today you learned:
- Arithmetic operators:
+,-,*,/,//,%,** - Comparison operators:
==,!=,>,<,>=,<= - Logical operators:
and,or,not - Assignment operators:
=,+=,-=,*=,/= - Operator precedence: Parentheses first, then exponents, then multiplication/division, then addition/subtraction
Practice Exercise
Calculate and print:
- The area of a rectangle (width=5, height=3)
- Whether a number is even (hint: use modulus)
- Whether a person can vote (age >= 18) AND is a citizen
- The result of
2 + 3 * 4 - 1and explain why
Next Steps
Tomorrow, we’ll learn about control flow - how to make decisions in your code with if, elif, and else statements.
Complete the quiz below to unlock Day 4!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 3 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!