Lesson 6: Operators and Expressions
·Programming

Lesson 6: Operators and Expressions

Master the tools of logic and math in Python, from addition and subtraction to complex comparisons.

Lesson 6: Operators and Expressions

What makes a computer "smart" is its ability to perform calculations at lightning speed. To do this, we use Operators. In this lesson, we will learn how to do basic math, compare values, and combine logical statements.

Lesson Overview

In this lesson, we will cover:

  • Arithmetic Operators: Addition, subtraction, multiplication, and more.
  • Comparison Operators: Checking if things are equal, greater, or smaller.
  • Logical Operators: Using and, or, and not to combine logic.
  • Operator Precedence: Understanding which operations happen first.

1. Arithmetic Operators (The Math Stuff)

You already know most of these from school, but Python has a few special ones.

OperatorNameExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
//Floor Division5 // 22 (rounds down)
%Modulo5 % 21 (the remainder)
**Exponent5 ** 225 (5 squared)

Pro Tip: The Modulo (%) operator is extremely useful for checking if a number is even or odd. If number % 2 == 0, it’s even!


2. Comparison Operators (The Decision Stuff)

These operators compare two values and always return a Boolean (True or False).

OperatorMeaningExample
==Equal to5 == 5 (True)
!=Not equal to5 != 3 (True)
>Greater than5 > 10 (False)
<Less than5 < 10 (True)
>=Greater or equal5 >= 5 (True)
<=Less or equal4 <= 5 (True)

Warning: Beginners often confuse = (assigning a value) with == (checking if values are equal). Be careful!


3. Logical Operators (The Thinking Stuff)

We use these to combine multiple comparison statements.

  • and: True only if both sides are True.
  • or: True if at least one side is True.
  • not: Reverses the result (True becomes False).
age = 20
has_license = True

# Is the person an adult AND do they have a license?
can_drive = age >= 18 and has_license # Result: True

# Is it either raining OR cold?
is_staying_home = is_raining or is_cold

4. Operator Precedence (BODMAS)

Python follows standard math rules for which operations happen first:

  1. Parentheses () always go first.
  2. Exponents **.
  3. Multiplication/Division *, /, //, %.
  4. Addition/Subtraction +, -.

Practice Exercise: The Score Calculator

Create a file named calculator.py. Your program should:

  1. Ask the user for their score on Test 1.
  2. Ask the user for their score on Test 2.
  3. Ask the user for their score on Test 3.
  4. Calculate the average score (Test1 + Test2 + Test3 divided by 3).
  5. Check if the average is greater than or equal to 70.
  6. Print: "Your average is [Average]. Did you pass? [True/False]"

Quick Knowledge Check

  1. Which operator gives you the remainder of a division?
  2. What is the difference between / and //?
  3. Why do we use == instead of = when comparing two values?
  4. If x = 10 and y = 20, what does x > 5 and y < 15 return?

Key Takeaways

  • Python can do advanced math like exponents and modulo.
  • Comparison operators (==, !=, <, >) help programs make decisions.
  • Logical operators (and, or, not) allow for complex thinking.
  • Use parentheses () to control which parts of a calculation happen first.

What’s Next?

We can calculate and compare data, but our programs still run in a straight line from top to bottom. In Lesson 7, we’ll learn about Conditional Statements—telling our code how to take different paths based on the result of these comparisons!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn