Python Python Basics Types and Branching If, Else and Elif

Boolean keywords

What is the difference in functionality between 'in' and == ?

1 Answer

andren
andren
28,336 Points

The == operator checks if two things are the same. So if you wanted to know if two strings matched each other exactly for example you would use ==.

The in operator checks if one thing is inside another. So if you wanted to see if the content of something like a string was within another string or a list of strings for example you would use in.

Here are some example:

# Here I create a list and two strings for the demonstration 
names = ["John", "Lucy", "Alex"]
name1 = "John"
name2 = "John Smith"

# This will print False because "John" and "John Smith" are not the same
print(name1 == name2)

# This will print True because "John" exists within the name "John Smith"
print(name1 in name2)

# This will print False because "John" is not a list of strings
print(name1 == names)

# This will print True because "John" is one of the string in the names list
print(name1 in names)