Skip to content

Commit 31d347e

Browse files
committed
Add identity operators example
1 parent 732c56b commit 31d347e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Identity Operators
2+
3+
message_1 = 'hello'
4+
message_2 = 'hello'
5+
6+
print(message_1 is message_2) # True
7+
print(message_1 is not message_2) # False
8+
9+
message_3 = 'world'
10+
message_4 = 'tahiti'
11+
12+
print(message_3 is message_4) # False
13+
print(message_3 is not message_4) # True
14+
15+
# Identity vs Equality
16+
17+
# primitive values, if duplicated,
18+
# are stored in the same location
19+
x = 5
20+
y = 5
21+
if x is y:
22+
print('x is y')
23+
else:
24+
print('x is not y')
25+
26+
# lists are objects,
27+
# so they are located in different
28+
# locations in memory
29+
a = [1, 2, 3]
30+
b = [1, 2, 3]
31+
32+
# use the identity operator
33+
# to test if the 2 variables
34+
# reference the same location
35+
# in memory
36+
if a is b:
37+
print('a is b')
38+
else:
39+
print('a is not b')
40+
41+
# use the equality operator
42+
# to test if content is equivalent
43+
if a == b:
44+
print('a == b')
45+
else:
46+
print('a != b')

0 commit comments

Comments
 (0)