Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,558 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)