SlideShare una empresa de Scribd logo
1 de 24
Python Programming-Part8
Megha V
Research Scholar
Dept of IT
Kannur University
Tuples
• Sequence data type
• Tuples are enclosed in parentheses ()
• The values can not be updated
• Tuples can be considered as read-only lists
Tuples
• Example:
first_tuple = (‘abcd’,147,2.43,’Tom’,74.9)
small_tuple = (111,’Tom’)
print(first_tuple) #Prints complete tuple
print(first_tuple[0]) #Prints first element of the tuple
print(first_tuple[1:3]) #Prints elements starting from 2nd till 3rd
print(first_tuple[2:]) # Prints elements starting from 3rd element
print(small_tuple*2) # Prints tuples 2 times
print(first_tuple+small_tuple) # Prints concatenated tuple
Output
(‘abcd’,147,2.43,’Tom’,74.9)
abcd
(147,2.43)
(2.43,’Tom’,74.9)
(111,’Tom’,111,’Tom’)
(‘abcd’,147,2.43,’Tom’,74.9(111,’Tom’)
Deleting Tuple
• To delete an entire tuple we can use the del statement
• Example: It is not possible to remove individual items from a tuple
• It is possible to create tuples which contain mutable objects, such as lists
Example:
tuple1=([1,2,3],[‘apple’,’pear’,’orange’])
print(tuple1)
del tuple1
Output
t=([1,2,3],[‘apple’,’pear’,’orange’])
Tuples
• It is possible to pack values to a tuple and unpack values from a tuple
• We can create tuples without parenthesis
• The reverse operation is called sequence unpacking
• Sequence unpacking requires that there are as many variable on the
left side of the equal sign as there are elements in the sequence.
Tuples
Example:
t= “apple”,1,100
print(t)
x,y,z=t
print(x)
print(x)
print(x)
Output
(‘apple’,1,100)
apple
1
100
Built-in Tuple functions
1. len(tuple)- Gives the total lenghth of the tuple
tuple1=(‘abcd’,147,2.43,’Tom’)
print(len(tuple)) #4
2. max(tuple)- Returns item from the tuple with maximum value
tuple1=(1200,147,2.43,1.12)
print(“Maximum value in tuple1 is:”,max(tuple1))
Output
Maximum value in tuple1 is 1200
3. min(tuple) – Returns item from tuple with minimum value
print(“Minimum value in tuple1 is:”,min(tuple1))
Output
Minimum value in tuple1 is 1.12
Built-in Tuple functions
4. tuple(seq) – Returns a converted tuple from list
list=[‘abcd’,147,2.43,’Tom’]
print(“Tuple:”,tuple(list))
Output
Tuple:(‘abcd’,147,2.43,’Tom’)
Set
• Unordered collection of unique items
• Set is defined by values separated by comma inside braces{}
• It can have any number of items and they may be of different types
(integer, float, tuple, string etc)
• We can not change or access an item using indexing or slicing
• We can perform set operations like union, intersection, difference, on
two sets.
• Set have unique values, eliminate duplicates
• Empty set is created by the function set()
Set
Example
s1={1,2,3}
print(s1)
s2={1,2,3,2,1,2} #output will contain only unique values
print(s2)
s3={1,2.4,’apple’,’Tom’,3} #set of mixed data types
print(s3)
#s4={1,2,[3,4]} # set can not have mutable items
#print(s4) #hence not permitted
s5=set([1,2,3,4]) # using set function to create set from list
print(s5)
Output
{1,2,3}
{1,2,3}
{1,3,2.4,’apple’,’Tom’}
{1,2,3,4}
Built-in set functions
1. len(set) – Returns length or total number of items in a set
set1={‘abcd’,147,2.43,’Tom’}
print(len(set1)) #4
2. max(set) – Returns item from set with maximum value
set1={1200,1.12,300,2.43,147}
print(“Maximum value is:”,max(set1))
Output
Maximum value is:1200
3. min (set) – Returns item with minimum value
print(“Minimum value is:”,min(set1))
Output
Minimum value is:1.12
Built-in set functions
4. sum (set) – Returns the sum of all item in the set
set1={147,2.43}
print(“Sum of elements in”,set1,”is”,sum(set1))
Output
Sum of elements in {147,2.43} is 149.23
5. sorted (set) – Returns a new sorted list.
set1={213,100,289,40,23,1,1000}
set2=sorted(set1)
print(“Elements before sorting:”,set1)
print(“Elements after sorting:”,set2)
Output
Elements before sorting:{213,100,289,40,23,1,1000}
Elements after sorting:{1,23,40,100,213,289,1000}
Built-in set functions
6. enumerate(set) – Returns an enumerate object.
It contains the index and value of all the items of set as a pair
set1={213,100,289,40,23,1,1000}
print(“enumerate(set):”,enumerate(set1))
Output
enumerate(set): <enumerate object at 0x00F75728>
7. any(set) – Returns True, if the set contains at least one item. Otherwise returns False
set1=set()
set2={1,2,3,4}
print(“any (set):”,any(set1))
print(“any (set):”,any(set2))
Output
any(set): False
any(set):True
Built-in set functions
8. all(set) – Returns True, if all the elements are true or the set is empty
set1=set()
set2={1,2,3,4}
print(“all(set):”,all(set1))
print(“all(set):”,all(set2))
Output
all(set):True
all(set):True
Built-in set methods
1. set.add(obj) – Adds an element obj to a set
set1={3,8,2,6}
set1.add(9)
print(set1) # {8,9,2,3,6}
2. set.remove(obj) – Removes an element obj from set.
Raise an error if the set is empty
set1={3,8,2,6}
set1.remove(8)
print(set1) # {2,3,6}
Built-in set methods
3. set.discard(obj) – Removes an item obj from set.
Nothing happens if the element to be deleted is not
present
set1={3,8,2,6}
set1.discard(8)
set1.discard(10)
4. set.pop() – Removes an returns an arbitrary set element.
Raise Key error if set is empty
set1={3,8,2,6}
set1.pop()
print(“set after poping:”,set1) # {2,3,6}
Built-in set methods
5. set1.union(set2) – Returns the union of two sets as a new set
set1={3,8,2,6}
set2={4,2,1,9}
set3=set1.union(set2) #unique values will be taken
print(set3) # {1,2,3,4,6,8,9}
6. set1.update (set2) – Update a set with the union of itself and others. The
result will be sorted in set1
set1={3,8,2,6}
set2={4,2,1,9}
set1.update(set2)
print(set1) # {1,2,3,4,6,8,9}
Built-in set methods
7. set1.intersection(set2) – Returns the intersection of two sets as a
new set
set1={3,8,2,6}
set2={4,2,1,9}
set3=set1.intersection(set2
print(set3) # {2}
8. set1.intersection_update() – Update the set with the intersection of
itself and another.
result will be stored in set1
set1={3,8,2,6}
set2={4,2,1,9}
set1.intersection_update(set2)
print(set1) # {8,2,3,6}
Built-in set methods
9. set1.difference(set2) – Returns the difference of two or more sets into a new set
set1={3,8,2,6}
set2={4,2,1,9}
set3=set1.difference(set2)
print(set3) # {8,3,6}
10. set1.difference_update() – Removes all elements of another set set2 from set1
and the result is stored in set1
set1={3,8,2,6}
set2={4,2,1,9}
set1.difference_update(set2)
print(set1) # {8,3,6}
Built-in set methods
11. set1.symmetric_difference(set2)- Return the symmetric difference of two sets
as a new set.
set1={3,8,2,6}
set2={4,2,1,9}
set3=set1.symmetric_difference(set2)
print(set3) # {1,3,4,6,8,9}
12. set1.difference_update(set2)- Update a set with the symmetric difference of
itself and another
set1={3,8,2,6}
set2={4,2,1,9}
set1.symmetric_difference_update(set2)
print(set1) # {1,3,4,6,8,9}
Built-in set methods
13.set1.isdisjoint(set2) – Returns True if two sets have a null intersection
set1={3,8,2,6}
set2={4,7,1,9}
print(“Result of set1.isdisjoint(set2):”,set1.isdisjoint(set2))
Output
Result of set1.isdisjoint(set2): True
14. set1.issubset(set2) – Returns True if set1 is a subset of set2
set1={3,8}
set2={38,4,7,1,9}
print(“Result of set1.issubset(set2):”,set1.issubset(set2))
Output
Result of set1.issubset(set2): True
Built-in set methods
15. set1.issuperset(set2) – Returns True, if set1 is a super set of set2
set1={3,8,4,6}
set2={3,8}
print(“Result of set1.issuperset(set2):”,set1.issuperset(set2))
Output
Result of set1.issuperset(set2): True
Frozenset
• Frozenset is a new class that has the characteristics of a set
• Its elements cannot be changed once assigned
• Frozensets are immutable sets
• Frozenset are hashable and can be used as keys to a dictionary
• Frozensets are creates by the function frozenset()
• Supports methods like difference(), intersection(), isdisjoint(),
issubset(), issuperset(), symmetric_difference() and union()
• Being immutable it does not have methods like add(), remove(),
update(), difference_update(),
intersection_update(),bsymmetric_difference_update() etc.
Frozen set
Example:
set1= frozenset({3,8,4,6})
print(“Set 1:”,set1)
set2=frozenset({3,8})
print(“Set 2:”,set2)
print(“Result of set1.intersection(set2):”,set1.intersection(set2))
Output
Set1: frozenset({8,3,4,6})
Set 2:frozenset({8,3})
Result of set1.intersection(set2): frozenset({8,3})

Más contenido relacionado

La actualidad más candente (20)

Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python list
Python listPython list
Python list
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Sets in python
Sets in pythonSets in python
Sets in python
 
STL in C++
STL in C++STL in C++
STL in C++
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
python Function
python Function python Function
python Function
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Namespaces
NamespacesNamespaces
Namespaces
 
Python functions
Python functionsPython functions
Python functions
 
Python set
Python setPython set
Python set
 
Python list
Python listPython list
Python list
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
List in Python
List in PythonList in Python
List in Python
 

Similar a Python programming -Tuple and Set Data type

Similar a Python programming -Tuple and Set Data type (20)

PRESENTATION ON TUPLES.pptx
PRESENTATION ON TUPLES.pptxPRESENTATION ON TUPLES.pptx
PRESENTATION ON TUPLES.pptx
 
Python data structures
Python data structuresPython data structures
Python data structures
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Set data structure
Set data structure Set data structure
Set data structure
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.doc
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
Python PCEP Lists Collections of Data
Python PCEP Lists Collections of DataPython PCEP Lists Collections of Data
Python PCEP Lists Collections of Data
 
Python-Tuples
Python-TuplesPython-Tuples
Python-Tuples
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
arraycreation.pptx
arraycreation.pptxarraycreation.pptx
arraycreation.pptx
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 

Más de Megha V

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxMegha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxMegha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMegha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expressionMegha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7Megha V
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsMegha V
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Megha V
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming languageMegha V
 
Python programming
Python programmingPython programming
Python programmingMegha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplicationMegha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm AnalysisMegha V
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmMegha V
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data Megha V
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGLMegha V
 
Msc project_CDS Automation
Msc project_CDS AutomationMsc project_CDS Automation
Msc project_CDS AutomationMegha V
 

Más de Megha V (20)

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
Python programming
Python programmingPython programming
Python programming
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
 
Msc project_CDS Automation
Msc project_CDS AutomationMsc project_CDS Automation
Msc project_CDS Automation
 

Último

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Último (20)

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Python programming -Tuple and Set Data type

  • 1. Python Programming-Part8 Megha V Research Scholar Dept of IT Kannur University
  • 2. Tuples • Sequence data type • Tuples are enclosed in parentheses () • The values can not be updated • Tuples can be considered as read-only lists
  • 3. Tuples • Example: first_tuple = (‘abcd’,147,2.43,’Tom’,74.9) small_tuple = (111,’Tom’) print(first_tuple) #Prints complete tuple print(first_tuple[0]) #Prints first element of the tuple print(first_tuple[1:3]) #Prints elements starting from 2nd till 3rd print(first_tuple[2:]) # Prints elements starting from 3rd element print(small_tuple*2) # Prints tuples 2 times print(first_tuple+small_tuple) # Prints concatenated tuple Output (‘abcd’,147,2.43,’Tom’,74.9) abcd (147,2.43) (2.43,’Tom’,74.9) (111,’Tom’,111,’Tom’) (‘abcd’,147,2.43,’Tom’,74.9(111,’Tom’)
  • 4. Deleting Tuple • To delete an entire tuple we can use the del statement • Example: It is not possible to remove individual items from a tuple • It is possible to create tuples which contain mutable objects, such as lists Example: tuple1=([1,2,3],[‘apple’,’pear’,’orange’]) print(tuple1) del tuple1 Output t=([1,2,3],[‘apple’,’pear’,’orange’])
  • 5. Tuples • It is possible to pack values to a tuple and unpack values from a tuple • We can create tuples without parenthesis • The reverse operation is called sequence unpacking • Sequence unpacking requires that there are as many variable on the left side of the equal sign as there are elements in the sequence.
  • 7. Built-in Tuple functions 1. len(tuple)- Gives the total lenghth of the tuple tuple1=(‘abcd’,147,2.43,’Tom’) print(len(tuple)) #4 2. max(tuple)- Returns item from the tuple with maximum value tuple1=(1200,147,2.43,1.12) print(“Maximum value in tuple1 is:”,max(tuple1)) Output Maximum value in tuple1 is 1200 3. min(tuple) – Returns item from tuple with minimum value print(“Minimum value in tuple1 is:”,min(tuple1)) Output Minimum value in tuple1 is 1.12
  • 8. Built-in Tuple functions 4. tuple(seq) – Returns a converted tuple from list list=[‘abcd’,147,2.43,’Tom’] print(“Tuple:”,tuple(list)) Output Tuple:(‘abcd’,147,2.43,’Tom’)
  • 9. Set • Unordered collection of unique items • Set is defined by values separated by comma inside braces{} • It can have any number of items and they may be of different types (integer, float, tuple, string etc) • We can not change or access an item using indexing or slicing • We can perform set operations like union, intersection, difference, on two sets. • Set have unique values, eliminate duplicates • Empty set is created by the function set()
  • 10. Set Example s1={1,2,3} print(s1) s2={1,2,3,2,1,2} #output will contain only unique values print(s2) s3={1,2.4,’apple’,’Tom’,3} #set of mixed data types print(s3) #s4={1,2,[3,4]} # set can not have mutable items #print(s4) #hence not permitted s5=set([1,2,3,4]) # using set function to create set from list print(s5) Output {1,2,3} {1,2,3} {1,3,2.4,’apple’,’Tom’} {1,2,3,4}
  • 11. Built-in set functions 1. len(set) – Returns length or total number of items in a set set1={‘abcd’,147,2.43,’Tom’} print(len(set1)) #4 2. max(set) – Returns item from set with maximum value set1={1200,1.12,300,2.43,147} print(“Maximum value is:”,max(set1)) Output Maximum value is:1200 3. min (set) – Returns item with minimum value print(“Minimum value is:”,min(set1)) Output Minimum value is:1.12
  • 12. Built-in set functions 4. sum (set) – Returns the sum of all item in the set set1={147,2.43} print(“Sum of elements in”,set1,”is”,sum(set1)) Output Sum of elements in {147,2.43} is 149.23 5. sorted (set) – Returns a new sorted list. set1={213,100,289,40,23,1,1000} set2=sorted(set1) print(“Elements before sorting:”,set1) print(“Elements after sorting:”,set2) Output Elements before sorting:{213,100,289,40,23,1,1000} Elements after sorting:{1,23,40,100,213,289,1000}
  • 13. Built-in set functions 6. enumerate(set) – Returns an enumerate object. It contains the index and value of all the items of set as a pair set1={213,100,289,40,23,1,1000} print(“enumerate(set):”,enumerate(set1)) Output enumerate(set): <enumerate object at 0x00F75728> 7. any(set) – Returns True, if the set contains at least one item. Otherwise returns False set1=set() set2={1,2,3,4} print(“any (set):”,any(set1)) print(“any (set):”,any(set2)) Output any(set): False any(set):True
  • 14. Built-in set functions 8. all(set) – Returns True, if all the elements are true or the set is empty set1=set() set2={1,2,3,4} print(“all(set):”,all(set1)) print(“all(set):”,all(set2)) Output all(set):True all(set):True
  • 15. Built-in set methods 1. set.add(obj) – Adds an element obj to a set set1={3,8,2,6} set1.add(9) print(set1) # {8,9,2,3,6} 2. set.remove(obj) – Removes an element obj from set. Raise an error if the set is empty set1={3,8,2,6} set1.remove(8) print(set1) # {2,3,6}
  • 16. Built-in set methods 3. set.discard(obj) – Removes an item obj from set. Nothing happens if the element to be deleted is not present set1={3,8,2,6} set1.discard(8) set1.discard(10) 4. set.pop() – Removes an returns an arbitrary set element. Raise Key error if set is empty set1={3,8,2,6} set1.pop() print(“set after poping:”,set1) # {2,3,6}
  • 17. Built-in set methods 5. set1.union(set2) – Returns the union of two sets as a new set set1={3,8,2,6} set2={4,2,1,9} set3=set1.union(set2) #unique values will be taken print(set3) # {1,2,3,4,6,8,9} 6. set1.update (set2) – Update a set with the union of itself and others. The result will be sorted in set1 set1={3,8,2,6} set2={4,2,1,9} set1.update(set2) print(set1) # {1,2,3,4,6,8,9}
  • 18. Built-in set methods 7. set1.intersection(set2) – Returns the intersection of two sets as a new set set1={3,8,2,6} set2={4,2,1,9} set3=set1.intersection(set2 print(set3) # {2} 8. set1.intersection_update() – Update the set with the intersection of itself and another. result will be stored in set1 set1={3,8,2,6} set2={4,2,1,9} set1.intersection_update(set2) print(set1) # {8,2,3,6}
  • 19. Built-in set methods 9. set1.difference(set2) – Returns the difference of two or more sets into a new set set1={3,8,2,6} set2={4,2,1,9} set3=set1.difference(set2) print(set3) # {8,3,6} 10. set1.difference_update() – Removes all elements of another set set2 from set1 and the result is stored in set1 set1={3,8,2,6} set2={4,2,1,9} set1.difference_update(set2) print(set1) # {8,3,6}
  • 20. Built-in set methods 11. set1.symmetric_difference(set2)- Return the symmetric difference of two sets as a new set. set1={3,8,2,6} set2={4,2,1,9} set3=set1.symmetric_difference(set2) print(set3) # {1,3,4,6,8,9} 12. set1.difference_update(set2)- Update a set with the symmetric difference of itself and another set1={3,8,2,6} set2={4,2,1,9} set1.symmetric_difference_update(set2) print(set1) # {1,3,4,6,8,9}
  • 21. Built-in set methods 13.set1.isdisjoint(set2) – Returns True if two sets have a null intersection set1={3,8,2,6} set2={4,7,1,9} print(“Result of set1.isdisjoint(set2):”,set1.isdisjoint(set2)) Output Result of set1.isdisjoint(set2): True 14. set1.issubset(set2) – Returns True if set1 is a subset of set2 set1={3,8} set2={38,4,7,1,9} print(“Result of set1.issubset(set2):”,set1.issubset(set2)) Output Result of set1.issubset(set2): True
  • 22. Built-in set methods 15. set1.issuperset(set2) – Returns True, if set1 is a super set of set2 set1={3,8,4,6} set2={3,8} print(“Result of set1.issuperset(set2):”,set1.issuperset(set2)) Output Result of set1.issuperset(set2): True
  • 23. Frozenset • Frozenset is a new class that has the characteristics of a set • Its elements cannot be changed once assigned • Frozensets are immutable sets • Frozenset are hashable and can be used as keys to a dictionary • Frozensets are creates by the function frozenset() • Supports methods like difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union() • Being immutable it does not have methods like add(), remove(), update(), difference_update(), intersection_update(),bsymmetric_difference_update() etc.
  • 24. Frozen set Example: set1= frozenset({3,8,4,6}) print(“Set 1:”,set1) set2=frozenset({3,8}) print(“Set 2:”,set2) print(“Result of set1.intersection(set2):”,set1.intersection(set2)) Output Set1: frozenset({8,3,4,6}) Set 2:frozenset({8,3}) Result of set1.intersection(set2): frozenset({8,3})