1
1
# tuple packing
2
+ from collections import OrderedDict
2
3
3
4
x = 1 , 2 , 3 , 4 , 5
4
5
print (type (x ), x )
7
8
8
9
t = (1 , 2 )
9
10
t_1 = (1 ,)
10
- t_2 = tuple ()
11
+ t_2 : tuple [ int , ...] = tuple ()
11
12
t_3 = 1 , 2 , 3 # tuple packing
12
13
13
14
def magic ():
@@ -27,9 +28,33 @@ def magic():
27
28
28
29
print (rest , rest_1 , rest_2 )
29
30
30
- x = 0
31
- y = 1
31
+ x2 : int = 0
32
+ y2 : int = 1
32
33
33
34
# left side tuple unpacking = right side tuple packing
34
35
# 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