TUPLE
TUPLE
TUPLE
CREATING TUPLE:
• Tuple is one of 4 built-in data types in Python used to store collections of data, the other
3 are List, Set, and Dictionary, all with different qualities and usage.
Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
UTILITY OF TUPLES:
Tuple items are ordered, unchangeable, and allow duplicate values.
Tuple items are indexed, the first item has index [0], the second item has index [1]
Ordered:
When we say that tuples are ordered, it means that the items have a defined order, and that order
will not change.
Unchangeable:
Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has
been created.
Allow Duplicates:
Since tuples are indexed, they can have items with the same value
Example:
EXAMPLE:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
OUTPUT:
banana
Negative Indexing
Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.
EXAMPLE:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
OUTPUT:
cherry
UPDATING TUPLES:
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the
tuple is created.
But there are some workarounds.
Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or
immutable as it.
But there is a workaround. You can convert the tuple into a list, change the list, and
convert the list back into a tuple.
Example
Convert the tuple into a list to be able to change it:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Add Items
Since tuples are immutable, they do not have a build-in append() method, but there
are other ways to add items to a tuple.
1.Convert into a list:
Just like the workaround for changing a tuple, you can convert it into a list, add your item(s), and convert it
back into a tuple.
Example
Convert the tuple into a list, add "orange", and convert it back into a tuple:
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
2. Add tuple to a tuple:
You are allowed to add tuples to tuples, so if you want to add one item, (or
many), create a new tuple with the item(s), and add it to the existing tuple:
Example
Create a new tuple with the value "orange", and add that tuple:
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
Note: When creating a tuple with only one item, remember to include a comma after the item,
otherwise it will not be identified as a tuple.
DELETING ELEMENTS IN TUPLES:
Tuples are unchangeable, so you cannot remove items from it, but you can use the
same workaround as we used for changing and adding tuple items.
Example
Convert the tuple into a list, remove "apple", and convert it back into a tuple:
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
you can delete the tuple completely:
EXAMPLE:
>>> t = (1, 2, 3, 6, 7, 8)
>>> 2 in t
>>> 5 in t
Output:
True
False
NESTED TUPLES:
A nested tuple is a Python tuple that has been placed inside of another tuple. Let's have a look at
the following 8-element tuple.
tuple = (12, 23, 36, 20, 51, 40, (200, 240, 100))
This last element, which consists of three items enclosed in parenthesis, is known as a nested
tuple since it is contained inside another tuple. The name of the main tuple with the index value,
tuple[index], can be used to obtain the nested tuple, and we can access each item of the nested tuple
by using tuple[index-1][index-2].
EXAMPLE:
print(employee)
employee = ((10, "Itika", 13000), (24, "Harry", 15294), (15, "Naill", 20001), (40, "Peter", 16395))
print(employee)
OUTPUT:
((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395))