Tuple
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python
A tuple is a collection which is ordered and unchangeable.(IMMUTABLE)
Tuples are written with round brackets.
Example
Create a Tuple:
T= ("Python", “HTML", "Java")
print(T)
Output: ("Python", “HTML", "Java")
Tuple Items - Data Types
Tuple items can be of any data type:
tuple1 =("Python", “HTML", "Java")
tuple2 = (10, 50,27,9.8,93)
tuple3 = (True, False, False)
tuple4=(“python”,10,”a”,23.45,True)
The tuple() Constructor
It is also possible to use the tuple() constructor to make a tuple.
Example
Using the tuple() method to make a tuple:
T= tuple(("Python", “HTML", "Java")) # note the double round-brackets
print(T)
Output: ("Python", “HTML", "Java")
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.
Example
One item tuple, remember the comma:
T = ("oakridge",)
print(type(T))
Output: <class 'tuple'>
#NOT a tuple
T = ("oakridge")
print(type(T))
Output: <class 'str'>
Tuple Items
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.
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
Tuples allow duplicate values:
T = ("Ram", "Shyam", "Gopi", "Ram", "Gopi")
print(T)
Output: ("Ram", "Shyam", "Gopi", "Ram", "Gopi")
Tuple Length
To determine how many items a tuple has, use the len() function:
Example
Print the number of items in the tuple:
T = ("Ram", "Shyam", "Gopi", "Ram", "Gopi")
print(len(T))
Output: 5
Access Tuple Items
You can access tuple items by referring to the index number, inside square
brackets: Note: The first item has index 0.
Example
Print the second item in the tuple:
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[1])
Output: HTML
Range of Indexes
You can specify a range of indexes by specifying where to start and where to
end the range.
Syntax: T [ start:Stop:Step]
By leaving out the start value, the range will start at the first item:
By leaving out the end value, the range will go on to the end of the list:
By leaving out the step value, the range will go on to every element in
the list:
When specifying a range, the return value will be a new tuple with the specified
items.
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[1:4])
Here the start value is 1 and stop is 4.So till index 3 is considered to
print.
Output: (“HTML", "Java",”c”)
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[:4])
Output: ("Python", “HTML", "Java",”c”)
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[1:])
Output: (“HTML", "Java",”c”,”c++”)
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the
tuple:
Indexing starts at -1 from right edge .
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[-5:-1])
Output: ("Python", “HTML", "Java",”c”
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[-5:])
Output: ("Python", “HTML", "Java",”c”,”c++”)
T= ("Python", “HTML", "Java",”c”,”c++”)
print(T[::-1])
Output(”c++,”C”,”java”,”html”,”python”) prints the tuple in reverse
order
Check if Item Exists
To determine if a specified item is present in a tuple use the in keyword:
Example
Check if "html" is present in the tuple:
T= ("Python", “HTML", "Java",”c”,”c++”)
if "html" in T:
print("Yes, 'html' is in the given tuple")
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. 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=("Python", “HTML", "Java",”c”,”c++”)
y = list(x)
y[1] = "Scratch"
x = tuple(y)
print(x)
Output:("Python", “Scratch", "Java",”c”,”c++”)
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 "xml", and convert it back into a tuple:
x=("Python", “HTML", "Java",”c”,”c++”)
y = list(x)
y.append("xml")
x = tuple(y)
Output: ("Python", “HTML", "Java",”c”,”c++”,”xml”)
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:
x=("Python", “HTML", "Java",”c”,”c++”)
y = ("grade8”,”cbse”)
x += y
print(x)
Output: ("Python", “HTML", "Java",”c”,”c++”,"grade8”,”cbse”)
Remove Items
Note: You cannot remove items in a tuple.
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:
x=("Python", “HTML", "Java",”c”,”c++”)
y = list(x)
y.remove("Java")
x = tuple(y)
print(x)
Output: =("Python", “HTML",”c”,”c++”)
Or you can delete the tuple completely:
Example
The del keyword can delete the tuple completely:
x=("Python", “HTML", "Java",”c”,”c++”)
print(x)
Output:
#this will raise an error because the tuple no longer exists