What is the difference between the operator IN and ==?

4

I would like to know the difference between the IN and the == operator in Python?

    
asked by anonymous 14.04.2018 / 22:45

3 answers

1

"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.

    
15.04.2018 / 19:34
6

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 .

    
15.04.2018 / 00:32
3

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.

    
14.04.2018 / 23:16