Assignment vs. Equality
Concepts covered: Variables
A single = is a statement: it takes the object produced by the expression on its right and binds the name on its left to it. It produces no value of its own, which is why = can never sit inside a larger expression. A double == is an operator: it compares the objects on both sides and produces a bool, which is either True or False. That result is an ordinary value. You can print it or store it, and another operator can take it as input.
| Line | Kind | What happens | Value produced |
|---|---|---|---|
| count = 5 | statement | binds count to the object 5 | none |
| count == 5 | expression | compares count with 5 | True |
| count == 6 | expression | compares count with 6 | False |
| same = count == 5 | statement | evaluates the comparison, then binds same to the result | none |
Read = aloud as "is set to" and == as "is equal to", and most mistakes disappear before they're typed. The run below changes actual twice and compares it after each change. Notice that the comparison on line 3 is evaluated once, when that line runs, and the stored check does not update when actual changes later.
expected = 3
actual = 3
check = actual == expected
print(check)
actual = 4
print(actual == expected)
print(check)
actual = expected
print(actual == expected)True False True True
Line 7 prints True even though actual is 4 by then, because check refers to the bool object built on line 3, and nothing has rebound it. Line 8 makes actual point at the very object expected points at, so the final comparison is True again.
Equality looks at values, and at types
== asks whether 2 objects represent the same value. Numbers compare by their numeric value, so an int can equal a float. True equals 1 as well, because a bool is an integer underneath. A string never equals a number, even when the digits match, because the text "10" and the integer 10 are different kinds of object, and == between them reports False rather than failing.
score = 10
print(score == 10)
print(score == 11)
print(score == 10.0)
print(score == "10")
print(True == 1)Output:
True False True False True
That fourth line is the one that bites in data work. A value read from a CSV column arrives as text, and comparing it with a number is legal and always False, so a check that looks correct passes nothing. The later lesson on type conversions shows how to turn the text into a number before comparing.
What the left side of = may be
The left side of an assignment names a place to attach the object. A literal like 5 or an expression like score + 1 is not a place, so Python rejects the whole program before running a single line.
| Line | What Python reports |
|---|---|
| 5 = score | SyntaxError: cannot assign to literal |
| score + 1 = 11 | SyntaxError: cannot assign to operator |
| score + 1 == 11 | accepted: a comparison, True when score is 10 |
Storing the answer to a question
Because == produces a value, a comparison can be stored under a name and reused. Name the stored bool as a question so the later line reads naturally, and remember that it records the answer at the moment it was computed.
seats = 180
booked = 180
is_full = booked == seats
print("Full flight:", is_full)
booked = 179
print("Full flight:", is_full)
print("Full flight:", booked == seats)Output:
Full flight: True Full flight: True Full flight: False
What does this program print?
limit = 100
usage = 100
over = usage == limit
usage = 150
print(over)- True
- False
- 150
- The program fails on line 3
Before writing a line, decide what it is for: changing what a name points at, or producing an answer. Storing an answer uses = at the left with == on its right side. Rebinding a name uses a single = alone. == compares numeric value, so it answers True when an int meets a float that holds the same number.
About this lesson
This section is part of the Python Expressions: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.