|
| 1 | +#Using python to manipulate tuples |
| 2 | +''' |
| 3 | +Python Tuple is used to store the sequence of immutable Python |
| 4 | +objects. |
| 5 | +
|
| 6 | +Tuples are written as a list of "comma-separated" |
| 7 | +values (items) between parentheses. () |
| 8 | +
|
| 9 | +Tuples are immutable - this means that items can not be changed. |
| 10 | +However,a tuple can contain mutable objects. |
| 11 | +
|
| 12 | +Tuple has 2 methods available. |
| 13 | +count() Returns the number of elements with the specified value |
| 14 | +index() Returns the index of the first element with the specified value |
| 15 | +''' |
| 16 | + |
| 17 | +#The basics - tuple packing |
| 18 | +t = 12345, 54321, 'hello!' |
| 19 | +t[0] |
| 20 | +t |
| 21 | +type(t) |
| 22 | + |
| 23 | +T1 = (101, "Peter", 22) |
| 24 | +T2 = ("Apple", "Banana", "Orange") |
| 25 | +T3 = 10,20,30,40,50 |
| 26 | + |
| 27 | +print(type(T1)) |
| 28 | +print(type(T2)) |
| 29 | +print(type(T3)) # Class Tuple |
| 30 | + |
| 31 | +#The tuple which is created without using parentheses |
| 32 | +# is also known as tuple packing. |
| 33 | +a = 'abc', 2, 4, 'd' |
| 34 | +print(type(a)) # Class Tuple |
| 35 | + |
| 36 | +#An empty tuple can be created as follows. |
| 37 | +T4 = () |
| 38 | + |
| 39 | +#Creating a tuple with single element is slightly different. |
| 40 | +# We will need to put comma after the element to declare the tuple |
| 41 | +tup1 = ("JavaTpoint") #String |
| 42 | +print(type(tup1)) |
| 43 | +#Creating a tuple with single element |
| 44 | +tup2 = ("JavaTpoint",) #Tuple |
| 45 | +print(type(tup2)) |
| 46 | +''' |
| 47 | +Output |
| 48 | +<class 'str'> |
| 49 | +<class 'tuple'> |
| 50 | +''' |
| 51 | +#Example 1 |
| 52 | +tuple1 = (10, 20, 30, 40, 50, 60) |
| 53 | +print(tuple1) |
| 54 | +count = 0 |
| 55 | +for i in tuple1: |
| 56 | + print("tuple1[%d] = %d"%(count, i)) |
| 57 | + count = count+1 |
| 58 | + |
| 59 | +''' |
| 60 | +(10, 20, 30, 40, 50, 60) |
| 61 | +tuple1[0] = 10 |
| 62 | +tuple1[1] = 20 |
| 63 | +tuple1[2] = 30 |
| 64 | +tuple1[3] = 40 |
| 65 | +tuple1[4] = 50 |
| 66 | +tuple1[5] = 60 |
| 67 | +''' |
| 68 | + |
| 69 | +#Example 2 |
| 70 | +tuple1 = tuple(input("Enter the tuple elements ...")) |
| 71 | +print(tuple1) |
| 72 | +count = 0 |
| 73 | +for i in tuple1: |
| 74 | + print("tuple1[%d] = %s"%(count, i)) |
| 75 | + count = count+1 |
| 76 | +''' |
| 77 | +Output |
| 78 | +Enter the tuple elements ...123456 |
| 79 | +('1', '2', '3', '4', '5', '6') |
| 80 | +tuple1[0] = 1 |
| 81 | +tuple1[1] = 2 |
| 82 | +tuple1[2] = 3 |
| 83 | +tuple1[3] = 4 |
| 84 | +tuple1[4] = 5 |
| 85 | +tuple1[5] = 6 |
| 86 | +''' |
| 87 | +#indexing |
| 88 | +tuple = (1,2,3,4,5,6,7) |
| 89 | +#element 1 to end |
| 90 | +print(tuple[1:]) |
| 91 | +#element 0 to 3 element |
| 92 | +print(tuple[:4]) |
| 93 | +#element 1 to 4 element |
| 94 | +print(tuple[1:5]) |
| 95 | +# element 0 to 6 and take step of 2 |
| 96 | +print(tuple[0:6:2]) |
| 97 | +''' |
| 98 | +Output |
| 99 | +(2, 3, 4, 5, 6, 7) |
| 100 | +(1, 2, 3, 4) |
| 101 | +(1, 2, 3, 4) |
| 102 | +(1, 3, 5) |
| 103 | +''' |
| 104 | + |
| 105 | +#Negative Indexing |
| 106 | +tuple1 = (1, 2, 3, 4, 5) |
| 107 | +print(tuple1[-1]) |
| 108 | +print(tuple1[-4]) |
| 109 | +print(tuple1[-3:-1]) |
| 110 | +print(tuple1[:-1]) |
| 111 | +print(tuple1[-2:]) |
| 112 | +''' |
| 113 | +Output |
| 114 | +5 |
| 115 | +2 |
| 116 | +(3, 4) |
| 117 | +(1, 2, 3, 4) |
| 118 | +(4, 5) |
| 119 | +''' |
| 120 | +#Deleting Tuple |
| 121 | +''' |
| 122 | +Unlike lists, the tuple items cannot be deleted by using the del keyword |
| 123 | +as tuples are immutable. To delete an entire tuple, we can use the del |
| 124 | +keyword with the tuple name. |
| 125 | +''' |
| 126 | +tuple1 = (1, 2, 3, 4, 5, 6) |
| 127 | +print(tuple1) |
| 128 | +del tuple1[0] |
| 129 | +print(tuple1) |
| 130 | +del tuple1 |
| 131 | +print(tuple1) |
| 132 | +''' |
| 133 | +Output |
| 134 | +(1, 2, 3, 4, 5, 6) |
| 135 | +Traceback (most recent call last): |
| 136 | + File "tuple.py", line 4, in <module> |
| 137 | + print(tuple1) |
| 138 | +NameError: name 'tuple1' is not defined |
| 139 | +''' |
| 140 | +#Python Tuple inbuilt functions |
| 141 | +''' |
| 142 | +SN Function Description |
| 143 | +1 cmp(tuple1, tuple2) It compares two tuples and returns true |
| 144 | + if tuple1 is greater than tuple2 otherwise false. |
| 145 | +2 len(tuple) It calculates the length of the tuple. |
| 146 | +3 max(tuple) It returns the maximum element of the tuple |
| 147 | +4 min(tuple) It returns the minimum element of the tuple. |
| 148 | +5 tuple(seq) It converts the specified sequence to the tuple. |
| 149 | +''' |
| 150 | + |
| 151 | +#Where use tuple? |
| 152 | +''' |
| 153 | +1. Using tuple instead of list gives us a clear idea that tuple data |
| 154 | +is constant and must not be changed. |
| 155 | +2. Tuple can simulate a dictionary without keys. Consider the following |
| 156 | +nested structure, which can be used as a dictionary. |
| 157 | +[(101, "John", 22), (102, "Mike", 28), (103, "Dustin", 30)] |
| 158 | +''' |
| 159 | +# Tuples may be nested: |
| 160 | +v = ([1, 2, 3], [3, 2, 1]) |
| 161 | +u = v, (1, 2, 3, 4, 5) |
| 162 | +u |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
0 commit comments