I would like to know the difference between the IN
and the ==
operator in Python?
I would like to know the difference between the IN
and the ==
operator in Python?
"in" means "this is contained", ie it can be used to check if a value is contained within a set of values. Example:
list = ['ball', 10, 'shoe', 'sweet'] if ('shoe' in list): print ('Ball is contained in list')
Since "==" means equal, and can be used to check if one value is equal to another. Example:
name = 'Paul' if (name == 'Paul): print ('The name is really Paul')
Sorry for the lack of indentation in the code, I'm new to the site.
in
is an operator that hides a lot of complexity. It will be true if the first operand is contained in the second operand, which can be any list, including a string .
print("b" in "abc")
print("b" == "abc")
print("abc" in "abc")
print("abc" == "abc")
See working on ideone .
It would be semantically equivalent to something like this:
for char in "abc"
if "b" == char
return true
return false
in
is also used as a loop build .
Basically, the in
is, for example, A contained in B , that is, there is an element C in B such that A = C is true.