Skip to content

Commit caf06ad

Browse files
authored
Create tuples_exercises_python.py
1 parent d35e287 commit caf06ad

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

tuples_exercises_python.py

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
"""
2+
1. Write a Python program to create a tuple.
3+
"""
4+
5+
my_empty_tuple = ()
6+
my_another_empty_tuple = tuple()
7+
my_single_tuple = 'abc',
8+
my_multiple_tuple = 'abc', 'bcd', 'xyz'
9+
my_multiple_tuple_with_braktes = ('abc', 'bcd', 'xyz')
10+
my_list = [1, 2, 3, 3, 4]
11+
my_tuple_from_list = tuple(my_list)
12+
13+
14+
print(my_empty_tuple, type(my_empty_tuple), '\n',
15+
my_another_empty_tuple, type(my_another_empty_tuple), '\n',
16+
my_single_tuple, type(my_single_tuple), '\n',
17+
my_multiple_tuple, type(my_multiple_tuple), '\n',
18+
my_multiple_tuple_with_braktes, type(my_multiple_tuple_with_braktes), '\n',
19+
my_tuple_from_list, type(my_tuple_from_list))
20+
21+
"""
22+
2. Write a Python program to create a tuple with different data types.
23+
"""
24+
25+
my_tuple = ('abc', 1, 4.56, ['a', 'b', 'c'], True)
26+
print(my_tuple)
27+
28+
for i in my_tuple:
29+
print(i, 'type is', type(i))
30+
31+
"""
32+
3. Write a Python program to create a tuple with numbers and print one item.
33+
"""
34+
from random import randint
35+
my_tuple = (1, 2, 3, 3, 4)
36+
print(my_tuple[randint(0, len(my_tuple)-1)])
37+
38+
"""
39+
4. Write a Python program to unpack a tuple in several variables.
40+
"""
41+
my_tuple = ('a', 'b')
42+
var1, var2 = my_tuple
43+
print(var1, var2)
44+
45+
"""
46+
5. Write a Python program to add an item in a tuple.
47+
"""
48+
49+
my_tuple = ('a', 'b')
50+
my_new_tuple = my_tuple + ('c',)
51+
52+
print(my_new_tuple)
53+
54+
"""
55+
6. Write a Python program to convert a tuple to a string.
56+
"""
57+
my_tuple = ('a', 'b')
58+
my_string = ''.join(my_tuple)
59+
print(my_string)
60+
61+
"""
62+
7. Write a Python program to get the 4th element
63+
and 4th element from last of a tuple.
64+
"""
65+
66+
my_tuple = tuple('abcdefgh')
67+
print(my_tuple)
68+
print('4th element is', my_tuple[3])
69+
print('4th to last element is', my_tuple[-4])
70+
71+
"""
72+
8. Write a Python program to create the colon of a tuple.
73+
"""
74+
75+
my_tuple = (':',)
76+
my_colon, = my_tuple
77+
print(my_colon)
78+
79+
"""
80+
9. Write a Python program to find the repeated items of a tuple.
81+
"""
82+
83+
my_tuple = (1, 1, 2, 3, 4, 4, 5)
84+
85+
repeated_items = []
86+
for i in my_tuple:
87+
if my_tuple.count(i) > 1:
88+
repeated_items.append(i)
89+
print(set(repeated_items))
90+
91+
"""
92+
10. Write a Python program to check whether an element exists within a tuple.
93+
"""
94+
95+
my_tuple = (1, 2, 3, 4, 5)
96+
97+
if 2 in my_tuple:
98+
print('2 is in', my_tuple)
99+
else:
100+
print('2 is NOT in', my_tuple)
101+
102+
if 6 in my_tuple:
103+
print('6 is in', my_tuple)
104+
else:
105+
print('6 is NOT in', my_tuple)
106+
107+
"""
108+
11. Write a Python program to convert a list to a tuple.
109+
"""
110+
111+
my_list = [1, 2, 3, 4, 5]
112+
my_tuple = tuple(my_list)
113+
print(my_tuple)
114+
115+
"""
116+
12. Write a Python program to remove an item from a tuple.
117+
"""
118+
119+
my_tuple = (1, 2, 3, 4, 5)
120+
# to remove number 3
121+
my_new_tuple = my_tuple[:my_tuple.index(3)] + my_tuple[my_tuple.index(3)+1:]
122+
print(my_new_tuple)
123+
124+
"""
125+
13. Write a Python program to slice a tuple.
126+
"""
127+
128+
my_tuple = tuple('axbxcx')
129+
130+
print(my_tuple[::2])
131+
132+
"""
133+
14. Write a Python program to find the index of an item of a tuple.
134+
"""
135+
136+
my_tuple = ('a', 'b')
137+
print(my_tuple.index('a'))
138+
139+
"""
140+
15. Write a Python program to find the length of a tuple.
141+
"""
142+
143+
my_tuple = ('a', 'b')
144+
print(len(my_tuple))
145+
146+
"""
147+
16. Write a Python program to convert a tuple to a dictionary.
148+
"""
149+
150+
my_tuple = ('a', 1, 'b', 2)
151+
152+
my_dict = {}
153+
for i in range(0, len(my_tuple), 2):
154+
my_dict[my_tuple[i]] = my_tuple[i+1]
155+
print(my_dict)
156+
157+
"""
158+
17. Write a Python program to unzip a list of tuples into individual lists.
159+
"""
160+
161+
my_list = [(1, 2), ('a', 'b'), (True, False)]
162+
new_list = []
163+
for i in my_list:
164+
new_list.append(list(i))
165+
print(*new_list)
166+
167+
"""
168+
18. Write a Python program to reverse a tuple.
169+
"""
170+
my_tuple = (1, 2, 3, 4)
171+
reversed_tuple = my_tuple[::-1]
172+
print(reversed_tuple)
173+
174+
"""
175+
19. Write a Python program to convert a list of tuples into a dictionary.
176+
"""
177+
178+
# solution 1 for unique keys
179+
my_list = [('a', 1), ('b', 2)]
180+
my_dict = {i[0]:i[1] for i in my_list}
181+
print(my_dict)
182+
183+
# solution 2 for same key appearing in several tuples
184+
my_list = [('a', 1), ('b', 2), ('a', 5)]
185+
my_dict = {}
186+
for i in my_list:
187+
my_dict.setdefault(i[0],[]).append(i[1])
188+
print(my_dict)
189+
190+
"""
191+
20. Write a Python program to print a tuple with string formatting.
192+
Sample tuple : (100, 200, 300)
193+
Output : This is a tuple (100, 200, 300)
194+
"""
195+
196+
my_tuple = (100, 200, 300)
197+
print('This is a tuple {}'.format(my_tuple))
198+
199+
"""
200+
21. Write a Python program to replace last value of tuples in a list.
201+
Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
202+
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]
203+
"""
204+
205+
my_list = [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
206+
207+
new_list = [i[:len(i)-1]+(100,) for i in my_list]
208+
print(new_list)
209+
210+
"""
211+
22. Write a Python program to remove an empty tuple(s) from a list of tuples.
212+
Sample data: [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
213+
Expected output: [('',), ('a', 'b'), ('a', 'b', 'c'), 'd']
214+
"""
215+
216+
217+
my_list = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
218+
219+
new_list = [i for i in my_list if i]
220+
print(new_list)
221+
222+
"""
223+
23. Write a Python program to sort a listof tuples by the float element.
224+
Sample data: [('item1', '12.20'), ('item2', '15.10'), ('item3', '24.5')]
225+
Expected Output: [('item3', '24.5'), ('item2', '15.10'), ('item1', '12.20')]
226+
"""
227+
228+
my_list = [('item1', '12.20'), ('item2', '15.10'), ('item3', '24.5')]
229+
230+
my_list.sort(key=lambda x: x[1], reverse=True)
231+
print(my_list)
232+
233+
"""
234+
24. Write a Python program to count the elements in a list until an element is a tuple.
235+
"""
236+
237+
my_list = [1, 2, 3, (4,), 5, 6]
238+
239+
counter = 0
240+
for i in my_list:
241+
if not isinstance(i, tuple):
242+
counter += 1
243+
else:
244+
break
245+
print(counter)

0 commit comments

Comments
 (0)