dev.rean.me
python

10 How to Use IN and IS operator in Python + Tips (Video in Khmer)

Learn Python in, not in, is and is not operators with list, string, tuple and None. Simple example and tips. Good for beginner!

2026-10-22 ยท 4 min read

Share:

Hello friend! ๐Ÿ‘‹ Python have 2 special operator: in and is. They look easy, but many beginner mix them.

In this post, I show you IN and IS operator in Python. Easy and short. ๐Ÿ


๐Ÿ” IN Operator

in check if item exist inside something. Result is True or False. Use not in for opposite.

With List

fruits = ["apple", "banana", "orange"]

print("apple" in fruits)       # True
print("mango" in fruits)       # False
print("mango" not in fruits)   # True

With String

Check if text exist inside text.

text = "I love Python"

print("love" in text)      # True
print("Python" in text)    # True
print("python" in text)    # False  (big and small letter different)

With Tuple

Tuple is like list, but cannot change. Use ( ).

colors = ("red", "green", "blue")

print("red" in colors)     # True
print("pink" in colors)    # False

With Dictionary

in check the key, not the value.

user = {"name": "Dara", "age": 20}

print("name" in user)      # True
print("Dara" in user)      # False
print("Dara" in user.values())  # True

Use with if

role = "admin"

if role in ("admin", "editor"):
    print("Can edit")
else:
    print("Read only")

๐ŸŽฅ Video: Using IN Operator (List, String, Tuple) in Python (Khmer)


๐Ÿ†” IS Operator

is check if two variable are the same object in memory. Use is not for opposite.

== check same value. is check same object. Not same thing!

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)    # True   same value
print(a is b)    # False  different object (2 box)
print(a is c)    # True   same object (1 box, 2 name)

Think like this: a and b are two paper with same words. a and c are same paper.

Proof with id()

print(id(a))   # 140234...
print(id(b))   # 140235...  (different)
print(id(c))   # 140234...  (same as a)

Same id = same object = is give True.

Best Use: Check None

None mean "no value". Always use is with None.

result = None

if result is None:
    print("No data yet")

if result is not None:
    print("Have data")

Same for True and False singleton check:

flag = True
print(flag is True)   # True

๐ŸŽฅ Video: Using IS Operators in Python (Khmer)


๐Ÿ“Š IN vs IS vs ==

OperatorQuestionExample
inItem exist inside?"a" in "cat" โ†’ True
==Same value?[1] == [1] โ†’ True
isSame object?[1] is [1] โ†’ False

โŒ Common Error

Use is to compare value:

name = "Dara"
if name is "Dara":     # SyntaxWarning, not reliable
    print("Hi")

Fix: use ==

if name == "Dara":
    print("Hi")

Use == None:

if result == None:     # work, but not best
    pass

Fix: if result is None:

Think in check value in dictionary:

user = {"name": "Dara"}
print("Dara" in user)           # False
print("Dara" in user.values())  # True

Use in on number:

print(2 in 123)    # TypeError: argument of type 'int' is not iterable

Fix: print("2" in str(123))


๐Ÿ’ก Tips

  • Use in to check before remove() from list. No error!
  • Use in with tuple or set for many or: if x in (1, 2, 3): is cleaner than x == 1 or x == 2 or x == 3.
  • in on string check part of text. in on list check whole item: "py" in "python" โ†’ True, "py" in ["python"] โ†’ False
  • Use == for value. Use is for None only (and True / False).
  • Don't use is with number or string. Result may surprise you.
  • Use is not None โ€” better than not x is None.
  • Use id() if you want see if 2 variable are same object.
  • Big list? in is slow. Use set for fast check.

โ“ Any Question?

Tell us in comment below! ๐Ÿ‘‡

#Python #InOperator #IsOperator #None #LearnPython #Beginner


๐Ÿ“š Related Post

โ† Back to python
Share: