|
| 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 | +--- |
0 commit comments