Identity Operators in Python (IS, IS NOT) - Python Tutorial for Beginners

Опубликовано: 19 Октябрь 2024
на канале: Digital Academy
516
14

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Types of Operators in Python - Identity Operators in Python

Depending upon the type of operations you want to perform, there are 7 types of operators in Python: Arithmetic operators, Assignment operator, Comparison operators, Logical operators, Bitwise operators, Identity operators and Membership operators.

○ Identity Operators in Python - IS, IS NOT

Identity Operators, is and is not, are used to compare two values, or variables: not if they are equal, but if they are actually the same Object, located on the same part of the memory. Hence, the 2 variables that are equal does not necessarily imply that they are identical — with de same identity!

Operator | Meaning | Example
is | True operands are identical (same object) | x is True
is not | True if operands are not identical (not the same object) | x is not True

Here is an example of 2 objects that are equal, but not identical. x and y both refer to objects whose value is 5: they are equal. But they do not reference the same object, as you can verify: x and y do not have the same identity, and "x is y" returns False.

OUTPUT
x = 5 5
y = 4 + 1 5

OUTPUT
x == y True
x is y False

You saw previously that when you make an assignment like "x = y", Python merely creates a second reference, to the same object - and you could confirm that fact with the id() function. You can also confirm it, using the "is" operator.

OUTPUT
id(x) 60307920
id(y) 60307920

In this specific case, since a and b reference the same object, it stands to reason that a and b would be equal, as well.

a = "I am a string"
b = a

OUTPUT
id(a) 55993992
id(b) 55993992

OUTPUT
a is b True
a == b True

Unsurprisingly, the opposite of "is", is "is not":

x = 5
y = 4 + 1

OUTPUT
x is not y True

Let's play this video, stick around and watch until the end of this video! 👍🏻

Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, String...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assi...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF,...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Co...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***