SlideShare a Scribd company logo
3
Most read
7
Most read
11
Most read
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})

More Related Content

What's hot (20)

DOCX
Understanding c file handling functions with examples
Muhammed Thanveer M
 
PPSX
Type conversion
Frijo Francis
 
PPTX
python conditional statement.pptx
Dolchandra
 
PDF
Introduction to Python
Mohammed Sikander
 
PDF
Python set
Mohammed Sikander
 
PPTX
String
Kishan Gohel
 
PDF
Queues
Hareem Aslam
 
PDF
Python tuple
Mohammed Sikander
 
PPTX
Row major and column major in 2 d
nikhilarora2211
 
PPTX
Python dictionary
Mohammed Sikander
 
PDF
Python-03| Data types
Mohd Sajjad
 
PPT
Python GUI Programming
RTS Tech
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PPTX
Chapter 17 Tuples
Praveen M Jigajinni
 
PPTX
Method overloading
Lovely Professional University
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
C data types, arrays and structs
Saad Sheikh
 
PPTX
Arrays in c
CHANDAN KUMAR
 
Understanding c file handling functions with examples
Muhammed Thanveer M
 
Type conversion
Frijo Francis
 
python conditional statement.pptx
Dolchandra
 
Introduction to Python
Mohammed Sikander
 
Python set
Mohammed Sikander
 
String
Kishan Gohel
 
Queues
Hareem Aslam
 
Python tuple
Mohammed Sikander
 
Row major and column major in 2 d
nikhilarora2211
 
Python dictionary
Mohammed Sikander
 
Python-03| Data types
Mohd Sajjad
 
Python GUI Programming
RTS Tech
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Edureka!
 
Looping Statements and Control Statements in Python
PriyankaC44
 
Chapter 17 Tuples
Praveen M Jigajinni
 
Method overloading
Lovely Professional University
 
Python Flow Control
Mohammed Sikander
 
C data types, arrays and structs
Saad Sheikh
 
Arrays in c
CHANDAN KUMAR
 

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

PPTX
Python data structures
kalyanibedekar
 
PDF
Set methods in python
deepalishinkar1
 
PPT
Tuples, sets and dictionaries-Programming in Python
ssuser2868281
 
PPTX
Python_Sets(1).pptx
rishiabes
 
PPTX
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
BrazilAccount1
 
PPTX
Python Lecture 11
Inzamam Baig
 
PPTX
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
judyhilly13
 
PPTX
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
PPTX
Seventh session
AliMohammad155
 
PPTX
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
PPT
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
Farhana859326
 
PPT
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
PPT
GF_Python_Data_Structures.ppt
ManishPaul40
 
PPTX
Tuples-and-Dictionaries.pptx
AyushTripathi998357
 
PPTX
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
PPTX
Python introduction data structures lists etc
ssuser26ff68
 
PPTX
Programming with Python_Unit-3-Notes.pptx
YugandharaNalavade
 
PPTX
UNIT-3 python and data structure alo.pptx
harikahhy
 
Python data structures
kalyanibedekar
 
Set methods in python
deepalishinkar1
 
Tuples, sets and dictionaries-Programming in Python
ssuser2868281
 
Python_Sets(1).pptx
rishiabes
 
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
BrazilAccount1
 
Python Lecture 11
Inzamam Baig
 
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
judyhilly13
 
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
Seventh session
AliMohammad155
 
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
Farhana859326
 
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
GF_Python_Data_Structures.ppt
ManishPaul40
 
Tuples-and-Dictionaries.pptx
AyushTripathi998357
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
Python introduction data structures lists etc
ssuser26ff68
 
Programming with Python_Unit-3-Notes.pptx
YugandharaNalavade
 
UNIT-3 python and data structure alo.pptx
harikahhy
 
Ad

More from Megha V (20)

PPTX
Soft Computing Techniques_Part 1.pptx
Megha V
 
PPTX
JavaScript- Functions and arrays.pptx
Megha V
 
PPTX
Introduction to JavaScript
Megha V
 
PPTX
Python Exception Handling
Megha V
 
PPTX
Python- Regular expression
Megha V
 
PPTX
File handling in Python
Megha V
 
PPTX
Python programming –part 7
Megha V
 
PPTX
Python programming Part -6
Megha V
 
PPTX
Python programming: Anonymous functions, String operations
Megha V
 
PPTX
Python programming- Part IV(Functions)
Megha V
 
PPTX
Python programming –part 3
Megha V
 
PPTX
Parts of python programming language
Megha V
 
PPTX
Python programming
Megha V
 
PPTX
Strassen's matrix multiplication
Megha V
 
PPTX
Solving recurrences
Megha V
 
PPTX
Algorithm Analysis
Megha V
 
PPTX
Algorithm analysis and design
Megha V
 
PPTX
Genetic algorithm
Megha V
 
PPTX
UGC NET Paper 1 ICT Memory and data
Megha V
 
PPTX
Seminar presentation on OpenGL
Megha V
 
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Megha V
 
Python Exception Handling
Megha V
 
Python- Regular expression
Megha V
 
File handling in Python
Megha V
 
Python programming –part 7
Megha V
 
Python programming Part -6
Megha V
 
Python programming: Anonymous functions, String operations
Megha V
 
Python programming- Part IV(Functions)
Megha V
 
Python programming –part 3
Megha V
 
Parts of python programming language
Megha V
 
Python programming
Megha V
 
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Megha V
 
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Megha V
 
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Megha V
 
Ad

Recently uploaded (20)

PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
PPTX
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
Life and Career Skills Lesson 2.pptxProtective and Risk Factors of Late Adole...
ryangabrielcatalon40
 
Marketing Management PPT Unit 1 and Unit 2.pptx
Sri Ramakrishna College of Arts and science
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
DIGESTION OF CARBOHYDRATES ,PROTEINS AND LIPIDS
raviralanaresh2
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 

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})