-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtuple.py
More file actions
executable file
·83 lines (57 loc) · 1.29 KB
/
tuple.py
File metadata and controls
executable file
·83 lines (57 loc) · 1.29 KB
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
#!/usr/bin/env python
"""
## tuple
# Immutable list of elements of any type
"""
# Special constructor notation:
t = (1, 2, 3)
t = 1, 2, 3
# For a single element, a trailing comma is needed
# to differentiate from numeric expressions:
assert (1) == 1
assert type((1,)) == tuple
# Also works without parenthesis on some cases:
a = 1,
# Global factory method from list:
assert tuple([1, 2, 3]) == (1, 2, 3)
# Doest not exist:
#t = tuple(1, 2, 3)
t2 = (4, 5, 6)
t3 = (4, 5, 1)
tb = (False, False, True)
tm = (1, 1.1, True, 'asdf')
# Index access:
t = (1, 2, 3)
assert t[0] == 1
assert t[1] == 2
assert t[2] == 3
# Unpack:
a, b, c = (1, 2, 3)
assert a == 1
assert b == 2
assert c == 3
if 'tuples are immutable':
t = (0)
try:
t[0] = 'a'
except TypeError:
pass
else:
assert False
# Concatenate:
assert (0, 1) + (2, 3) == (0, 1, 2, 3)
t = (0, 1)
assert t * 2 == (0, 1, 0, 1)
assert 2 * t == (0, 1, 0, 1)
# Compare: does alphabetical like compare from left to right.
assert (0, 1) == (0, 1)
assert (0, 1) < (1, 2)
assert (0, 10) < (1, 1)
# TODO why:
#assert (0, 1) > (1)
# The list global functions also work on tuples:
assert len((0,1)) == 2
assert max((0,1)) == 1
assert min((0,1)) == 0
assert any((True, False)) == True
assert all((True, False)) == False