SlideShare a Scribd company logo
1 of 16
Developed
By
SANDHIYA R
Assistant Professor
Department of Computer Science and
Engineering
UNIT 4
LIST, TUPLE AND DICTIONARY
TUPLES
● Tuples are used to store multiple items in a single variable.
● It is a sequence of values separated by comma and enclosed within brackets.
● A tuple is a collection which is ordered and unchangeable.
● Tuple items are ordered, unchangeable and allow duplicate values.
EXAMPLE
Fruit = (‘apple’ , ‘banana’, ‘cherries’)
print(Fruit)
print(type(Fruit)
OUTPUT
(‘apple’ , ‘banana’, ‘cherries’)
<class ‘tuple’>
IMMUTABLE
CHANGE TUPLE VALUES, ADD ITEMS,
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x) // OUTPUT:(“kiwi”,”banana”,”cherry”)
ADD TUPLE TO TUPLE
● Allowed to add tuple to tuple by creating new tuple
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple) //OUTPUT: (‘apple’,’banana’,’cheery’,’orange’)
DELETING TUPLE ELEMENT
● Removing individual tuple element is not possible.
● Using del we can delete entire tuple
EXAMPLE
Fruit = ("apple", "banana", "cherry")
del Fruit
print(Fruit) #this will raise an error because the tuple no longer exists
BASIC TUPLE OPERATION
Some of the operation performed in tuples are:
Operation Example Output
Length Fruit = ("apple", "banana", "cherry")
print(len(Fruit))
3
Concatenation / join tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
('a', 'b', 'c', 1, 2, 3)
Repetition /
multiplying
fruits = ("apple", "banana", cherry")
Fruit = fruits * 2
print(Fruit)
​('apple', 'banana', 'cherry', 'apple',
'banana', 'cherry')
Membership 3 in (1,2,3) True
Iteration 1.for x in (1,2,3):
Print x
2. fruits = ("apple", "banana", cherry")
for i in range(len(Fruit)):
print(Fruit[i])
1 2 3
apple
banana
cherry
TUPLE METHOD
1. count() - Returns the number of times a specified value occur in a tuple
2. index() - Searches the tuple for a specified value and returns the position of where it
was found
3. max() - Returns item from the tuple with max value
4. min() - Returns item from th etuple with min value
5. tuple(seq) - Converts to tuple
Tuple Indexing and Tuple Slicing
● Forward indexing
● Reverse indexing
Slicing Syntax:
Tuple_name[start index : stop index : step index]
TUPLE ASSIGNMENT
● Multiple assignment can be done
● Number of objects on left hand side should be equal to number of objects on the right
side
EXAMPLE:
>>X,Y =1,2,3
Value Error: Too many values to unpack
TUPLE AS RETURN VALUES:
EXAMPLE:
def circleinfo(r)
c = 2*3,14*r
a = 3.14*r*r
return (c,a)
r = int(input(“Enter the radius:”))
print(circleinfo(r))
OUTPUT
Enter the radius: 10
(62.8318, 314.159)
VARIABLE LENGTH ARGUMENT TUPLES
1. def display(*args):
print(args)
Tup = (1,2,3,4,5,6)
display(Tup)
OUTPUT
((1,2,3,4,5,6),)
1. Tup = (56,3)
quo,rem = divmod(*Tup)
print(quo, rem)
OUTPUT
18 2
zip()
1. S = ‘abc’
T = [0,1,2]
zip(S,T)
for pair in zips(s,t)
print(pair)
OUTPUT:
(‘a’,0)
(‘b’,1)
(‘c’,2)
1. list(zip(s,t)
OUTPUT:
[(‘a’,0),(‘b’,1),(‘c’,2)]
3. list(zip(‘Anne’,’ElK’))
OUTPUT
[(‘A’,’E’),(‘n’,’l’),(‘n’,’k’)]
4. t = [(‘a’,0),(‘b’,1),(‘c’,2)]
for letter, number in t:
print(number, letter)
OUTPUT
0 a
1 b
2 c
4. for index, element in enumerate(‘abc):
print(index, element)
OUTPUT
0 a
1 b
2 c
DICTIONARIES AND TUPLE
1. d = {'a':0,'b':1,'c':2}
t = d.items()
print(t)
OUTPUT
dict_items([('a', 0), ('b', 1), ('c', 2)])
2. for key, value in d.items():
print(key, value)
OUTPUT
a 0
b 1
c 2
2. t=[('a',0),('c',2),('b',1)]
d = dict(t)
print(d)
OUTPUT
{'a': 0, 'c': 2, 'b': 1}
3. d = dict(zip('abc',range(3)))
print(d)
OUTPUT
{'a': 0, 'b': 1, 'c': 2}

More Related Content

Similar to List.ppt

Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
computer science tuple in python project.pdf
computer science tuple in python project.pdfcomputer science tuple in python project.pdf
computer science tuple in python project.pdfREXGAMING6
 
Decision control units by Python.pptx includes loop, If else, list, tuple and...
Decision control units by Python.pptx includes loop, If else, list, tuple and...Decision control units by Python.pptx includes loop, If else, list, tuple and...
Decision control units by Python.pptx includes loop, If else, list, tuple and...supriyasarkar38
 
Data Type In Python.pptx
Data Type In Python.pptxData Type In Python.pptx
Data Type In Python.pptxhothyfa
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfomprakashmeena48
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptxVGaneshKarthikeyan
 
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...KavineshKumarS
 
Python_Sets(1).pptx
Python_Sets(1).pptxPython_Sets(1).pptx
Python_Sets(1).pptxrishiabes
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionarynitamhaske
 
Effective tuples in phyton template.pptx
Effective tuples in phyton template.pptxEffective tuples in phyton template.pptx
Effective tuples in phyton template.pptxkarthimaathavan
 
Python Collections
Python CollectionsPython Collections
Python Collectionssachingarg0
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsSunil Kumar Gunasekaran
 

Similar to List.ppt (20)

Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
computer science tuple in python project.pdf
computer science tuple in python project.pdfcomputer science tuple in python project.pdf
computer science tuple in python project.pdf
 
Decision control units by Python.pptx includes loop, If else, list, tuple and...
Decision control units by Python.pptx includes loop, If else, list, tuple and...Decision control units by Python.pptx includes loop, If else, list, tuple and...
Decision control units by Python.pptx includes loop, If else, list, tuple and...
 
Tuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptxTuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptx
 
LIST_tuple.pptx
LIST_tuple.pptxLIST_tuple.pptx
LIST_tuple.pptx
 
updated_tuple_in_python.pdf
updated_tuple_in_python.pdfupdated_tuple_in_python.pdf
updated_tuple_in_python.pdf
 
Data Type In Python.pptx
Data Type In Python.pptxData Type In Python.pptx
Data Type In Python.pptx
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx
 
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
 
Python_Sets(1).pptx
Python_Sets(1).pptxPython_Sets(1).pptx
Python_Sets(1).pptx
 
Tuple.py
Tuple.pyTuple.py
Tuple.py
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Effective tuples in phyton template.pptx
Effective tuples in phyton template.pptxEffective tuples in phyton template.pptx
Effective tuples in phyton template.pptx
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
Python - Lecture 3
Python - Lecture 3Python - Lecture 3
Python - Lecture 3
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 

Recently uploaded

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscapingDr. M. Kumaresan Hort.
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 

Recently uploaded (20)

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 

List.ppt

  • 1. Developed By SANDHIYA R Assistant Professor Department of Computer Science and Engineering
  • 2. UNIT 4 LIST, TUPLE AND DICTIONARY
  • 3. TUPLES ● Tuples are used to store multiple items in a single variable. ● It is a sequence of values separated by comma and enclosed within brackets. ● A tuple is a collection which is ordered and unchangeable. ● Tuple items are ordered, unchangeable and allow duplicate values. EXAMPLE Fruit = (‘apple’ , ‘banana’, ‘cherries’) print(Fruit) print(type(Fruit) OUTPUT (‘apple’ , ‘banana’, ‘cherries’) <class ‘tuple’>
  • 4. IMMUTABLE CHANGE TUPLE VALUES, ADD ITEMS, x = ("apple", "banana", "cherry") y = list(x) y[1] = "kiwi" x = tuple(y) print(x) // OUTPUT:(“kiwi”,”banana”,”cherry”) ADD TUPLE TO TUPLE ● Allowed to add tuple to tuple by creating new tuple thistuple = ("apple", "banana", "cherry") y = ("orange",) thistuple += y print(thistuple) //OUTPUT: (‘apple’,’banana’,’cheery’,’orange’)
  • 5. DELETING TUPLE ELEMENT ● Removing individual tuple element is not possible. ● Using del we can delete entire tuple EXAMPLE Fruit = ("apple", "banana", "cherry") del Fruit print(Fruit) #this will raise an error because the tuple no longer exists
  • 6. BASIC TUPLE OPERATION Some of the operation performed in tuples are: Operation Example Output Length Fruit = ("apple", "banana", "cherry") print(len(Fruit)) 3 Concatenation / join tuple1 = ("a", "b" , "c") tuple2 = (1, 2, 3) tuple3 = tuple1 + tuple2 print(tuple3) ('a', 'b', 'c', 1, 2, 3) Repetition / multiplying fruits = ("apple", "banana", cherry") Fruit = fruits * 2 print(Fruit) ​('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry') Membership 3 in (1,2,3) True Iteration 1.for x in (1,2,3): Print x 2. fruits = ("apple", "banana", cherry") for i in range(len(Fruit)): print(Fruit[i]) 1 2 3 apple banana cherry
  • 7. TUPLE METHOD 1. count() - Returns the number of times a specified value occur in a tuple 2. index() - Searches the tuple for a specified value and returns the position of where it was found 3. max() - Returns item from the tuple with max value 4. min() - Returns item from th etuple with min value 5. tuple(seq) - Converts to tuple
  • 8. Tuple Indexing and Tuple Slicing ● Forward indexing ● Reverse indexing Slicing Syntax: Tuple_name[start index : stop index : step index]
  • 9. TUPLE ASSIGNMENT ● Multiple assignment can be done ● Number of objects on left hand side should be equal to number of objects on the right side EXAMPLE: >>X,Y =1,2,3 Value Error: Too many values to unpack
  • 10. TUPLE AS RETURN VALUES: EXAMPLE: def circleinfo(r) c = 2*3,14*r a = 3.14*r*r return (c,a) r = int(input(“Enter the radius:”)) print(circleinfo(r)) OUTPUT Enter the radius: 10 (62.8318, 314.159)
  • 11. VARIABLE LENGTH ARGUMENT TUPLES 1. def display(*args): print(args) Tup = (1,2,3,4,5,6) display(Tup) OUTPUT ((1,2,3,4,5,6),) 1. Tup = (56,3) quo,rem = divmod(*Tup) print(quo, rem) OUTPUT 18 2
  • 12. zip() 1. S = ‘abc’ T = [0,1,2] zip(S,T) for pair in zips(s,t) print(pair) OUTPUT: (‘a’,0) (‘b’,1) (‘c’,2) 1. list(zip(s,t) OUTPUT: [(‘a’,0),(‘b’,1),(‘c’,2)]
  • 13. 3. list(zip(‘Anne’,’ElK’)) OUTPUT [(‘A’,’E’),(‘n’,’l’),(‘n’,’k’)] 4. t = [(‘a’,0),(‘b’,1),(‘c’,2)] for letter, number in t: print(number, letter) OUTPUT 0 a 1 b 2 c
  • 14. 4. for index, element in enumerate(‘abc): print(index, element) OUTPUT 0 a 1 b 2 c
  • 15. DICTIONARIES AND TUPLE 1. d = {'a':0,'b':1,'c':2} t = d.items() print(t) OUTPUT dict_items([('a', 0), ('b', 1), ('c', 2)]) 2. for key, value in d.items(): print(key, value) OUTPUT a 0 b 1 c 2
  • 16. 2. t=[('a',0),('c',2),('b',1)] d = dict(t) print(d) OUTPUT {'a': 0, 'c': 2, 'b': 1} 3. d = dict(zip('abc',range(3))) print(d) OUTPUT {'a': 0, 'b': 1, 'c': 2}