Unit-4 Basic Concepts of Tuple in Python .pptx

SwapnaliGawali5 193 views 22 slides May 27, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Refer this to understand Python Tuple concepts in detail.


Slide Content

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. By using index 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 ‹#›

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 ‹#›

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 ‹#›

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) ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

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 ‹#›

THANK YOU…
Tags