-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit2_lesson_03_understanding_identity_and_equality.py
93 lines (72 loc) · 2.31 KB
/
unit2_lesson_03_understanding_identity_and_equality.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
__author__ = 'Kalyan'
from placeholders import *
notes = '''
Identity and equality are 2 concepts which most beginners are confused about.
The 'is' operator is used to test identity and == is used to test equality.
Two objects are identical if they are the same object
Two objects can be equal even if they are not the same object, if they are of the same type and the type defines some
equality semantics. E.g. all string objects with content "abc" are equal irrespective of where the objects are in memory,
two lists can be equal if all elements in them are equal in same order etc.
Write sample code in the python visualizer to see and understand this topic better.
http://pythontutor.com/visualize.html
'''
def test_identity_equality_lists():
a = []
b = []
assert False == (a is b)
assert True == (a == b)
a.append("one")
assert False == (a is b)
assert False == (a == b)
c = []
d = c
assert True == (c is d)
assert True == (c == d)
c.append("one")
assert True == (c is d)
assert True == (c == d)
def test_identity_equality_string():
a = b = "hello"
assert True == (a is b)
assert True == (a == b)
c = "hello"
d = "".join(["hel", "lo"])
assert False == (c is d)
assert True == (c == d)
def test_identity_equality_numbers():
a = b = 10000
assert True == (a is b)
assert True == (a == b)
c = 10000
d = int("10000")
assert False == (c is d)
assert True == (c == d)
def test_identity_equality_small_numbers():
"""
why do small numbers behave differently? google and find out!
"""
a = b = 10
assert True == (a is b)
assert True == ( a == b)
c = 10
d = int("10")
assert True == (c is d)
assert True == (c == d)
def test_identity_equality_None():
a = b = None
assert True == (a is b)
assert True == (a == b)
a = None
b = None
assert True == (a is b)
assert True == (a == b)
notes_2 = '''
None is a builtin constant as you can see above. This allows you to write more
readable code like if x is None: instead of if x == None:
Read up http://effbot.org/zone/python-objects.htm for a good understanding of python objects.
'''
three_things_i_learnt = """
-
-
-
"""