-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtuples.py
More file actions
111 lines (77 loc) · 2.52 KB
/
tuples.py
File metadata and controls
111 lines (77 loc) · 2.52 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
Two following tuples are given:
dji1 = ('AAPL.US', 'IBM.US', 'MSFT.US')
dji2 = ('HD.US', 'GS.US', 'NKE.US')
Combine these tuples into one as shown below and print result to console
('AAPL.US', 'IBM.US', 'MSFT.US', 'HD.US', 'GS.US', 'NKE.US')
"""
from itertools import count
dji1 = ('AAPL.US', 'IBM.US', 'MSFT.US')
dji2 = ('HD.US', 'GS.US', 'NKE.US')
result = dji1 + dji2
print(result)
"""
The following tuples are given:
dji1 = ('AAPL.US', 'IBM.US', 'MSFT.US')
dji2 = ('HD.US', 'GS.US', 'NKE.US')
Nest these tuples into one tuple as shown below and print the result
to console
Expected result:
(('AAPL.US', 'IBM.US', 'MSFT.US'), ('HD.US', 'GS.US', 'NKE.US'))
"""
dji1 = ('AAPL.US', 'IBM.US', 'MSFT.US')
dji2 = ('HD.US', 'GS.US', 'NKE.US')
result = (dji1, dji2)
print(result)
"""
Tuples are immutable. The following tuple is given:
members = (('Kate', 23), ('Tom', 19))
insert a tuple ('John', 26) between Kate and Tom as shown belo.
Print the result to the console
Tip: You have to create a new tuple
Expected result:
(('Kate', 23), ('John', 26), ('Tom', 19))
"""
members = (('Kate', 23), ('Tom', 19))
members = (members[0], ('John', 26), members[1])
print(members)
"""
The following is given:
default = ('YES', 'NO', 'NO', 'YES', 'NO')
Using the appropriate method return the number of occurrences of the
string 'YES' and print the result to the console as shown below
Number of occurences: 2
"""
default = ('YES', 'NO', 'NO', 'YES', 'NO')
cnt = default.count('YES')
print(f'Number of occurences: {cnt}')
"""
Sort the given tuple (from A to Z)
names = ('Monica', 'Tom', 'John', 'Michael')
Print the sorted tuple to the console as shown below
"""
names = ('Monica', 'Tom', 'John', 'Michael')
result = tuple(sorted(names))
print(result)
"""
The following tuple is given (name,age):
info = (('Monica', 19), ('Tom', 21), ('John', 18))
Sort the tuple:
- ascending by age
- descending by age
And print the result to the console as shown below
"""
info = (('Monica', 19), ('Tom', 21), ('John', 18))
asc = tuple(sorted(info, key=lambda item: item[1]))
desc = tuple(sorted(info, key=lambda item: item[1], reverse=True))
print(f'Ascending: {asc}')
print((f'descending: {desc}'))
"""
The following tuple is given:
stocks = (('Apple Inc', ('AAPL.US', 310)), ('Microsoft Corp', ('MSFT.US', 184)))
Extract a ticker for Apple and print the result to the comsole.
Expected result:
APPL.US
"""
stocks = (('Apple Inc', ('AAPL.US', 310)), ('Microsoft Corp', ('MSFT.US', 184)))
print(stocks[0][1][0])