What do the symbol “=” and “==” mean in python?

== is a comparison operator while = will assign a value to said variable.

You can use == to see whether any two items as long they are the same type are equivalent:

if a == 2: # Compares whether a is equal to 2
    print a

Now here’s the thing. If you are comparing any two items like these, an error will pop up:

  • String with integer

  • Integer with string

  • String and float

  • Float and string

Floats and integers are comparable as they are numbers but are usually not equal to each other except when the float is basically the integer but with .0 added to the end. When using ==, if the two items are the same, it will return True. Otherwise, it will return False.

You can use = to assign values to variables. Using == will either do nothing or throw an error (if the variable is undefined). For example, you wanted the variable hi to have the value of 2. Then use the =:

hi = 2

Now hi is equal to 2. You can combine = with operations like + and - assuming the variable is an integer or float:

hi += 1
hi -= 1

Now by using += or -= like above, the variable must already be defined as these operators will directly change the value of the variable. Basically, they are like this:

hi += 1 # is the same as hi = hi + 1
hi -= 1 # is the same as hi = hi - 1

So in conclusion, they are different as:

  • == is a comparison operator: returns True is the two items are equal, returns False if not, throws error if used to assign variable before definition and if the two items are not compatible

  • = is an assignment operator: will assign values like strings or numbers to variables. Can be used in forms like += when variable’s value is a number and is already defined.

The only way they can be used the same time is that they can be used in strings:

"hi = hello"
"2 == 3 probably returns False don't you think?" 

Leave a Comment