In this week at school, our discussion assignment asked us to post about the difference between ‘=’ and ‘==’ in Python. I put a pretty decent amount of effort in this assignment, so I thought I’d share it here.
Difference Between ‘=’ And ‘==’ In Programming
The short answer:
‘=’ is an assignment operator. It is used in programming when we want to assign a value to a variable. It does not mean ‘equal to’. Instead it’s really saying ‘take the value or expression on my right, and store it in the variable on my left.’
The ‘==’ is a comparison operator. It is used in programming to compare two values against each other. It returns a value of either true or false. It is used to make decisions in a program based on whether two values, or expressions are equal or not.
The super overkill answer:
Understanding the difference between ‘=’ and ‘==’ is probably one of the first major hurdles beginner programmers have to face. It directly relates to the understanding of how variables are created, how they are changed, and is necessary in understanding the flow of a program. To some, this difference is understood right away, but to those who don’t make sense of it, I think it comes from a misunderstanding of what the ‘=’ operator means in programming.
In math class we are taught that ‘=’ means ‘is equal to’.
Examples:
5 = 5 # 5 is equal to 5 2 + 3 = 3 + 2 # 2 + 3 is equal to 3 + 2 2 * 2 * 4 = 4 * 2 * 2 # 2 * 2 * 4 is equal to 4 * 2 * 2 x + 4 = 5 # x + 4 is equal to 5
In these examples the ‘=’ sign is being used as a comparison operator, sometimes called a relational operator.
In programming however, the ‘=’ sign is treated much differently as seen in the example below.
x = 1 x = x + 1 x = x + x print(x)
If your a mathematician you might be ripping your hair out, planes are falling from the sky, and high rises are toppling over because in math a variable can not be equal to more than one value or else grotesque pieces of matter hits the fan. How can x be equal to both 1, and itself plus 1? – It can’t. However, if you’re a programmer you can clearly see that this program simply prints the number 4 to the screen. No need for self induced balding.
This is because the ‘=’ sign in Python and many other languages is really called the assignment operator. The ‘=’ sign doesn’t say ‘x is equal to 1′ but rather ‘take 1 and assign it to x‘. Or more generally, ‘take what is on the right of me and assign it to the container (variable) on the left of me’. In this way the ‘=’ sign is really more like an arrow pointing to the left. ( x <– 1 ).
It will always evaluate the right side, and store it in the left. The right side of an assignment operator can be a literal value such as a number or a string (1, 5, 255, ‘my name’). Or it can be an expression like:
someVariable * i + j + (x % 2)
Whatever value is derived after the evaluation is what gets stored or assigned to the variable on the left. In the case of our 2nd line of code earlier, the value of x + 1 gets stored inside of x. This means what ever value x is equal to at the time of the 2nd line’s execution, will get added to 1 and then it replaces the current value of x.
x = 1 x = x + 1
In this case x was previously set to 1, so the expression ‘x + 1′ evaluates to ‘1 + 1′ and then is evaluated to ‘2’. This means after this line is executed the value stored inside of x is now 2.
The ‘==’ Operator
Moving onward we can now probably understand why there is an ‘==’ in programming. Since the ‘=’ sign is actually used as an assignment operator, we need a way to express a comparison between two values. That is where the ‘==’ sign or comparison operator comes to play (Also sometimes referred to as the relational operator).
We can treat the comparison operator the same way we would treat it in mathematics. The only differences are that in programming the comparison operator returns a Boolean value known as true and false, and it can also be used to compare other data types aside from numbers, ie. strings, booleans, or expressions.
Examples:
5 == 5 # compares two numbers: returns true 2 + 3 == 3 + 2 # compares two expressions: returns true 'string' == 'string' # compares two strings: returns true true == true # compares two booleans: returns true false == false # compares two booleans: returns true true == false # compares two booleans: returns false 'string' == 'dog' # compares two strings: returns false 5 == 4 # compares two numbers: returns false 2 + 3 == 2 * 3 # compares two expressions: returns false
Edit: One student in my class mentioned that the ‘==’ operator also checks for type equality. Meaning that 7 as an integer does not equal ‘7’ as a string. Here’s an example.
7 == '7' # returns false 7 == int('7') # returns true
The way we use the comparison operator in languages such as Python is to make decisions in our programs. It turns out that checking to see if two values are equal is a very common thing to do. There is often blocks of code that we want executed only if a condition is true. There is also usually more code that we want executed only if something else is true.
Hence if-elif-else statements usually will require a comparison operator, in order to check for a condition to be true. The only case were you would not use the comparison operator is if the condition of an ‘if’ statement is checking the value of a boolean or a function call that returns a boolean type (true or false).
Here’s an example of the comparison operator used in an ‘if’ statement.
name = input("Enter your name: ")
if (len(name) == 3): # here is our comparison
print(name + ', your name is 3 characters long!')
else:
print(name + ', your name is not 3 characters long :(' )
In this example we grab a string from the user and store it in ‘name’. We then compare the length of the string with the literal value 3. If the length of the string is equal to 3 then the comparison returns true, and the appropriate code is executed. However if the length of the string is not 3, then the comparison returns false, and the else code is executed.
Here’s the output when I enter ‘Dan’ as input.
>>> ================================ RESTART ================================ >>> Enter your name: dan dan, your name is 3 characters long! >>>
Conclusion (finally)
That’s all there is to it! Use the ‘=’ sign when you want to assign a value to a variable, and use the ‘==’ when you want to compare two values to determine if they are equal. Remember that the ‘=’ does not mean ‘is equal to’ and you should be well on your way to becoming a coding master!
I hope you enjoyed this explanation of the difference between ‘=’ and ‘==’. If you have any suggestions, questions or additions for this post please let me know in the comments! I will be glad to add your contribution, or answer any questions you may have.
Thanks for reading.
Cheers,
Dan