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

Asked

Viewed 162 times

6

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

4 answers

5

The in is an operator that hides a lot of complexity. It will result in true if the first operated 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")

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

He would be semantically equivalent to something like this:

for char in "abc"
    if "b" == char
        return true
return false

The in is also used as the construction of a loop.

  • The IN Operator Works with Integers ?

  • In a list of integers, yes.

  • Obrigado◕‿◕ ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

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.

Already in the ==, is, for example, a comparison, that if A equals to B will be true.

2


"in" means "this contained" that is, it can be used to verify whether a value is contained within a set of values. Example:

list = ['ball', 10, 'shoe', 'sweet'] if('shoe' in list): print('Ball contained in list')

Already "==" means equal, and can be used to verify 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 on the site.

  • Great Response.

-3

The IN would be a kind of list, right?

Example:

select * from client c Where c.codigo in (1,5,10)

It will only bring customers who have the same codes as the rule set in IN.

Already the == varies of language, in Dart for example:

'==' - We use when we check if one variable is the same as another.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.