TUPLE
Prepared By,
Prof. S. S. Gawali
Computer Engineering
WHAT IS TUPLE
▪ Tuples exactly same as list except that it is immutable.
▪ Tuples are used to store multiple items in a single variable.
▪ Once we create tuple object, We cannot perform any changes in that object.
▪ Tuple elements are represented within parenthesis and comma separator.
▪ When to prefer Tuple?
▪ If our data is fixed and never changes then we should go for Tuple.
PROPERTIES OF TUPLE
▪ Immutable
▪ Ordered data
▪ Duplicates are allowed
▪ Heterogeneous objects are allowed.
▪ Tuple support both +ve and –ve indexing
▪ () used to represent tuple
CREATION OF TUPLE
▪ Tuple elements are represented within parenthesis and comma separator.
▪ Syntax
▪ Var=(elements)
▪ Var=ele1,ele2,ele3
▪ Example:
t1=(1,2,3,4)
print(t1)
t2=3,'Hi',4.5
print(t2)
Output:
(1, 2, 3, 4)
(3, 'Hi', 4.5)
ACCESSING ELEMENTS IN
TUPLE
▪ In Python, there are two ways to access elements of Tuple.
1. By using index
2. By using slice operator
ACCESSING CHARACTERS
BY USING INDEX:
▪ In Python, individual Tuple element can be accessed by using the method of Indexing.
▪ Python support two types of index positive(+) and negative(-):
▪ Positive index: means left to right (forward direction)
▪ Negative index: means right to left (backward direction)
(10, 11, 12, 23, 14, 25, 16, 18, 19, 20)
0 1 2 3 4 5 6 7 8
9
+ve index
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -ve index
6
EXAMPLE 1: USING POSITIVE
INDEXING
t=(10,11,12,23,14,25,16,18,19,20)
print(t[7])
print(t[2])
Output:
18
12
7
EXAMPLE 2: USING
NEGATIVE INDEXING
t=(10,11,12,23,14,25,16,18,19,20)
print(t[-6])
print(t[-1])
Output:
14
20
8
ACCESSING ELEMENT USING
SLICE OPERATOR
t=(10,11,12,23,14,25,16,18,19,20)
print(t[2:9])
print(t[2:9:3])
print(t[-2:-8])
print(t[-8:-2])
print(t[:])
print(t[::])
print(t[::-1])
Output
(12, 23, 14, 25, 16, 18, 19)
(12, 25, 19)
()
(12, 23, 14, 25, 16, 18)
(10, 11, 12, 23, 14, 25, 16, 18, 19, 20)
(10, 11, 12, 23, 14, 25, 16, 18, 19, 20)
(20, 19, 18, 16, 25, 14, 23, 12, 11, 10)
ARITHMETICAL OPERATOR
FOR TUPLE
▪ + is used for concatenation/ join two tuple
▪ * is used for repetition of tuple
▪ += is used to append list with new tuple
▪ Example
t=(10,11,12,23,14,25,16,18,19,20)
t1=(4,5,6)
print(t1+t)
print(t1*5)
t1+=t
print(t1)
Output
(4, 5, 6, 10, 11, 12, 23, 14, 25, 16, 18, 19, 20)
(4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6)
(4, 5, 6, 10, 11, 12, 23, 14, 25, 16, 18, 19, 20)
10
Tuple Packing and Unpacking
Tuple packing refers to assigning multiple values into a tuple.
Example:
t=(2,3,4,5,6)
t1=1,2,3,4,5,6
Tuple unpacking refers to assigning a tuple into multiple variables.
Example:
t=(1,2,3,4)
a,b,c,d=t
len()
▪ This method used to returns number of elements in the Tuple.
▪ Example
t=(1,2,'swap','my’)
print(len(t))
Output
4
Computer Engineering PSP Prof. S. S.
Gawali 12
TRAVERSING TUPLE
ELEMENTS
▪ Using While Loop
▪ Forward Traversing:
t=(1,2,'swap','my’)
i=0
while i<len(t):
print(t[i])
i+=1
Output
1
2
swap
my
Computer Engineering PSP Prof. S. S.
Gawali 13
TRAVERSING TUPLE
ELEMENTS
▪ Using While Loop
▪ Backward Traversing:
t=(1,2,'swap',’my’)
i=-1
end=-len(t)-1
while i>end:
print(t[i])
i-=1
Output
my
swap
2
1
Computer Engineering PSP Prof. S. S.
Gawali 14
TRAVERSING TUPLE
ELEMENTS
▪ Using Membership Operator
T=(1,2,'swap',’my’)
for i in t:
print(i) Output
1
2
swap
my
Computer Engineering PSP Prof. S. S.
Gawali 15
min(), max() and sum()
▪ min() is used to return minimum of all the elements of Tuple.
▪ Syntax:
min(Tuple)
▪ max() is used to return maximum of all the elements of Tuple.
▪ Syntax:
max(Tuple)
▪ sum() is used to return sum of all elements of Tuple.
▪ Syntax:
sum(Tuple)
Computer Engineering PSP Prof. S. S.
Gawali 16
EXAMPLE
t=(1,2,3,6,102,456,899)
print("Minimum element in Tuple is ",min(t))
print("Maximum element in Tuple is ",max(t))
print("Sum of all elements in Tuple is ",sum(t))
Output
Minimum element in list is 1
Maximum element in list is 899
Sum of all elements in list is 1469
Computer Engineering PSP Prof. S. S.
Gawali 17
sorted()
▪ sorted() returns new sorted Tuple.
▪ The original Tuple is not sorted.
▪ Syntax:
sorted(Tuple)
▪ Example
t=(102,1,11,456,36,788,99)
print(sorted(t))
print(t)
Output:
[1, 11, 36, 99, 102, 456, 788]
[102, 1, 11, 456, 36, 788, 99]
Computer Engineering PSP Prof. S. S.
Gawali 18
METHOD FOR TUPLE
▪ count()
▪ index()
count()
▪ Used to calculates total occurrence of a given element of Tuple.
▪ Syntax:
▪ Tuple.count(element)
▪ Example
t1=('bye','hi','welcome','hello','bye')
c=t1.count('bye')
print(f"Bye occurs {c} times") Output
Bye occurs 2 times
Computer Engineering PSP Prof. S. S.
Gawali 20
index()
▪ Returns the index of the first occurrence. The start and End index are not necessary
parameters.
▪ Syntax:
▪ Tuple.index(element[,start[,end]])
▪ Example
t1=('bye','hi','welcome','hello','bye')
i=t1.index('bye')
ii=t1.index('bye',2,len(t1))
print(f"bye present at {i} index")
print(f"bye present at {ii} index")
Output
bye present at 0 index
bye present at 4 index
Computer Engineering PSP Prof. S. S.
Gawali 21
THANK YOU…

Unit-4 Basic Concepts of Tuple in Python .pptx

  • 1.
    TUPLE Prepared By, Prof. S.S. Gawali Computer Engineering
  • 2.
    WHAT IS TUPLE ▪Tuples exactly same as list except that it is immutable. ▪ Tuples are used to store multiple items in a single variable. ▪ Once we create tuple object, We cannot perform any changes in that object. ▪ Tuple elements are represented within parenthesis and comma separator. ▪ When to prefer Tuple? ▪ If our data is fixed and never changes then we should go for Tuple.
  • 3.
    PROPERTIES OF TUPLE ▪Immutable ▪ Ordered data ▪ Duplicates are allowed ▪ Heterogeneous objects are allowed. ▪ Tuple support both +ve and –ve indexing ▪ () used to represent tuple
  • 4.
    CREATION OF TUPLE ▪Tuple elements are represented within parenthesis and comma separator. ▪ Syntax ▪ Var=(elements) ▪ Var=ele1,ele2,ele3 ▪ Example: t1=(1,2,3,4) print(t1) t2=3,'Hi',4.5 print(t2) Output: (1, 2, 3, 4) (3, 'Hi', 4.5)
  • 5.
    ACCESSING ELEMENTS IN TUPLE ▪In Python, there are two ways to access elements of Tuple. 1. By using index 2. By using slice operator
  • 6.
    ACCESSING CHARACTERS BY USINGINDEX: ▪ In Python, individual Tuple element can be accessed by using the method of Indexing. ▪ Python support two types of index positive(+) and negative(-): ▪ Positive index: means left to right (forward direction) ▪ Negative index: means right to left (backward direction) (10, 11, 12, 23, 14, 25, 16, 18, 19, 20) 0 1 2 3 4 5 6 7 8 9 +ve index -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -ve index 6
  • 7.
    EXAMPLE 1: USINGPOSITIVE INDEXING t=(10,11,12,23,14,25,16,18,19,20) print(t[7]) print(t[2]) Output: 18 12 7
  • 8.
    EXAMPLE 2: USING NEGATIVEINDEXING t=(10,11,12,23,14,25,16,18,19,20) print(t[-6]) print(t[-1]) Output: 14 20 8
  • 9.
    ACCESSING ELEMENT USING SLICEOPERATOR t=(10,11,12,23,14,25,16,18,19,20) print(t[2:9]) print(t[2:9:3]) print(t[-2:-8]) print(t[-8:-2]) print(t[:]) print(t[::]) print(t[::-1]) Output (12, 23, 14, 25, 16, 18, 19) (12, 25, 19) () (12, 23, 14, 25, 16, 18) (10, 11, 12, 23, 14, 25, 16, 18, 19, 20) (10, 11, 12, 23, 14, 25, 16, 18, 19, 20) (20, 19, 18, 16, 25, 14, 23, 12, 11, 10)
  • 10.
    ARITHMETICAL OPERATOR FOR TUPLE ▪+ is used for concatenation/ join two tuple ▪ * is used for repetition of tuple ▪ += is used to append list with new tuple ▪ Example t=(10,11,12,23,14,25,16,18,19,20) t1=(4,5,6) print(t1+t) print(t1*5) t1+=t print(t1) Output (4, 5, 6, 10, 11, 12, 23, 14, 25, 16, 18, 19, 20) (4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6) (4, 5, 6, 10, 11, 12, 23, 14, 25, 16, 18, 19, 20) 10
  • 11.
    Tuple Packing andUnpacking Tuple packing refers to assigning multiple values into a tuple. Example: t=(2,3,4,5,6) t1=1,2,3,4,5,6 Tuple unpacking refers to assigning a tuple into multiple variables. Example: t=(1,2,3,4) a,b,c,d=t
  • 12.
    len() ▪ This methodused to returns number of elements in the Tuple. ▪ Example t=(1,2,'swap','my’) print(len(t)) Output 4 Computer Engineering PSP Prof. S. S. Gawali 12
  • 13.
    TRAVERSING TUPLE ELEMENTS ▪ UsingWhile Loop ▪ Forward Traversing: t=(1,2,'swap','my’) i=0 while i<len(t): print(t[i]) i+=1 Output 1 2 swap my Computer Engineering PSP Prof. S. S. Gawali 13
  • 14.
    TRAVERSING TUPLE ELEMENTS ▪ UsingWhile Loop ▪ Backward Traversing: t=(1,2,'swap',’my’) i=-1 end=-len(t)-1 while i>end: print(t[i]) i-=1 Output my swap 2 1 Computer Engineering PSP Prof. S. S. Gawali 14
  • 15.
    TRAVERSING TUPLE ELEMENTS ▪ UsingMembership Operator T=(1,2,'swap',’my’) for i in t: print(i) Output 1 2 swap my Computer Engineering PSP Prof. S. S. Gawali 15
  • 16.
    min(), max() andsum() ▪ min() is used to return minimum of all the elements of Tuple. ▪ Syntax: min(Tuple) ▪ max() is used to return maximum of all the elements of Tuple. ▪ Syntax: max(Tuple) ▪ sum() is used to return sum of all elements of Tuple. ▪ Syntax: sum(Tuple) Computer Engineering PSP Prof. S. S. Gawali 16
  • 17.
    EXAMPLE t=(1,2,3,6,102,456,899) print("Minimum element inTuple is ",min(t)) print("Maximum element in Tuple is ",max(t)) print("Sum of all elements in Tuple is ",sum(t)) Output Minimum element in list is 1 Maximum element in list is 899 Sum of all elements in list is 1469 Computer Engineering PSP Prof. S. S. Gawali 17
  • 18.
    sorted() ▪ sorted() returnsnew sorted Tuple. ▪ The original Tuple is not sorted. ▪ Syntax: sorted(Tuple) ▪ Example t=(102,1,11,456,36,788,99) print(sorted(t)) print(t) Output: [1, 11, 36, 99, 102, 456, 788] [102, 1, 11, 456, 36, 788, 99] Computer Engineering PSP Prof. S. S. Gawali 18
  • 19.
    METHOD FOR TUPLE ▪count() ▪ index()
  • 20.
    count() ▪ Used tocalculates total occurrence of a given element of Tuple. ▪ Syntax: ▪ Tuple.count(element) ▪ Example t1=('bye','hi','welcome','hello','bye') c=t1.count('bye') print(f"Bye occurs {c} times") Output Bye occurs 2 times Computer Engineering PSP Prof. S. S. Gawali 20
  • 21.
    index() ▪ Returns theindex of the first occurrence. The start and End index are not necessary parameters. ▪ Syntax: ▪ Tuple.index(element[,start[,end]]) ▪ Example t1=('bye','hi','welcome','hello','bye') i=t1.index('bye') ii=t1.index('bye',2,len(t1)) print(f"bye present at {i} index") print(f"bye present at {ii} index") Output bye present at 0 index bye present at 4 index Computer Engineering PSP Prof. S. S. Gawali 21
  • 22.