Vinay Garg Assistant Professor

Lesson 9: Comparison and logical operators

Comparison operators ask a question and answer True or False. Logical operators join those answers with and, or and not. One program shows all of them, including a chained comparison.

A code window showing age = 20 and print(age >= 18 and True)

In short

  • Comparison operators are ==, !=, >, <, >= and <=, and every one returns True or False.
  • `and` needs both sides true, `or` needs one side true, `not` flips the answer.
  • Python allows chained comparisons: `18 <= age < 60` means both tests at once.
  • `=` assigns a value; `==` compares two values. Mixing them up is the most common beginner error.

6

comparison operators: ==, !=, >, <, >=, <=

Python docs

3

logical operators: and, or, not

Python docs

What this program shows

A comparison operator compares two values and gives one of two answers: True or False. That answer is a bool, the same type you met in lesson 1.

A logical operator joins two of those answers into one. The three are and, or and not.

The operators

OperatorRead it asExampleAnswer
==is equal to5 == 5True
!=is not equal to5 != 5False
>is greater than7 > 3True
<is less than7 < 3False
>=is greater than or equal7 >= 7True
<=is less than or equal7 <= 6False

The program

# compare.py — comparisons give True or False, logical operators join them

age = 20
marks = 72
passed = marks >= 40

print(age == 20)               # True
print(age != 20)               # False
print(marks > 90)              # False
print(marks <= 72)             # True
print(18 <= age < 60)          # True: chained, age is in the range
print(passed)                  # True

print(age >= 18 and marks >= 40)   # True: both sides true
print(age >= 18 or marks >= 90)    # True: one side is enough
print(not passed)                  # False: not flips it

print(True and False)   # False
print(True or False)    # True
print(not True)         # False

How it works, step by step

  1. marks >= 40 is a question. Python answers it and hands the answer to passed, so passed holds True.
  2. 18 <= age < 60 is two questions joined by Python for you: is age at least 18, and is age below 60? Both must be true.
  3. and gives True only when both sides are true. If either side is false, the answer is False.
  4. or gives True when at least one side is true. It is only False when both sides are false.
  5. not takes one value and flips it: not True is False.
  6. The last three lines show the same operators working on the words True and False directly, because those are ordinary values.

What it prints

True
False
False
True
True
True
True
True
False
False
True
False

Common mistakes

  • if marks = 40 is an error. One equals sign assigns; two compare.
  • 5 == "5" is False. A number and text are never equal, even when they look the same.
  • and is stricter than or. True or False and False is True, because and runs first. Use brackets when mixing them.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Store your age and check whether you are between 18 and 60 with one chained comparison.
  2. Take marks in three subjects and print True if all three are at least 40.
  3. Print True if a number is either negative or greater than 100.
  4. Login check: a correct password is `"python123"`. Store what the user typed and print whether it matches.

Sources

Frequently asked questions

What is the difference between = and == in Python?

One equals sign assigns a value to a name, as in age = 20. Two equals signs compare two values and return True or False, as in age == 20. Using = inside a condition is a syntax error.

When should I use and instead of or?

Use and when every condition must hold — a student needs at least 40 in all subjects. Use or when one is enough — a number is negative or above 100. Reading the sentence out loud usually tells you which one you mean.

What does a chained comparison like 18 <= age < 60 do?

It checks both parts at once and is true only when age is at least 18 and below 60. It is the same as writing 18 <= age and age < 60, but shorter and easier to read.

Why does 5 == "5" return False?

Because an int and a str are different types. Python does not convert one to the other to make the comparison true. Convert first if that is what you meant: int("5") == 5.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.