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

preview_player
Показать описание
🎓 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:

#Python #Tutorial #Beginners #Shorts

***

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

***

♡ FOLLOW US ♡

♡ SUPPORT US ♡

***
Рекомендации по теме
Комментарии
Автор

Did you know ALL Identity Operators in Python? 🤔

DigitalAcademyOnline
Автор

Best channel ever! Thanks for sharing, 🐍👍

lalajt
Автор

Je suis absolument et définitivement fan !!!

PassionsByCath
Автор

C'est vraiment une super chaine sur Python !

PassionsByCath