-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_collections.py
83 lines (71 loc) · 1.7 KB
/
my_collections.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
"""
File learn about collection: Tuples, Strings, Range, List, Dictionaries, Sets
"""
def do_tuples():
"""
Practice tuples
:return: nothing
"""
# Immutable sequence of arbitrary objects
# Use () to define a tuple
t = ("Ogden", 1.99, 2)
print(t, type(t))
print("Size ", len(t))
print("One member:", t[0]) # by index notation
# To iterate over a tuple
for item in t:
print(item)
# Single tuples, must end with comma
t1 = (6,)
print(t1, type(t1))
# Another way to create tuples
# Parenthesis are optional
t2 = 1, 2, 3, 5
print(t2, type(t2))
# Tuple constructor: tuple()
t_from_l = tuple([3, 77, 11])
print(t_from_l, type(t_from_l))
# test for membership
print(5 in (3, 6, 8, 5, 12))
print(5 not in (3, 6, 8, 5, 12))
def min_max(items):
"""
Return the minimum and maximum elements of a collection
:param items: collection
:return: the minimum and maximum
"""
return min(items), max(items)
def swap(obj1, obj2):
"""
Swap value of the objects
:param obj1: first
:param obj2: second
:return: values swapped
"""
return obj2, obj1
def main():
"""
Test function
:return:
"""
do_tuples()
# output = min_max([56, 76, 11, 12, 90])
# print("min", output[0])
# print("max", output[1])
# # Tuple unpacking
# lower, upper = min_max([56, 76, 11, 12, 90])
# print("min", lower)
# print("max", upper)
#
# # Swap values
# a = "jelly"
# b = "bean"
# print(a, b)
# # Call your function
# a, b = swap(a, b)
# print(a, b)
# a, b = b, a
# print(a, b)
if __name__ == '__main__':
main()
exit(0)