Reputation: 85
When should I write ==
in Python code, and when should it just say =
? What does ==
actually mean?
Sometimes I get an error message when I try using =
that goes away if I change it to ==
. It seems like ==
can be used in every situation. Is that true? If so, why is there a separate =
, and why do I need to use the same symbol twice?
Upvotes: 2
Views: 72977
Reputation: 3106
==
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?"
Upvotes: 4
Reputation: 3891
The simple answer is =
is an assignment operator, ==
is a comparison operator. And you are wrong in saying that ==
can be used in any situation when =
works. For example if I wanted to create the variable my_string
and set it equal to "something"
I would use the =
operator.
my_string = "something"
I am assigning the variable to an object using that operator.
If I want to compare two strings (or other data types) like this:
if "something" == "nothing":
#perform a function
I am comparing the two strings.
However if I try to assign a variable with the ==
operator for the initial assignment of the variable it will not work. If it is already assigned it will work but it will not do what you expect. For example:
my_string == "something"
Will raise an error a Name Error.
Likewise if you try to compare two strings (or other data types) with the =
operator like this:
if "something" = "nothing":
#perform a function
This will raise a Syntax Error.
I hope this helps you understand this concept. Happy programming!
Upvotes: 3
Reputation: 30813
=
is assignment operator, it is used to assign something to a variable:
A = 67 #this is called assignment. With this, A has a value of 67
==
is comparison operator, it is used to compare an item to another item to see if they are equal. It results in true or false
A == 65 #Is A equal to 65? This does NOT assign A to 65. If A was previously assigned as 67, it still retains its 67 value
Upvotes: 3
Reputation: 7941
As MarkyPython already said. Assignment means you use the =
to assign the value on the right side to a variable a
on the left side. a=10
means that a
is equal to 10
from here on.
The expression a==10
tests if a variable a
is equal to 10. The outcome of such a test is a Boolean (True or False). Such tests are typically used in if...then
decisions.
a=10 # a is assigned the value of 10, and per duck-typing the type integer
print a # --> 10
print type(a) # --> int
print a==10 # --> True
print a==11 # --> False
if a==10: # if the condition is True
do something
else:
do something else
Upvotes: 1
Reputation: 20346
The difference is that name = value
is telling Python that name
is now equal to value
. name == value
, on the other hand, is asking Python if name
is equal to value
. There are places where you can't tell Python what name
is equal to, but you can ask. For example, if you want to print it:
>>> x = 4
>>> print x = 4
File "<stdin>", line 1
print x = 4
^
SyntaxError: invalid syntax
That is because we are printing something, but x = 4
has no value. It is x
that has the value, so we want to know if x
is equal to 4
; we don't want to tell Python that it is. In that case, you need double =
:
>>> x = 4
>>> print x == 4
True
In any place that you can use =
, you can use ==
; but it will have a different meaning. For example:
>>> x = 4
>>> print x
4
>>> x == 4
True
x = 4
tells Python that x
is equal to 4
. Nothing else is displayed because it is just a command. x == 4
on the other hand is asking if x
is equal to 4
. When we ask a question, the Python shell will tell us the answer, so it prints True
.
Upvotes: 1