|
| 1 | +''' TUPLE |
| 2 | + A tuple is a collection which is ordered and unchangeable(immutable). |
| 3 | +''' |
| 4 | + |
| 5 | +''' Adding Elements ''' |
| 6 | +print("**********Adding Elements**********", end="\n\n") |
| 7 | + |
| 8 | + |
| 9 | +tuple = (1, 2, 3, 'Alphabet') |
| 10 | +print(f"Tuple is {tuple}", end="\n\n") |
| 11 | + |
| 12 | + |
| 13 | +''' Accessing Elements ''' |
| 14 | +print("**********Accessing Elements**********", end="\n\n") |
| 15 | + |
| 16 | + |
| 17 | +for x in tuple: |
| 18 | + print(f"Element of tuple is {x}") |
| 19 | + |
| 20 | + |
| 21 | +print(f"\nGetting whole Tuple {tuple}", end="\n\n") |
| 22 | + |
| 23 | +print(f"0th Elements of Tuple is {tuple[0]}", end="\n\n") |
| 24 | + |
| 25 | +print(f"Can be accessed through ':' Operator {tuple[:]}", end="\n\n") |
| 26 | + |
| 27 | +print(f"Reversed Tuple is {tuple[::-1]}", end="\n\n") |
| 28 | + |
| 29 | +print(f"Letter-at-3-index of 3rd-Index-elements is {tuple[3][3]}", end="\n\n") |
| 30 | + |
| 31 | + |
| 32 | +''' Appending Elements ''' |
| 33 | +print("**********Appending Elements**********", end="\n\n") |
| 34 | + |
| 35 | + |
| 36 | +tuple = tuple + (4, 5, ['B', 'C']) #add elements |
| 37 | +print(f"Tuple after appending elements is {tuple}", end="\n\n") |
| 38 | +# (1, 2, 3, 'Alphabet', 4, 5, ['B','C']) |
| 39 | + |
| 40 | + |
| 41 | +''' Other Functions ''' |
| 42 | +print("**********Other Functions**********", end="\n\n") |
| 43 | + |
| 44 | + |
| 45 | +tuple[6][0] = 'K' |
| 46 | +print(f"Changed 6th-index-element to K {tuple}", end="\n\n") |
| 47 | + |
| 48 | +print(f"2 has been repeated {tuple.count(2)} time", end="\n\n") |
| 49 | + |
| 50 | +print(f"Index of ['K','C'] is {tuple.index(['K', 'C'])}", end="\n\n") |
| 51 | + |
0 commit comments