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

Python list
Python listPython list
Python list
Mohammed Sikander
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
Create table
Create tableCreate table
Create table
Nitesh Singh
 
Dictionary
DictionaryDictionary
Dictionary
Pooja B S
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
Rajendran
 
Java Strings
Java StringsJava Strings
Java Strings
RaBiya Chaudhry
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
deepalishinkar1
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
Online
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
GauravPatil318
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
Input output statement
Input output statementInput output statement
Input output statement
sunilchute1
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
String functions in C
String functions in CString functions in C
String functions in C
baabtra.com - No. 1 supplier of quality freshers
 

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

Python data structures
Python data structuresPython data structures
Python data structures
kalyanibedekar
 
Tuples, sets and dictionaries-Programming in Python
Tuples, sets and dictionaries-Programming in PythonTuples, sets and dictionaries-Programming in Python
Tuples, sets and dictionaries-Programming in Python
ssuser2868281
 
Python_Sets(1).pptx
Python_Sets(1).pptxPython_Sets(1).pptx
Python_Sets(1).pptx
rishiabes
 
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
BrazilAccount1
 
Sets
SetsSets
Sets
Lakshmi Sarvani Videla
 
Sets in python
Sets in pythonSets in python
Sets in python
baabtra.com - No. 1 supplier of quality freshers
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
Inzamam Baig
 
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
Intro Python Data Structures.pptx Intro Python Data Structures.pptxIntro Python Data Structures.pptx Intro Python Data Structures.pptx
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
judyhilly13
 
Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
Seventh session
Seventh sessionSeventh session
Seventh session
AliMohammad155
 
Chapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptxChapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
STACK_Imp_Functiosbbsbsbsbsbababawns.pptSTACK_Imp_Functiosbbsbsbsbsbababawns.ppt
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
Farhana859326
 
Python set
Python setPython set
Python set
Mohammed Sikander
 
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjGF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
GF_Python_Data_Structures.ppt
GF_Python_Data_Structures.pptGF_Python_Data_Structures.ppt
GF_Python_Data_Structures.ppt
ManishPaul40
 
Tuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptxTuples-and-Dictionaries.pptx
Tuples-and-Dictionaries.pptx
AyushTripathi998357
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
Python introduction data structures lists etc
Python introduction data structures lists etcPython introduction data structures lists etc
Python introduction data structures lists etc
ssuser26ff68
 
Programming with Python_Unit-3-Notes.pptx
Programming with Python_Unit-3-Notes.pptxProgramming with Python_Unit-3-Notes.pptx
Programming with Python_Unit-3-Notes.pptx
YugandharaNalavade
 
UNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptxUNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptx
harikahhy
 
Python data structures
Python data structuresPython data structures
Python data structures
kalyanibedekar
 
Tuples, sets and dictionaries-Programming in Python
Tuples, sets and dictionaries-Programming in PythonTuples, sets and dictionaries-Programming in Python
Tuples, sets and dictionaries-Programming in Python
ssuser2868281
 
Python_Sets(1).pptx
Python_Sets(1).pptxPython_Sets(1).pptx
Python_Sets(1).pptx
rishiabes
 
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
BrazilAccount1
 
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
Intro Python Data Structures.pptx Intro Python Data Structures.pptxIntro Python Data Structures.pptx Intro Python Data Structures.pptx
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
judyhilly13
 
Chapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptxChapter 3-Data structure in python programming.pptx
Chapter 3-Data structure in python programming.pptx
atharvdeshpande20
 
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
STACK_Imp_Functiosbbsbsbsbsbababawns.pptSTACK_Imp_Functiosbbsbsbsbsbababawns.ppt
STACK_Imp_Functiosbbsbsbsbsbababawns.ppt
Farhana859326
 
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjGF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
GF_Python_Data_Structures.pptjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
divijareddy0502
 
GF_Python_Data_Structures.ppt
GF_Python_Data_Structures.pptGF_Python_Data_Structures.ppt
GF_Python_Data_Structures.ppt
ManishPaul40
 
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptxLecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
Lecture-15-Tuples-and-Dictionaries-Oct23-2018.pptx
lokeshgoud13
 
Python introduction data structures lists etc
Python introduction data structures lists etcPython introduction data structures lists etc
Python introduction data structures lists etc
ssuser26ff68
 
Programming with Python_Unit-3-Notes.pptx
Programming with Python_Unit-3-Notes.pptxProgramming with Python_Unit-3-Notes.pptx
Programming with Python_Unit-3-Notes.pptx
YugandharaNalavade
 
UNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptxUNIT-3 python and data structure alo.pptx
UNIT-3 python and data structure alo.pptx
harikahhy
 
Ad

More from 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
Megha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Megha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
Megha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
Megha V
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
Megha V
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
Megha 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 3
Megha V
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
Megha V
 
Python programming
Python programmingPython programming
Python programming
Megha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Megha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
Megha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
Megha 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 OpenGL
Megha V
 
Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
Megha V
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Megha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
Megha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
Megha V
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
Megha V
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
Megha 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 3
Megha V
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
Megha V
 
Python programming
Python programmingPython programming
Python programming
Megha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Megha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
Megha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
Megha 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 OpenGL
Megha V
 
Ad

Recently uploaded (20)

THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Rose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdfRose Cultivation Practices by Kushal Lamichhane.pdf
Rose Cultivation Practices by Kushal Lamichhane.pdf
kushallamichhame
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Optimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptxOptimization technique in pharmaceutical product development.pptx
Optimization technique in pharmaceutical product development.pptx
UrmiPrajapati3
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_HyderabadWebcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf                              /Nice Dream.pdf                              /
Nice Dream.pdf /
ErinUsher3
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 

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