|
1 | 1 | print("------------------------------------------------------------------------------------------")
|
2 | 2 | print("-------------------------------------Tuple------------------------------------------------")
|
3 | 3 | print("------------------------------------------------------------------------------------------")
|
| 4 | + |
| 5 | +# Tuple |
| 6 | +# A tuple is a collection of ordered elements that can be of different data types. |
| 7 | +# Tuples are immutable, meaning that once they are created, their elements cannot be changed. |
| 8 | +# Tuples are defined using parentheses () and can contain any number of elements. |
| 9 | +# Tuples can also be nested, meaning that a tuple can contain other tuples as elements. |
| 10 | +# Tuples are similar to lists, but they are immutable, |
| 11 | +# meaning that their elements cannot be changed after they are created. |
| 12 | +# Tuples are often used to group related data together, |
| 13 | +# such as coordinates (x, y) or RGB color values (red, green, blue). |
| 14 | +# Tuples can also be used as keys in dictionaries, |
| 15 | +# while lists cannot be used as keys in dictionaries because they are mutable. |
| 16 | +# Tuples are also more memory efficient than lists, |
| 17 | +# making them a better choice for storing large amounts of data. |
| 18 | +# time complexity of tuple is O(1) for accessing elements, |
| 19 | +# O(n) for searching elements, and O(n) for iterating through the elements. |
| 20 | +# Tuples are also faster than lists for certain operations, |
| 21 | +# such as concatenation and repetition. |
| 22 | +# Tuples are also hashable, meaning that they can be used as keys in dictionaries, |
| 23 | +# while lists are not hashable and cannot be used as keys in dictionaries. |
| 24 | +# Tuples are also more memory efficient than lists, |
| 25 | +# making them a better choice for storing large amounts of data. |
| 26 | + |
| 27 | +# Methods of Tuple |
| 28 | +# 1. count() - Returns the number of occurrences of a specified value in a tuple. |
| 29 | +# 2. index() - Returns the index of the first occurrence of a specified value in a tuple. |
| 30 | +# 3. len() - Returns the number of elements in a tuple. |
| 31 | +# 4. max() - Returns the largest element in a tuple. |
| 32 | +# 5. min() - Returns the smallest element in a tuple. |
| 33 | +# 6. sum() - Returns the sum of all elements in a tuple. |
| 34 | +# 7. sorted() - Returns a sorted list of the elements in a tuple. |
| 35 | +# 8. all() - Returns True if all elements in a tuple are true (or if the tuple is empty). |
| 36 | +# 9. any() - Returns True if any element in a tuple is true. If the tuple is empty, returns False. |
| 37 | +# 10. tuple() - Converts an iterable (like a list) into a tuple. |
| 38 | +# 11. zip() - Combines two or more tuples into a single tuple of tuples. |
| 39 | +# 12. enumerate() - Returns an enumerate object, which contains pairs of index and value from the tuple. |
| 40 | +# 13. reversed() - Returns a reversed iterator of the tuple. |
| 41 | +# 14. map() - Applies a function to all items in the tuple and returns a new tuple. |
| 42 | +# 15. filter() - Filters elements from the tuple based on a function and returns a new tuple. |
| 43 | +# 16. repeat() - Repeats the elements of a tuple a specified number of times. |
| 44 | +# 17. unpacking - Unpacks the elements of a tuple into separate variables. |
| 45 | +# 18. slicing - Returns a new tuple that contains a portion of the original tuple. |
| 46 | +# 19. concatenation - Combines two or more tuples into a single tuple. |
| 47 | +# 20. repetition - Repeats the elements of a tuple a specified number of times. |
| 48 | +# 21. membership - Checks if an element is present in a tuple. |
| 49 | +# 22. iteration - Iterates through the elements of a tuple. |
| 50 | +# 23. copying - Creates a shallow copy of a tuple. |
| 51 | +# 24. deep copying - Creates a deep copy of a tuple. |
| 52 | +# 25. sorting - Sorts the elements of a tuple and returns a new tuple. |
| 53 | +# 26. reversing - Reverses the elements of a tuple and returns a new tuple. |
| 54 | +# 27. converting - Converts a tuple to a list and vice versa. |
| 55 | +# 28. packing - Packs multiple values into a tuple. |
| 56 | + |
| 57 | +# Example of Tuple |
| 58 | +# Creating a tuple |
| 59 | +my_tuple = (1, 2, 3, 4, 5) |
| 60 | +print("Tuple:", my_tuple) |
| 61 | + |
| 62 | +# Accessing elements in a tuple |
| 63 | +print("First element:", my_tuple[0]) |
| 64 | +print("Last element:", my_tuple[-1]) |
| 65 | +print("Slice of tuple:", my_tuple[1:4]) |
| 66 | +print("Tuple length:", len(my_tuple)) |
| 67 | + |
| 68 | +# Concatenating tuples |
| 69 | +tuple1 = (1, 2, 3) |
| 70 | +tuple2 = (4, 5, 6) |
| 71 | +tuple3 = tuple1 + tuple2 |
| 72 | +print("Concatenated tuple:", tuple3) |
| 73 | + |
| 74 | +# Repeating tuples |
| 75 | +tuple4 = tuple1 * 3 |
| 76 | +print("Repeated tuple:", tuple4) |
| 77 | + |
| 78 | +# Unpacking tuples |
| 79 | +a, b, c = tuple1 |
| 80 | +print("Unpacked values:", a, b, c) |
| 81 | + |
| 82 | +# Nested tuples |
| 83 | +nested_tuple = (1, (2, 3), (4, 5)) |
| 84 | +print("Nested tuple:", nested_tuple) |
| 85 | + |
| 86 | +# Accessing nested tuple elements |
| 87 | +print("Nested tuple element:", nested_tuple[1][0]) |
| 88 | +print("Nested tuple length:", len(nested_tuple[1])) |
| 89 | + |
| 90 | +# Tuple methods |
| 91 | +# Count method |
| 92 | +count_tuple = (1, 2, 3, 1, 4, 5) |
| 93 | +print("Count of 1 in tuple:", count_tuple.count(1)) |
| 94 | + |
| 95 | +# Index method |
| 96 | +print("Index of 3 in tuple:", count_tuple.index(3)) |
| 97 | + |
| 98 | +# Max method |
| 99 | +print("Max element in tuple:", max(count_tuple)) |
| 100 | + |
| 101 | +# Min method |
| 102 | +print("Min element in tuple:", min(count_tuple)) |
| 103 | + |
| 104 | +# Sum method |
| 105 | +print("Sum of elements in tuple:", sum(count_tuple)) |
| 106 | + |
| 107 | +# Sorted method |
| 108 | +print("Sorted tuple:", sorted(count_tuple)) |
| 109 | + |
| 110 | +# All method |
| 111 | +print("All elements are true:", all(count_tuple)) |
| 112 | + |
| 113 | +# Any method |
| 114 | +print("Any element is true:", any(count_tuple)) |
| 115 | + |
| 116 | +# Tuple conversion |
| 117 | +list_to_tuple = [1, 2, 3, 4, 5] |
| 118 | +tuple_from_list = tuple(list_to_tuple) |
| 119 | +print("Tuple from list:", tuple_from_list) |
| 120 | + |
| 121 | +# Zip method |
| 122 | +tuple1 = (1, 2, 3) |
| 123 | +tuple2 = (4, 5, 6) |
| 124 | +zipped_tuple = zip(tuple1, tuple2) |
| 125 | +print("Zipped tuple:", list(zipped_tuple)) |
| 126 | + |
| 127 | +# Enumerate method |
| 128 | +tuple1 = (1, 2, 3) |
| 129 | +enumerated_tuple = enumerate(tuple1) |
| 130 | +print("Enumerated tuple:", list(enumerated_tuple)) |
| 131 | + |
| 132 | +# Reversed method |
| 133 | +tuple1 = (1, 2, 3) |
| 134 | +reversed_tuple = reversed(tuple1) |
| 135 | +print("Reversed tuple:", list(reversed_tuple)) |
| 136 | + |
| 137 | +# Map method |
| 138 | +def square(x): |
| 139 | + return x * x |
| 140 | +tuple1 = (1, 2, 3) |
| 141 | +mapped_tuple = map(square, tuple1) |
| 142 | +print("Mapped tuple:", tuple(mapped_tuple)) |
| 143 | + |
| 144 | +# Filter method |
| 145 | +def is_even(x): |
| 146 | + return x % 2 == 0 |
| 147 | +tuple1 = (1, 2, 3, 4, 5) |
| 148 | +filtered_tuple = filter(is_even, tuple1) |
| 149 | +print("Filtered tuple:", tuple(filtered_tuple)) |
| 150 | + |
| 151 | +# Repeat method |
| 152 | +from itertools import repeat |
| 153 | +tuple1 = (1, 2, 3) |
| 154 | +repeated_tuple = repeat(tuple1, 3) |
| 155 | +print("Repeated tuple:", list(repeated_tuple)) |
0 commit comments