Variables#

Types, objects, classes#

  • In Python everything is an object

  • Objects are instances of classes (e.g., int is a class, 42 is an object of this class)

  • type(x) returns the class of x

print(type(1), type(3.14), type(False), type("word"), type(None))  
<class 'int'> <class 'float'> <class 'bool'> <class 'str'> <class 'NoneType'>
print(type([1, 2, 3]), type((1, 3)), type({2, 3}), type({"a": 1, "b": 2}))
<class 'list'> <class 'tuple'> <class 'set'> <class 'dict'>
# Note that jupyter suppresses 'class' without print(...)
type(1 + 10j)
complex
# Which type will it print?
type({}), len({}), type(set()), len(set())
(dict, 0, set, 0)

Variables and Assignments#

  • A variable is just a reference to an object

  • Use id(var) to get the id of the object var refers to

  • type(var) returns the class of that object

  • a is b == True iff a and b refer to the same object

a = 42
id(a), type(a), type(a) is int 
(4541138448, int, True)
b = 43
a == b, type(a) == type(b), id(a) == id(b), a is b
(False, True, False, False)
id(a), id(b), id(42)
(4541138448, 4541138480, 4541138448)
a = 420
b = 420
a == b, id(a) == id(b), a is b
(True, False, False)

Integer Cashing#

# Looks strange, em?
a = 42
b = 42
c = 42
a == b, id(a) == id(b), a is b
(True, True, True)

The integers between \(-5\) and \(256\) are so common that they are cached internally, i.e. these objects already exist when you run Python

for i in range(-6, 260):
    j = i + 1 - 1
    print(i, i is j)
-6 False
-5 True
-4 True
-3 True
-2 True
-1 True
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
10 True
11 True
12 True
13 True
14 True
15 True
16 True
17 True
18 True
19 True
20 True
21 True
22 True
23 True
24 True
25 True
26 True
27 True
28 True
29 True
30 True
31 True
32 True
33 True
34 True
35 True
36 True
37 True
38 True
39 True
40 True
41 True
42 True
43 True
44 True
45 True
46 True
47 True
48 True
49 True
50 True
51 True
52 True
53 True
54 True
55 True
56 True
57 True
58 True
59 True
60 True
61 True
62 True
63 True
64 True
65 True
66 True
67 True
68 True
69 True
70 True
71 True
72 True
73 True
74 True
75 True
76 True
77 True
78 True
79 True
80 True
81 True
82 True
83 True
84 True
85 True
86 True
87 True
88 True
89 True
90 True
91 True
92 True
93 True
94 True
95 True
96 True
97 True
98 True
99 True
100 True
101 True
102 True
103 True
104 True
105 True
106 True
107 True
108 True
109 True
110 True
111 True
112 True
113 True
114 True
115 True
116 True
117 True
118 True
119 True
120 True
121 True
122 True
123 True
124 True
125 True
126 True
127 True
128 True
129 True
130 True
131 True
132 True
133 True
134 True
135 True
136 True
137 True
138 True
139 True
140 True
141 True
142 True
143 True
144 True
145 True
146 True
147 True
148 True
149 True
150 True
151 True
152 True
153 True
154 True
155 True
156 True
157 True
158 True
159 True
160 True
161 True
162 True
163 True
164 True
165 True
166 True
167 True
168 True
169 True
170 True
171 True
172 True
173 True
174 True
175 True
176 True
177 True
178 True
179 True
180 True
181 True
182 True
183 True
184 True
185 True
186 True
187 True
188 True
189 True
190 True
191 True
192 True
193 True
194 True
195 True
196 True
197 True
198 True
199 True
200 True
201 True
202 True
203 True
204 True
205 True
206 True
207 True
208 True
209 True
210 True
211 True
212 True
213 True
214 True
215 True
216 True
217 True
218 True
219 True
220 True
221 True
222 True
223 True
224 True
225 True
226 True
227 True
228 True
229 True
230 True
231 True
232 True
233 True
234 True
235 True
236 True
237 True
238 True
239 True
240 True
241 True
242 True
243 True
244 True
245 True
246 True
247 True
248 True
249 True
250 True
251 True
252 True
253 True
254 True
255 True
256 True
257 False
258 False
259 False

Assignment just copies the reference#

a = 3.14
b = a
a == b, id(a) == id(b), a is b
(True, True, True)
a *= 2 # a = 2*a
print(a, b, a is b, id(a) == id(b))
6.28 3.14 False False

Tricky float#

# Loss of accuracy
print(float(10**16 - 1) == 10**16)
float(10 ** 16 -1), float(10 ** 16)
True
(1e+16, 1e+16)
a = 1.7976931348623157e+308 + 228
b = 1.7976931348623157e+308 + 777777
a == b, id(a) == id(b)
(True, False)
1.7976931348623157e+308 * 2 == float('inf')
True

Mutable vs Immutable#

  • a mutable object can be modified

  • an immutable object cannot be changed after creation and is actually a constant

Immutable types#

  • int

  • float

  • bool

  • str

  • tuple

Cannot change a string:

s = "hello"
print(s[1])
s[1] = 'a'
e
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [197], in <cell line: 3>()
      1 s = "hello"
      2 print(s[1])
----> 3 s[1] = 'a'

TypeError: 'str' object does not support item assignment
s = "hallo"
print(s)
hallo

The only way to modify a variable of an immutable type — assign it to a new object

a = 12
print(f"a={a}, id(a)={id(a)}")
a = a * 20
print(f"a={a}, id(a)={id(a)}")
a=12, id(a)=4541137488
a=240, id(a)=4541144784

Mutable types#

  • list

  • set

  • dict

l = [1, 2, 3]
print(id(l))
l.append(42)
print(f"l={l}, id(l)={id(l)}")
l[1] = -20
print(f"l={l}, id(l)={id(l)}")
4604482432
l=[1, 2, 3, 42], id(l)=4604482432
l=[1, -20, 3, 42], id(l)=4604482432

Aliasing#

colours = ["red", "blue", "green"]
colours_2 = colours
print(colours, colours_2, id(colours) == id(colours_2), colours is colours_2)
['red', 'blue', 'green'] ['red', 'blue', 'green'] True True
colours_2[0] = "orange"
print(colours)
print(colours_2)
['orange', 'blue', 'green']
['orange', 'blue', 'green']

How to avoid aliasing?

colours_3 = colours.copy()
colours_3[0] = "black"
print(colours, colours_3)
['orange', 'blue', 'green'] ['black', 'blue', 'green']

Another problem with aliasing

# lists of lists
l1 = 3 * [[1, 2]]
l2 = [[1, 2], [1, 2], [1,2]]
print(l1)
print(l2)
print(type(l1), type(l1[1]))
[[1, 2], [1, 2], [1, 2]]
[[1, 2], [1, 2], [1, 2]]
<class 'list'> <class 'list'>
# What will happen?
l1[1].append(5)
l2[1].append(10)
print(l1)
print(l2)
[[1, 2, 5], [1, 2, 5], [1, 2, 5]]
[[1, 2], [1, 2, 10], [1, 2]]
print(id(l1[0]), id(l1[1]), id(l1[2]))
print(id(l2[0]), id(l2[1]), id(l2[2]))
4605450752 4605450752 4605450752
4606045312 4600907968 4604481216

dict keys must be immutable

d = {(1,2): 3}
d = {[1,2]: 3}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [208], in <cell line: 1>()
----> 1 d = {[1,2]: 3}

TypeError: unhashable type: 'list'
d = {{1,2}: 3}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [209], in <cell line: 1>()
----> 1 d = {{1,2}: 3}

TypeError: unhashable type: 'set'

copy and deepcopy#

from copy import copy

a = [1, 2, 3]
b = copy(a)
b[0] = 4

a, b
([1, 2, 3], [4, 2, 3])
from copy import copy
a = [1, 2, []]
b = copy(a)
b[2].append(3)
a, b
([1, 2, [3]], [1, 2, [3]])
from copy import deepcopy
a = [1, 500, []]
b = deepcopy(a)
b[2].append(3)
a, b
([1, 500, []], [1, 500, [3]])

Comprehensions#

list comprehension#

lst = []
for i in range(5):
    lst.append(i**2)
lst
[0, 1, 4, 9, 16]
lst = [i ** 2 for i in range(5)]
lst
[0, 1, 4, 9, 16]

dict comprehension#

dct = {}
for i in range(5):
    dct[i] = i**2
dct
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
dct = {i : i**2 for i in range(5)}
dct

set comprehension#

st = set()

for i in range(10):
    st.add(i)

st
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
st = {i for i in range(10)}
st
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

comprehension with condition#

lst = [i ** 2 for i in range(10)]
lst
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
lst = [i ** 2 for i in range(10) if i % 2 == 1]
lst
[1, 9, 25, 49, 81]
lst = [i ** 2 if i > 2 else i for i in range(10)]
lst
[0, 1, 2, 9, 16, 25, 36, 49, 64, 81]

Counter#

dct = {}
for ch in "aaabbbbccd":
    if ch not in dct:
        dct[ch] = 0
    dct[ch] += 1
dct
{'a': 3, 'b': 4, 'c': 2, 'd': 1}
from collections import Counter

Counter("aaabbbbccd")
Counter({'a': 3, 'b': 4, 'c': 2, 'd': 1})
# d1 - d2
d1 = {'a': 3, 'b': 4, 'c': 2, 'd': 1}
d2 = {'a': 4, 'b': 1}
d3 = {}

for k in d1:
    if k in d2:
        if (value := d1[k] - d2[k]) > 0:
            d3[k] = value
    else:
        d3[k] = d1[k]

d3
{'b': 3, 'c': 2, 'd': 1}
from collections import Counter

a = Counter("aaabbbbccd")
b = Counter("aaaab")
a - b
Counter({'b': 3, 'c': 2, 'd': 1})
saved_pwd = "right_password"
pwd = input("Введите пароль для входа: ")
while pwd != saved_pwd:
    pwd = input("Введите пароль для входа: ")
print("Пароль верный. Вход разрешён.")
Введите пароль для входа: 12
Введите пароль для входа: right_password
Пароль верный. Вход разрешён.