Data Science – Python Tuple Data Structure
16. 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
1|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
16. Python – Tuple Data Structure
1. Tuple data Structure
✓ We can create tuple data structure by using,
o Parenthesis () symbol.
o Predefined tuple(p) function.
✓ A tuple can store group of objects or elements.
o A tuple can store same (Homogeneous) type of elements.
o A tuple can store different (Heterogeneous) type of elements.
✓ In tuple insertion order is preserved or fixed.
o If we insert elements into 10, 20, 30 then output also will display
as 10, 20, 30 then this is called as insertion order is preserved or
fixed
o Example
▪ Input => (10, 20, 30)
▪ Output => (10, 20, 30)
✓ Duplicate elements are allowed.
✓ Tuple having immutable nature.
o Immutable means once we create a tuple object then we cannot
change or modify the content of tuple object.
✓ Store elements by using index.
o A tuple data structure supports both positive and negative
indexes.
o Positive index means from left to right
o Negative index means right to left
Note:
✓ tuple is a predefined class in python.
✓ Once if we create tuple object means internally object is creating for
tuple class.
2|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
Note:
✓ Inside tuple every object can be separated by comma separator.
2. When should we go for tuple data structure?
✓ If we are going to define a data which never change over all the period,
then we should go for tuple data structure.
Example:
1. Week days names
2. Month names
3. Year names
Program Tuple having same type of objects
Name [Link]
employee_ids = (10, 20, 30, 40, 50)
print(employee_ids)
print(type(employee_ids))
Output
(10, 20, 30, 40, 50)
<class 'tuple'>
3|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
3. Syntax Surprise 1: Single value 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 [Link]
number = (9)
print(number)
print(type(number))
Output
(9)
<class ‘int’>
Program A single value with tuple syntax, but it’s not tuple
Name [Link]
name = ("Daniel")
print(name)
print(type(name))
Output
Daniel
<class ‘str’>
4|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
Program Tuple single value ends with comma separator then it’s tuple
Name [Link]
name = ("Daniel", )
print(name)
print(type(name))
Output
('Daniel')
<class 'tuple'>
4. Syntax Surprise 2. Parenthesis is optional for tuple
✓ While creating a tuple parenthesis is optional
Program Parenthesis symbol is optional while creating tuple
Name [Link]
emp_ids = 10, 20, 30, 40
print(emp_ids)
output
(10, 20, 30, 40)
5|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
5. Different ways to create a tuple
1. Empty tuple
✓ We can create an empty tuple by using empty parenthesis.
Program empty tuple
Name [Link]
emp_id = ()
print(emp_id)
print(type(emp_id))
output
()
<class 'tuple'>
2. Tuple with group of values
✓ Tuple can contain group of objects; those objects can be same type or
different type.
Program Tuple example
Name [Link]
emp_id = (11, 12, 13)
std_id = 120, 130, 140
print(emp_id)
print(std_id)
output
(11, 12, 13)
(120, 130, 140)
6|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
Program Tuple example
Name [Link]
t = (11, 12, 13, "daniel")
print(t)
output
(11, 12, 13, "daniel")
3. By using tuple(p) function
✓ We can create tuple by using tuple(p) function.
Program Creating tuple by using tuple function
Name [Link]
a = [11, 22, 33]
t = tuple(a)
print(t)
output
(11, 22, 33)
7|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
6. Accessing elements of tuple:
✓ We can access tuple elements by using,
o Index
o Slice operator
6.1 Index
✓ Index means position where element stores
Program Accessing tuple by using index
Name [Link]
t = (10, 20, 30, 40, 50, 60)
print(t[0]) # 10
print(t[-1]) # 60
Output
10
60
8|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
6.2. Slice operator:
✓ A group of objects from starting point to ending point
Program Accessing tuple by using slice
Name [Link]
t = (10, 20, 30, 40, 50, 60)
print(t[2:5])
print(t[2:100])
print(t[::2])
Output
30, 40, 50)
(30, 40, 50, 60)
(10, 30, 50)
9|Page [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
7. Tuple vs immutability:
✓ Tuple having immutable nature.
✓ If we create a tuple then we cannot modify the elements of existing
tuple.
Program Prove tuple having immutable nature
Name [Link]
t = (10, 20, 30, 40)
print(t[1])
t[1] = 70
Output
20
TypeError: 'tuple' object does not support item assignment
10 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
8. Mathematical operators on tuple:
✓ We can apply plus (+) and Multiplication (*) operators on tuple.
✓ + Operator works as concatenation.
✓ * Operator works as multiplication.
8.1. Concatenation operator (+):
✓ + operator concatenates two tuples and returns single tuple
Program Concatenation operator on tuple
Name [Link]
t1 = (10,20,30)
t2 = (40,50,60)
t3 = t1 + t2
print(t3)
Output
(10, 20, 30, 40, 50, 60)
11 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
8.2 Multiplication operator (*)
✓ Multiplication operator works as repetition operator
Program Repetition operator on tuple
Name [Link]
t1 = (10,20,30)
t2 = t1*3
print(t2)
Output
(10, 20, 30, 10, 20, 30, 10, 20, 30)
9. len(p) function
✓ To return number of elements present in the tuple
Program len(p) function
Name [Link]
t = (10,20,30,40)
print(len(t))
Output
12 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
10. Method in tuple data structure
✓ As discussed, tuple is a predefined class.
✓ So, tuple class can contain methods because methods can be created
inside of class only.
✓ We can check these methods by using dir(p) predefined function.
✓ So, internally tuple class contains two types of methods,
o With underscore symbol methods.
▪ We no need to focus
o Without underscore symbol methods.
▪ We need to focus much on these
Program Printing tuple data structure methods by using dir(p) function
Name [Link]
print(dir(tuple))
output
[
'__add__’, ..... , '__subclasshook__',
'count', 'index'
13 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
Important point
✓ As per object-oriented principle,
o If we want to access instance method then we should access by
using object name.
✓ So, all tuple methods we can access by using tuple object.
Methods in tuple
1. count(parameter1) method
2. index(parameter1) method
10.1. count(p) method
✓ count(p) is a method, we should access this method by using tuple
object.
✓ This method returns the number of occurrences of specified item in the
tuple
Program count(p) method
Name [Link]
t = (10, 20, 10, 10, 20)
print([Link](10))
output
3
14 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
10.2. index(p) method
✓ returns index of first occurrence of the given element.
✓ If the specified element is not available, then we will get ValueError.
Program index(p) method
Name [Link]
t = (10, 20, 30)
print([Link](30))
Output
2
Program index(p) method
Name [Link]
t = (10, 20, 30)
print([Link](88))
Output
ValueError: [Link](x): x not in tuple
15 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
12. Differences between List and Tuple:
✓ List and Tuple are exactly same except small difference:
o List objects are mutable
o Tuple objects are immutable.
✓ In both cases,
o Insertion order is preserved.
o Duplicate objects are allowed
o Heterogeneous objects are allowed
o Index and slicing are supported.
List Tuple
✓ List is a Group of Comma ✓ Tuple is a Group of Comma
separated Values within Square separated Values within
Brackets and Square Brackets Parenthesis and Parenthesis
are mandatory. are optional.
✓ Example: i = [10, 20, 30, 40] ✓ Example: t = (10, 20, 30, 40)
✓ Example: t = 10, 20, 30, 40
✓ List Objects are Mutable i.e. ✓ Tuple Objects are Immutable
once we create List object we i.e. once we create Tuple
can perform any changes in object we cannot change its
that Object. content.
✓ Example: i[1] = 70 ✓ Example: t [1] = 70
✓ TypeError: tuple object does
not support item assignment.
✓ If the Content is not fixed and ✓ If the content is fixed and
keep on changing, then we never changes then we should
should go for List. go for Tuple.
16 | P a g e [Link] Tuple Data Structure
Data Science – Python Tuple Data Structure
13. Can I add elements to this tuple t = (11, 22, [33, 44], 55, 66)?
✓ Yes we can add elements to list in tuple.
✓ In second index position list is available, to that we can add
Program tuple data structure can store any data
Name [Link]
t = (11, 22, [33, 44], 55, 66)
t[2].append(77)
print(t)
output
(11, 22, [33, 44, 77], 55, 66)
17 | P a g e [Link] Tuple Data Structure