Python - Tuple Data Structure
Python - Tuple Data Structure
Table of Contents
1. Tuple data Structure........................................................................................................................... 2
2. When should we go for tuple data structure? .................................................................................. 3
3. Syntax Surprise 1: Single value tuple ................................................................................................. 4
4. Syntax Surprise 2. Parenthesis is optional for tuple ......................................................................... 5
5. Different ways to create a tuple ........................................................................................................ 6
6. Accessing elements of tuple: ............................................................................................................. 8
6.1 Index.............................................................................................................................................. 8
6.2. Slice operator: .............................................................................................................................. 9
7. Tuple vs immutability:...................................................................................................................... 10
8. Mathematical operators on tuple: .................................................................................................. 11
8.1. Concatenation operator (+): ...................................................................................................... 11
8.2 Multiplication operator (*) ......................................................................................................... 12
9. len(p) function .................................................................................................................................. 12
10. Method in tuple data structure ..................................................................................................... 13
10.1. count(p) method ..................................................................................................................... 14
10.2. index(p) method ...................................................................................................................... 15
12. Differences between List and Tuple: ............................................................................................. 16
13. Can I add elements to this tuple t = (11, 22, [33, 44], 55, 66)? ..................................................... 17
Note:
Note:
✓ Inside tuple every object can be separated by comma separator.
✓ If we are going to define a data which never change over all the period,
then we should go for tuple data structure.
Example:
Output
(10, 20, 30, 40, 50)
<class 'tuple'>
✓ If tuple having only one object, then that object should end with comma
separator otherwise python internally not considered as it is tuple.
Program A single value with tuple syntax, but it’s not tuple
Name demo2.py
number = (9)
print(number)
print(type(number))
Output
(9)
<class ‘int’>
Program A single value with tuple syntax, but it’s not tuple
Name demo3.py
name = ("Daniel")
print(name)
print(type(name))
Output
Daniel
<class ‘str’>
Program Tuple single value ends with comma separator then it’s tuple
Name demo4.py
name = ("Daniel", )
print(name)
print(type(name))
Output
('Daniel')
<class 'tuple'>
output
(10, 20, 30, 40)
1. Empty tuple
emp_id = ()
print(emp_id)
print(type(emp_id))
output
()
<class 'tuple'>
✓ Tuple can contain group of objects; those objects can be same type or
different type.
output
output
output
(11, 22, 33)
6.1 Index
print(t[0]) # 10
print(t[-1]) # 60
Output
10
60
print(t[2:5])
print(t[2:100])
print(t[::2])
Output
7. Tuple vs immutability:
Output
20
TypeError: 'tuple' object does not support item assignment
t1 = (10,20,30)
t2 = (40,50,60)
t3 = t1 + t2
print(t3)
Output
t1 = (10,20,30)
t2 = t1*3
print(t2)
Output
(10, 20, 30, 10, 20, 30, 10, 20, 30)
9. len(p) function
t = (10,20,30,40)
print(len(t))
Output
print(dir(tuple))
output
[
'count', 'index'
Important point
Methods in tuple
1. count(parameter1) method
2. index(parameter1) method
output
3
Output
2
Output
ValueError: tuple.index(x): x not in tuple
List Tuple
13. Can I add elements to this tuple t = (11, 22, [33, 44], 55, 66)?
t[2].append(77)
print(t)
output
(11, 22, [33, 44, 77], 55, 66)