|
| 1 | +## Tuple |
| 2 | + |
| 3 | +Tuples are used to store multiple items in a single variable. |
| 4 | + |
| 5 | +Tuple is one of the four built-in data types in Python used to store collections of data. The other three are List, Set, and Dictionary, each with different qualities and usage. |
| 6 | + |
| 7 | +### Ordered |
| 8 | + |
| 9 | +When we say that tuples are ordered, it means that the items have a defined order, and that order will not change. |
| 10 | + |
| 11 | +### Unchangeable |
| 12 | + |
| 13 | +Tuples are unchangeable, meaning that we cannot change, add, or remove items after the tuple has been created. |
| 14 | + |
| 15 | +### Allow Duplicates |
| 16 | + |
| 17 | +A tuple is a collection that is ordered and unchangeable. Tuples can contain duplicate items. |
| 18 | + |
| 19 | +Tuples are written with round brackets `()`. |
| 20 | + |
| 21 | +```python |
| 22 | +thistuple = ("apple", "banana", "cherry") |
| 23 | +print(thistuple) |
| 24 | +print(type(thistyple)) |
| 25 | +``` |
| 26 | +## Modifying Tuples |
| 27 | + |
| 28 | +Tuples in Python are immutable, which means you **cannot** directly add or remove items from them once they are created. However, there are some workarounds to modify tuples: |
| 29 | + |
| 30 | +### Adding Items to a Tuple |
| 31 | + |
| 32 | +To add an item to a tuple, you can convert the tuple to a list, add the item, and then convert it back to a tuple. |
| 33 | + |
| 34 | +```python |
| 35 | +thistuple = ("apple", "banana", "cherry") |
| 36 | +temp_list = list(thistuple) # Convert tuple to list |
| 37 | +temp_list.append("orange") # Add item to the list |
| 38 | +thistuple = tuple(temp_list) # Convert list back to tuple |
| 39 | +print(thistuple) # Output: ('apple', 'banana', 'cherry', 'orange') |
| 40 | +``` |
| 41 | + |
| 42 | +### Tuple Practice Questions |
| 43 | + |
| 44 | +## Creating Tuples: |
| 45 | +Create a tuple named fruits that contains the items "apple", "banana", and "cherry". |
| 46 | + |
| 47 | +## Accessing Elements: |
| 48 | +Given a tuple fruits = ("apple", "banana", "cherry"), access and print the second item in the tuple. |
| 49 | + |
| 50 | +## Negative Indexing: |
| 51 | +Using the tuple fruits = ("apple", "banana", "cherry"), print the last item using negative indexing. |
| 52 | + |
| 53 | +## Tuple Length: |
| 54 | +Create a tuple named colors with the values "red", "green", "blue", and "yellow". Write a program to find and print the length of the tuple. |
| 55 | + |
| 56 | +## Tuple Slicing: |
| 57 | +Given the tuple numbers = (10, 20, 30, 40, 50, 60), slice the tuple to get a new tuple with the elements 20, 30, and 40. |
| 58 | + |
| 59 | +## Concatenating Tuples: |
| 60 | +Create two tuples, tuple1 with values (1, 2, 3) and tuple2 with values (4, 5, 6). Concatenate these two tuples into a new tuple. |
| 61 | + |
| 62 | +## Finding Elements: |
| 63 | +Write a program to check if the element "python" exists in the tuple languages = ("java", "python", "c++", "ruby"). |
| 64 | + |
| 65 | +## Tuple Unpacking: |
| 66 | +Given a tuple person = ("John", 25, "Engineer"), unpack the values into separate variables and print them. |
| 67 | + |
| 68 | +## Immutable Nature: |
| 69 | +Write a program to try to change the first item of the tuple animals = ("cat", "dog", "rabbit") to "bird". What happens? |
| 70 | + |
| 71 | +## Nested Tuples: |
| 72 | +Create a tuple named nested_tuple with values (1, (2, 3), 4). Access and print the value 3 from the nested tuple. |
| 73 | + |
| 74 | +## Converting List to Tuple: |
| 75 | +Convert the list weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] to a tuple and print it. |
| 76 | + |
| 77 | +## Iterating Over a Tuple: |
| 78 | +Write a program to iterate through the tuple prime_numbers = (2, 3, 5, 7, 11) and print each element. |
| 79 | + |
| 80 | +## Tuple Methods: |
| 81 | +Use the tuple method count() to find out how many times the value 4 appears in the tuple numbers = (1, 2, 3, 4, 4, 4, 5). |
| 82 | + |
| 83 | +## Tuple Index: |
| 84 | +Write a program to find the index of the value 100 in the tuple data = (10, 20, 30, 100, 50, 100). |
| 85 | + |
| 86 | +## Deleting a Tuple: |
| 87 | +Create a tuple months = ("January", "February", "March"). Delete this tuple and try printing it afterward. What happens? |
0 commit comments