Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple explanation and comparison between Python’s is and in operators with examples to understand their differences in real-world scenarios.

NotificationsYou must be signed in to change notification settings

muhammadwaheedairi/python-is-vs-in-operators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Understanding the difference betweenis andin in Python is really important — especially when you're trying to write clean and bug-free code. In this short guide, I’ve shared my learning and examples that helped me understand how these two operators work differently.


✅ What is theis Operator?

Theis operator checksidentity, not equality.

  • It returnsTrue if two variables refer to thesame object in memory.
  • It’s often used to check if something isNone.
a= [1,2,3]b=ac= [1,2,3]print(aisb)# True – same memory referenceprint(aisc)# False – different objects even if content is same

🔸 Useis when you care aboutobject identity, not just if values match.

A good use case:

x=NoneifxisNone:print("x has no value")

✅ What is thein Operator?

Thein operator checksmembership — it tells you if a value exists inside a list, string, tuple, dictionary, etc.

fruits= ["apple","banana","cherry"]print("banana"infruits)# Trueprint("grape"infruits)# False

It also works with strings:

text="hello world"print("world"intext)# Trueprint("python"intext)# False

🔁 Side-by-Side Comparison

ExpressionDescriptionOutput
a is bTrue ifa andb are same objectTrue
a == bTrue ifa andb have same valueTrue
x in yTrue ifx is inside containeryTrue/False

⚠️ Common Mistakes

  • ❌ Usingis to compare values:

    a=1000b=1000print(aisb)# Might be False even though a == b
  • ✅ Use== when comparing values, and useis for identity:

    print(a==b)# True
  • ✅ Always useis None,not== None.


🧠 Final Thoughts

  • Useis when comparing withNone or checking identity.
  • Usein to check if a value exists inside something.
  • Don't mix them up — Python won’t stop you, but your bugs will get harder to find!

About

A simple explanation and comparison between Python’s is and in operators with examples to understand their differences in real-world scenarios.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp