Paul's Python CoursePPC

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

, 2 minutes to read

A single = gives a value a name, so speed = 300 makes speed hold 300. A double == asks whether two values are the same and gives back True or False, so speed == 300 checks whether speed already holds 300 and doesn't change it.

What each one does

= is called assignment. Python works out whatever is on the right first, then puts the name on the left onto that value. So the name always goes on the left. Write it the other way round, as "2" = choice, and Python stops with SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

== is a comparison, like < and >. It doesn't store anything. It works out to True or False, and that's the kind of answer an if or a while needs to decide what to do.

The example sets choice, compares it with two different strings, then uses the comparison in an if. Run it, then change line 1 to choice = "1" and run it again. The True and False swap round, and nothing is printed under the if. After that, try changing the == on line 5 to a single = and see what Python says.

Run it

main.py
choice = "2"
print(choice == "1")
print(choice == "2")

if choice == "2":
    print("Knots to km/h")

Output

Press Run and what your program prints shows up here.

Using = in an if

A single = in an if or while test is the most common slip, and Python catches it before running anything. If you tried changing line 5 in the example, this is what you saw.

  File "main.py", line 5
    if choice = "2":
       ^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

The suggestion is right, and == is the one you want. The := it mentions is a different operator that you can leave for much later.

Using == where you meant =

The other way round is quieter, because it isn't an error at all.

total = 50
total == 0
print(total)

You might expect this to reset the total, but it prints 50. Line 2 asks whether total is 0, gets False, and does nothing with the answer. If the variable doesn't exist yet, you get NameError: name 'total' is not defined instead, since you can't compare something that has no value.

One more thing to watch with ==. It compares the values exactly, including their type. An answer from input() is always text, so if someone types 2, then choice == 2 is False and choice == "2" is True. That's why menu choices are usually compared with strings in quotes.

A small exercise

This program has one of each mistake. Fix it so it prints Hold on to your hat.

knots = 25
fast == knots > 20
if fast = True:
    print("Hold on to your hat.")
Show an answer
knots = 25
fast = knots > 20
if fast == True:
    print("Hold on to your hat.")

Line 2 should store the answer, so it needs =, and line 3 should ask a question, so it needs ==. Python reports the line 3 mistake first, because it checks for syntax errors before running anything. Once that's fixed, line 2 stops with a NameError, because fast has never been given a value. Since fast is already True or False, you can also write the test as just if fast:.

You'll use both on almost every line of the Unit Converter project, where Neil's menu stores the choice with = and checks it with ==. The Handbook has more on comparisons and on variables.

The project that goes with it

Unit Converter is a free project in Act 1, for Neil. It runs in your browser with nothing to install, and you don't need an account for it.

More guides