Tuples are used to store multiple items in a single variable.
A Tuple is a collection which is ordered , unchangeable, and allow duplicate items.
Tuple are written with round brackets .
Tuple items are ordered, unchangeable, and allow duplicate values.
my_tuple =('iphone','samsung','lg')
print(my_tuple)
create an empty Tuple:
my_tuple= ()
len(my_tuple) => 0
create Tuple with One Item:
my_tuple= ('iphone',) # Note a comma after the object.
len(my_tuple) => 1
The tupple() Constructor :
my_tuple =tuple(('iphone','samsung','lg')) # Note a double rounded brackets
print(my_tuple)
Tuples Properties :
- Ordered :
Tuples are ordered. When we say that Tuples are ordered, it means that the items have a defined order, and that order will not change.
- Unchangeable
Tuple items are not changeable, meaning that we can not change, add, and remove items in a Tuple after it has been created.
- Duplicate Allowed
Since tuples are indexed, they can have items with the same value.
- Tuples support indexing and slicing same as String and List and It can be nested as well.
Tuple Length
len() function is use to determine the length of a Tuple.
my_tuple = ('iphone','samsung','lg')
print(len(my_tuple))
Accessing Tuple Values:
Tuples support indexing and slicing same as String and List and It can be nested as well.
x = my_tuple[-1] #indexing
print(my_tuple[1:3]) #slicing
Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
But there is a workaround. We can convert the tuple into a list, change the list, and convert the list back into a tuple.
my_tuple = ('iphone', 'samsung', 'lg')
x = list(my_tuple)
x[1] = "Motorola"
my_tuple = tuple(x)
print(my_tuple)
Add Items to Tuple
Since tuples are immutable, they do not have a build-in append() method, but there are other ways to add items to a tuple.
- Convert into a list
Just like "Change Tuple Values" we can convert the tuple into a list, add item(s) and convert it back into a tuple.
my_tuple = ('iphone', 'samsung', 'lg') x = list(my_tuple) x.append('Motorola') my_tuple = tuple(x)
- Add tuple to a tuple
We are allowed to add tuples to tuples, To add item(s), create a new tuple with the item(s), and add it to the existing tuple.
my_tuple = ('iphone', 'samsung', 'lg') x = ("Motorola",) my_tuple += x print(my_tuple)
Remove Items
As above for Remove item(s) we need to convert the tuple into a list, remove item(s) and convert it back into a tuple.
The del keyword delete the Tuple completely:
del my_tuple
Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is called "packing" a tuple.
But, in Python, we can also extract the values from tuple into variables. This is called ""unpacking""
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
If the number of variables is less than the number of values, we can add an * to the variable name and the values will be assigned to the variable as a list. and If the asterisk is added to another variable name than the last, Python will assign values to the variable until the number of values left matches the number of variables left.
fruits = ("mango", "Lichi", "apple", "banana", "cherry")
(orange, *yellow, green) = fruits
print(green)
print(yellow)
print(red)
Loop Through a Tuple
- using for loop:
can loop through the tuple items by using a for loop.
for x in my_tuple: print(x)
- Loop Through the Index Number:
we can use the range() and len() functions to create a suitable iterable.
for x in range(len(my_tuple)): print(x)
- using while loop:
We can use the len() function to determine the length of the tuple, then start at 0 and loop through the list items by refering to their indexes. Remember to increase the index by 1 after each iteration.
my_tuple = ('iphone', 'samsung', 'lg') i = 0 while i < len(my_tuple): print(my_tuple[i]) i = i + 1
Join Tuples
- using + operator.
my_tuple1 = ("apple", "banana", "cherry") my_tuple2 = (1 , 2, 3, 4, 5) my_tuple3 = my_tuple1 + my_tuple2 print(my_tuple3)
Multiply Tuples
- using * operator.
my_tuple1 = ("apple", "banana", "cherry") my_tuple2 = my_tuple1 * 2 print(my_tuple3)
Tuple Methods
- count()
Returns the number of times a specified value occurs in a tuple.
- index()
Searches the tuple for a specified value and returns the position of where it was found.