Skip to content

Commit 42e40c0

Browse files
committed
exaples with dictionary
1 parent 5f7677c commit 42e40c0

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

example_2.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# tuple packing
2+
from collections import OrderedDict
23

34
x = 1, 2, 3, 4, 5
45
print(type(x), x)
@@ -7,7 +8,7 @@
78

89
t = (1, 2)
910
t_1 = (1,)
10-
t_2 = tuple()
11+
t_2: tuple[int, ...] = tuple()
1112
t_3 = 1, 2, 3 # tuple packing
1213

1314
def magic():
@@ -27,9 +28,33 @@ def magic():
2728

2829
print(rest, rest_1, rest_2)
2930

30-
x = 0
31-
y = 1
31+
x2: int = 0
32+
y2: int = 1
3233

3334
# left side tuple unpacking = right side tuple packing
3435
# TODO ---- L - tuple unpacking | R - tuple packing
35-
x, y = y, x
36+
# TODO tuple unpacking works with every variable that has !! Sequence protocol !!
37+
38+
x2, y2 = y2, x2
39+
40+
t_6 = (1, 2, [1, 2])
41+
42+
# TODO set must include hashable elements if tuple includes any kind of non hashable ,
43+
# we can't create set on the base that tuple , set can takes iterable and create itself ,
44+
# means __iter__ that returns object serves __iter__ and __next__
45+
46+
47+
48+
# x_6 = {1, 2, t_6}
49+
50+
x_7: set = {1, 2, 3} | {1, 4, 5}
51+
print(x_7)
52+
53+
# TODO from python 3.6+ default dict is ordered
54+
55+
y_7 = dict(q=1, z=2)
56+
y_8 = dict([(1, 2), (3, 4), (5, 6)])
57+
print(y_7, y_8, sep='\n')
58+
59+
x_8 = OrderedDict([(1, 2), (3, 4), (5, 6)])
60+
print(y_8.get(999, 42))

0 commit comments

Comments
 (0)