Skip to content

Commit 8af0f9e

Browse files
committed
completing the tuple
1 parent 684d8b3 commit 8af0f9e

File tree

5 files changed

+336
-0
lines changed

5 files changed

+336
-0
lines changed

Tuple/Assignemnt/Solutions.md

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
2+
### Python Tuple
3+
4+
1. Write a Python program to create a tuple of integers. Then, print each element of the tuple using a `for` loop.
5+
```python
6+
my_tuple = (1,2,3,4,5)
7+
8+
for i in range(len(my_tuple)):
9+
print(my_tuple[i])
10+
```
11+
12+
2. Create a tuple with mixed data types (e.g., integers, floats, strings). Write a program to display the type of each element in the tuple.
13+
```python
14+
my_tuple = (1,2,True,"Str",3j)
15+
16+
for i in range(len(my_tuple)):
17+
print(my_tuple[i])
18+
```
19+
20+
3. Write a Python program to check if an element exists in a tuple. If it exists, print the index of the element.
21+
```python
22+
my_tuple = (1,2,3,4,5)
23+
24+
if 3 in my_tuple:
25+
print(my_tuple.index(3))
26+
```
27+
28+
4. Write a Python program to convert a tuple into a list, modify the list, and convert it back into a tuple.
29+
```python
30+
my_tuple = (1,2,3,4,5)
31+
my_list = list(my_tuple)
32+
my_list[0] = 99
33+
my_tuple = tuple(my_list)
34+
print(my_tuple) # (99, 2, 3, 4, 5)
35+
```
36+
37+
5. Create a program that takes a tuple of numbers as input from the user and prints the sum, minimum, and maximum values.
38+
```python
39+
nums = input("Enter numbers separated by commas: ")
40+
my_tuple = tuple(map(int,nums.split(",")))
41+
print("your input",my_tuple)
42+
43+
total_sum = sum(my_tuple)
44+
minimum = min(my_tuple)
45+
maximum = max(my_tuple)
46+
47+
print(f"Sum : {total_sum}")
48+
print(f"Minimum : {minimum}")
49+
print(f"Maximum : {maximum}")
50+
```
51+
52+
### Access and Update
53+
54+
6. Write a Python program to access the first and last elements of a tuple. Use both positive and negative indexing.
55+
```python
56+
my_tuple = (1,2,3,4,5)
57+
# first element
58+
print(my_tuple[0])
59+
print(my_tuple[-len(my_tuple)])
60+
61+
#last element
62+
print(my_tuple[len(my_tuple) - 1])
63+
print(my_tuple[-1])
64+
```
65+
66+
7. Write a Python program to slice a tuple. Extract elements from index 2 to 5 from the tuple `(10, 20, 30, 40, 50, 60)`.
67+
```python
68+
my_tuple = (10, 20, 30, 40, 50, 60)
69+
print(my_tuple[2:5]) #(30, 40, 50)
70+
```
71+
72+
8. Write a program to demonstrate that tuples are immutable by attempting to change an element of a tuple. Handle the error gracefully.
73+
```python
74+
# do it after exception handing
75+
```
76+
9. Write a Python program that creates a tuple, converts it to a list, updates an element of the list, and then converts the list back into a tuple.
77+
```python
78+
my_tuple = (1,2,3,4,5)
79+
my_list = list(my_tuple)
80+
my_list[0] = 99
81+
my_tuple = tuple(my_list)
82+
print(my_tuple) # (99, 2, 3, 4, 5)
83+
```
84+
85+
10. Create a Python program to access the elements of a tuple using a `for` loop, printing both the index and the element.
86+
```python
87+
my_tuple = (10, 20, 30, 40, 50, 60)
88+
89+
for i in range(len(my_tuple)):
90+
print(f"Index {i} : Element {my_tuple[i]}")
91+
```
92+
93+
### Unpacking
94+
95+
11. Write a Python program that unpacks the tuple `(1, 2, 3)` into three variables `a`, `b`, and `c`, and prints the values of these variables.
96+
```python
97+
my_tuple = (1,2,3)
98+
99+
a,b,c = my_tuple
100+
print(f"Value of a : {a}")
101+
print(f"Value of b : {b}")
102+
print(f"Value of c : {c}")
103+
```
104+
105+
12. Create a tuple containing 5 elements and unpack the first two elements into individual variables, while the remaining elements are stored in another variable using the `*` operator.
106+
```python
107+
my_tuple = (10,20,30,40,50)
108+
109+
n1,n2,*n3 = my_tuple
110+
111+
print(f"Value of n1 : {n1}") # 10
112+
print(f"Value of n2 : {n2}") # 20
113+
print(f"Value of n3 : {n3}") # [30, 40, 50]
114+
```
115+
116+
13. Write a Python program to swap the values of two variables using tuple unpacking.
117+
```python
118+
a = 20
119+
b = 30
120+
my_tuple = (a,b)
121+
122+
b,a = my_tuple
123+
print(f"Value of a : {a}") # 30
124+
print(f"Value of b : {b}") # 20
125+
```
126+
127+
14. Write a Python program that takes a tuple of three elements (name, age, city) and unpacks them into individual variables.
128+
```python
129+
data = ("jhon",30,"London")
130+
print(data)
131+
name, age, city = data
132+
print("Name : ",name)
133+
print("Name : ",age)
134+
print("Name : ",city)
135+
```
136+
137+
15. Create a Python program that accepts a tuple with different lengths of sequences and unpacks them using extended unpacking (`*` operator).
138+
```python
139+
user_input= input("Enter elements separated by commas: ")
140+
# example - user_input -> 1,2,3,4,5
141+
my_tuple = tuple(user_input.split(","))
142+
143+
first , *second_list , last = my_tuple
144+
145+
print(f"value of first : {first}") # 1
146+
print(f"value of second : {second_list}") # [2,3,4]
147+
print(f"value of last : {last}") # 5
148+
```
149+
150+
### Join
151+
152+
16. Write a Python program to join two tuples into one. Display the result of concatenating `(1, 2, 3)` and `(4, 5, 6)`.
153+
```python
154+
tuple1 = (1,2,3)
155+
tuple2 = (4,5,6)
156+
tuple3 = tuple1 + tuple2
157+
print(tuple3)
158+
```
159+
160+
17. Create a program to concatenate multiple tuples and print the result.
161+
```python
162+
my_tuple = (1,2,3)
163+
print(my_tuple * 3)
164+
# 1, 2, 3, 1, 2, 3, 1, 2, 3)
165+
166+
```
167+
168+
18. Write a Python program to join two tuples, but before joining, check if both tuples contain at least one common element. If yes, concatenate them, otherwise return an error message.
169+
```python
170+
# do it after exception handing
171+
```
172+
173+
19. Write a Python program to repeat a tuple `n` times using the `*` operator. Take `n` as input from the user.
174+
```python
175+
my_tuple = (1,2,3)
176+
print(my_tuple)
177+
178+
n = int(input("Enter the vlaue of n to repeate tuple : "))
179+
print(my_tuple * n)
180+
181+
```
182+
183+
20. Write a Python program to join a tuple of strings into a single string, separated by spaces or a given delimiter.
184+
```python
185+
my_tuple = ("Hello","world,","i","am","on","fire","🔥")
186+
delimeter = "-"
187+
result = delimeter.join(my_tuple)
188+
print(result)
189+
# Hello-world,-i-am-on-fire-🔥
190+
```
191+
192+
### Tuple Methods
193+
194+
21. Write a Python program to count the occurrences of an element in a tuple using the `count()` method.
195+
```python
196+
my_tuple = ("apple","banana","coconut","banana")
197+
print(my_tuple.count("banana")) # 2
198+
199+
```
200+
201+
22. Create a Python program to find the index of a specified element in a tuple using the `index()` method.
202+
```python
203+
my_tuple = ("apple","banana","coconut","banana")
204+
print(my_tuple.index("banana")) # 1
205+
206+
```
207+
208+
23. Write a Python program to find and print the first occurrence of the number 5 in a tuple. If the element is not found, handle the error gracefully.
209+
```python
210+
# do it after exception handing
211+
```
212+
213+
24. Write a Python program to create a tuple, find the length of the tuple using the `len()` function, and print the result.
214+
```python
215+
my_tuple = (1,2,3,4,5,6,7)
216+
print(len(my_tuple))
217+
```
218+
219+
25. Create a Python program that demonstrates the immutability of tuples by trying to modify an element. Then, show how tuple methods like `index()` and `count()` can still work.
220+
```python
221+
my_tuple = (1,2,3,4,5)
222+
# my_tuple[0] = 99
223+
#TypeError: 'tuple' object does not support item assignment
224+
225+
print(my_tuple)
226+
227+
print(my_tuple.count(2)) # number of occurrences of 2 = 1.
228+
print(my_tuple.index(5)) # index of 5 = 4
229+
```
230+
231+
---

Tuple/Practice.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Do practice here

Tuple/Tuple.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
my_tuple = (1,2,3,4,5)
2+
my_tuple = tuple((1,2,3,4,5))
3+
4+
5+
# # access
6+
# by Index
7+
# by negative indexing
8+
# by range based indexing
9+
10+
my_tuple = (1,2,3,4,5)
11+
print(my_tuple[0]) # 1
12+
print(my_tuple[-1]) # 5
13+
print(my_tuple[2:5]) # (3,4,5)
14+
15+
16+
17+
# add.update,delete
18+
19+
# add
20+
my_tuple = (1,2,3,4,5)
21+
my_list = list(my_tuple)
22+
my_list.append(6)
23+
my_tuple = tuple(my_list)
24+
print(my_tuple) # (1,2,3,4,5,6)
25+
26+
# update
27+
my_tuple = (1,2,3,4,5)
28+
my_list = list(my_tuple)
29+
my_list[2] = 6
30+
my_tuple = tuple(my_list)
31+
print(my_tuple) # (1,2,6,4,5)
32+
33+
# remove
34+
my_tuple = (1,2,3,4,5)
35+
my_list = list(my_tuple)
36+
my_list.remove(1)
37+
my_tuple = tuple(my_list)
38+
print(my_tuple) # (2,3,4,5)
39+
40+
# delete
41+
my_tuple = (1,2,3,4)
42+
del my_tuple
43+
44+
45+
# packing & unpacking
46+
my_tuple = (10,20,30)
47+
a ,b, c = my_tuple
48+
print(a,b,c) # 10,20,30
49+
50+
my_tuple = (10,20,30,40,50)
51+
a ,b, *c = my_tuple
52+
print(a,b,c) # 10,20,[30,40,50]
53+
54+
my_tuple = (10,20,30,40,50)
55+
a ,*b, c = my_tuple
56+
print(a,b,c) # 10 [20, 30, 40] 50
57+
58+
my_tuple = (10,20,30,40,50)
59+
*a ,b, c = my_tuple
60+
print(a,b,c) # [10, 20, 30] 40 50

Tuple/Tuple_method.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# tuple having two methods
2+
# 1. count - Returns the number of times a specified value occurs in a tuple
3+
# 2. index - Searches the tuple for a specified value and returns the position of where it was found
4+
5+
my_tuple = (1,2,3,4,5)
6+
print(my_tuple)
7+
8+
print(my_tuple.count(2)) # number of occurrences of 2 = 1.
9+
print(my_tuple.index(5)) # index of 5 = 4

Tuple/Tuple_operations.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# loops
3+
4+
# without index
5+
my_tuple = (1,2,3,4,5)
6+
for x in my_tuple:
7+
print(x)
8+
9+
# with index
10+
my_tuple = (1,2,3,4,5)
11+
for i in range(len(my_tuple)):
12+
print(i,my_tuple[i])
13+
14+
# using while loop
15+
fruits = ("apple", "banana", "cherry")
16+
i = 0
17+
while i < len(fruits):
18+
print(fruits[i])
19+
i = i + 1
20+
21+
# joins
22+
fruit1 = ("apple", "banana", "cherry")
23+
fruit2 = ("avocado", "blueberry", "coconut")
24+
25+
all_fruits = fruit1 + fruit2
26+
print(all_fruits)
27+
#('apple', 'banana', 'cherry', 'avocado', 'blueberry', 'coconut')
28+
29+
# repete n number of times a tuple
30+
31+
fruits = ("apple", "banana", "cherry")
32+
my_tuple = fruits * 2
33+
34+
print(my_tuple)
35+
# ('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')

0 commit comments

Comments
 (0)