LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Lesson 24: Tuples In Python
A Tuple is a collection of Python objects separated by commas. In someways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists which are mutable.
Tuples are used to store multiple items in a single variable. 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.
A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.
Example
thistuple = ("apple", "banana", "cherry")
print(thistuple)
the output will be
('apple', 'banana', 'cherry')
Ordered: 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] etc.
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
thistuple = ("pineapple", "banana", "cherry", "pineapple", "cherry")
print(thistuple)
the output will be
('pineapple', 'banana', 'cherry', 'pineapple', 'cherry')
Tuple Length: To determine how many items a tuple has, use the len() function
Example Print the number of items in the tuple
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
the output will be
3
How to Create Tuple With One Item?
To create a tuple with only one item, you have to add a comma after
the item, otherwise Python will not recognize it as a tuple.
thistuple = ("apple",)
print(type(thistuple))
output
<class 'tuple'>
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
output
<class 'str'>
Tuple Items-Data Types: Tuple items can be of any data type: String, int and boolean data types.
Example
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
print(tuple1)
print(tuple2)
print(tuple3)
output will be
('apple', 'banana', 'cherry')
(1, 5, 7, 9, 3)
(True, False, False)
A tuple can contain different data types.
Example: the following is a tuple with strings, integers and boolean values.
tuple1 = ("abc", 34, True, 40, "male")
print(tuple1)
the output will be
('abc', 34, True, 40, 'male')
The tuple() Constructor: It is also possible to use the tuple() constructor to make a tuple.
Example
thistuple = tuple(("apple", "banana", "cherry"))
# note the double round-brackets
print(thistuple)
output will be.
('apple', 'banana', 'cherry')
How to add items to tuple?
Tuples are unchangeable, or immutable as it also is called.But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.
Example1: (Convert the tuple into a list to be able to change it)
x = ("pineapple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
output
("apple", "kiwi", "cherry")
Example2: (Convert the tuple into a list, add "orange", and convert it back into a tuple)
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
print(thistuple)
output will be.
('apple', 'banana', 'cherry', 'orange')
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)
output
('apple', 'banana', 'cherry', 'orange')