tuple in python : When reading data from a database in Python, for example, we may represent each table record as an ordered and immutable series of objects since we don’t need to change the sequence afterwards. Python includes a built-in sequence data type for storing data in an immutable object known as a tuple.
After completing this Python training, you will understand the following:
- How to Use Tuples in Python
- In Python, there is a distinction between tuples and lists.
- The fundamental applications of tuples
What are tuple in python ?
A tuple is a collection of ordered and immutable items. Tuples, like lists, are sequences. Tuples and lists vary in that tuples cannot be altered, although lists may, and tuples use parentheses, whereas lists use square brackets.
A tuple is a collection of ordered and immutable items. Tuples and lists are comparable in that they are both sequences. Tuples and lists vary in that we cannot alter tuples after they have been created, but we may modify lists after they have been created, and we build tuples using parentheses while we make lists with square brackets.
Creating a Tuple
A tuple is formed by putting all of the things (elements) inside parenthesis (), separated by commas. The parentheses are optional, however it is good practise to use them.
A tuple can contain any number of objects of any kind (integer, float, list, string, etc.).
Let’s look at some Python tuple examples:
- () — an empty tuple
- (1.0, 9.9, 10) — a tuple containing three numeric objects
- (‘Michael, Marvin, Markus, Axel’) — a tuple containing four string objects
- (’10, 101, True) — a tuple consisting of a string, an integer, and a Boolean object
hi = ("Lead", "for", "code")
print(hi)
Output
('Lead', 'for', 'code')
Accessing Values in Tuples in Python
1: Using Positive Index
data = ("Michael", "Marvin", "Markus", "Axel")
print("Value in Data[0] = ", data[0])
print("Value in Data[1] = ", data[1])
print("Value in Data[2] = ", data[2])
print("Value in Data[3] = ", data[3])
OUTPUT
Value in Data[0] = Michael
Value in Data[1] = Marvin
Value in Data[2] = Markus
Value in Data[3] = Axel
2: Using Negative index
data = ("Michael", "Marvin", "Markus", "Axel")
print("Value in Data[-10] = ", data[-10])
print("Value in Data[-9] = ", data[-9])
print("Value in Data[-8] = ", data[-8])
print("Value in Data[-7] = ", data[-7])
OUTPUT
Value in Data[-10] = Michael
Value in Data[-9] = Marvin
Value in Data[-8] = Markus
Value in Data[-7] = Axel
3: Tuples Concatenation in Python
Tuple in python can be concatenated by using + . As shown in below example we see data1 and data2 is concatenated in the print statement as shown in below output result
data1 = (0, 1, 2, 3)
data2 = ('lead', 'bycode')
# Concatenating above two tuple
print(data1 + data2)
OUTPUT
(0, 1, 2, 3, 'lead', 'bycode')
4: Unpacking a Tuple
data = "magic",4,'x'
print(data)
# unpacking of tuple is also possible
a, b, c = data
print(a) # magic
print(b) # 4
print(c) # x
OUTPUT
(magic,4,x)
magic
4
x
5. Nesting of Tuple
data = ("magic","day")
value = (1,3,5)
addtuple = (data,value)
print(tuple)
OUTPUT
(('magic','day'),(1,3,5))
6. Tuple Repetition
We can repeat the tuple by using the multiplier here in the below example we are multiplying the lead by 2 thus we get the output as show in the below output.
var tuple = ('lead',)*2
print(tuple
OUTPUT
('lead','lead')
7. Converting List to tuple
listdata = [4,5,6]
print(tuple(listdata))
OUTPUT
(4,5,6)
8. Slicing the Tuple
# Accessing tuple data using slicing
tupledata = (a,b,c,d,e,f,g,h,i,j,k)
print(tupledata[1:4])
print(tupledata[:5])
print(tupledata[4:])
OUTPUT
(b,c,d)
(a,b,c,d,e)
(e,f,g,h,i,j,k)
9. Find the length of Tuple
# Accessing length of tuple data
tupledata = (a,b,c,d,e,f,g,h,i,j,k)
print(len(tupledata))
OUTPUT
11
10. Use Tuple in the loop
tupledata = (1,2)
n = 4
for j in range(int(n)):
print(tupledata)
OUTPUT
(1,2)
(1,2)
(1,2)
(1,2)
11. Updating the Tuple
We cannot update the tuple as tuple are immutable as we cannot update or change the value or element of the tuple
tupledata = (1,2)
tupledata[0] = 3
print(tupledata)
OUTPUT
(1,2)