01 / 06
Ask with ==
To find out whether two values are the same, use ==, two = together.
The answer is True or False, the same as with any comparison.
print(10 == 10)print(10 == 5)Result
True False
The basics of syntax
INPUT · Slides
01 / 06
To find out whether two values are the same, use ==, two = together.
The answer is True or False, the same as with any comparison.
print(10 == 10)print(10 == 5)Result
True False
02 / 06
Because = is already taken. It means "put into".
x = 5 … put 5 into xx == 5 … is x the same as 5Completely different jobs, so they needed different symbols. One stroke puts in, two ask.
x = 5print(x == 5)Result
True
03 / 06
Write if x = 5: and Python raises a SyntaxError and stops.
That is actually a kindness. In some languages a stray = will run happily and leave you with a bug you cannot explain. Python stops you at the door.
x = 3if x = 5: print("pass")Result
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
04 / 06
The other way round, != asks whether two things are not the same. ! means "not".
== and != always give opposite answers, so pick whichever reads better.
print(10 != 5)print(10 != 10)Result
True False
05 / 06
== is not only for numbers. On text it checks whether the contents match character for character.
Capitals and small letters count as different, so "Yes" and "yes" do not match.
greeting = "good morning"print(greeting == "good morning")print("Yes" == "yes")Result
True False
06 / 06
Even when they look alike, a different type means a different thing. 10 and "10" do not match.
Values arriving from outside are usually text, so remember to put them through int() before comparing them with a number.
print(10 == "10")print(10 == int("10"))Result
False True