-2

Are there any practical differences between examples below and is better use one than others?

s = 'this@mail.is'

for i in range(len(email)):
  print('@' in email[i])

#vs

for i in range(len(email)):
    print(email[i] == '@')
#or

if '@' in email:
   

2 Answers2

0

in checks for substrings "a" in "apple" > True

== checks if it is exactly the same

b=10;
if(b==10)
0

There is no practical difference... for your example here...
In essence, (as explained here and here), with in, python calls the underlying __contains__ function.

Thus, in your example. It makes no difference.


On the other hand, one could create a module and define a differente behavior for the __contains__ function.
I guess this would be done more for of an academic purpose, but it could still be made.