SlideShare a Scribd company logo
1 of 30
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Python Programming
Dr. S. P. Ponnusamy
Assistant Professor and Head
1
Unit – 3
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Syllabus
• UNIT III
Tuples – creation – basic tuple operations – tuple() function –
indexing – slicing – built-in functions used on tuples – tuple
methods – packing – unpacking – traversing of tuples –
populating tuples – zip() function - Sets – Traversing of sets –
set methods – frozenset.
–.
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
3
Tuples
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples - Introduction
 Tuple is a collection of objects that are ordered, immutable, finite, and
heterogeneous (different types of objects).
 The tuples have fixed sizes.
 Tuples are used when the programmer wants to store multiples of
different data types under a single name.
 It can also be used to return multiple items from the function.
 The elements of the tuple cannot be altered as strings (both are
immutable).
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples - Creation
 The tuple is created with elements covered by a pair of opening and
closing parenthesis.
 An empty tuple is created with empty parenthesis.
 The individual elements of tuples are called "items."
>>> myTuple = (1, 2.0, 4.67, "Chandru", 'M')
>>> myTuple
(1, 2.0, 4.67, 'Chandru', 'M')
>>> myTuple=()
>>> myTuple
()
>>>type(myTuple)
<class 'tuple'>
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples - Creation
 The tuple is created with elements covered by a pair of opening and
closing parenthesis.
 An empty tuple is created with empty parenthesis.
 The individual elements of tuples are called "items."
>>> myTuple = (1, 2.0, 4.67, "Chandru", 'M')
>>> myTuple
(1, 2.0, 4.67, 'Chandru', 'M')
>>> myTuple=()
>>> myTuple
()
>>>type(myTuple)
<class 'tuple'>
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples - Creation
 Covering the elements in parenthesis is not mandatory.
 That means, the elements are assigned to tuples with comma separation
only.
>>> myTuple = 1, 2.0, 4.67, "Chandru", 'M'
>>> myTuple
(1, 2.0, 4.67, 'Chandru', 'M’)
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples - Creation
 A tuple with single item creation is problem in Python as per the above
examples.
 A single item covered by parenthesis is considered to be its own base
type.
>>> myTuple=(8)
>>> myTuple
8
>>> type(myTuple)
<class 'int'>
>>> myTuple =("u")
>>> myTuple
'u'
type(myTuple)
<class 'str'>
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples - Creation
 A tuple with a single item can be created in a special way.
 The elements should be covered with or without parenthesis, but an
additional comma must be placed at the end.
>>> myTuple=(8,)
>>> myTuple
(8,)
>>> type(myTuple)
<class 'tuple'>
>>> myTuple=("God",)
>>> myTuple
('God',)
>>> type(myTuple)
<class 'tuple'>
>>> myTuple=8,
>>> myTuple
(8,)
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
10
Basic Tuple operations
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples operations - Concatenation
 The tuple concatenation operation is done by the "+" (plus) operator.
 Two or more tuples can be concatenated using the plus operator .
>>> myTuple1=(1,2.4,"ABC")
>>> myTuple2=(111.23,-12,"xyz",23)
>>> myTuple3=myTuple1+myTuple2
>>> myTuple3
(1, 2.4, 'ABC', 111.23, -12, 'xyz', 23)
>>> (12,"University",33.8)+(23,345.12)
(12, 'University', 33.8, 23, 345.12)
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples operations - Repetition
 You must be careful when you apply tuple concatenation.
 The other type of value cannot be concatenated with tuple literals using
the concatenation operator.
 If you try this, the interpreter will raise a TypeError message.
>>>(12,"University",33.8)+ 8
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
(12,"University",33.8)+ 8
TypeError: can only concatenate tuple (not "int") to
tuple
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples operations - Repetition
 The given tuple is repeated for the given number of times using the ‘*’
(multiplication) operator.
 The operator requires the tuple as the first operand and an integer value
(the number of times to be repeated) as the second operand.
>>> (1,2,3,"Apple")*3
(1, 2, 3, 'Apple', 1, 2, 3, 'Apple', 1, 2, 3, 'Apple')
>>> (1,2,3,"Apple")*2 +(3,5,23.1)
(1, 2, 3, 'Apple', 1, 2, 3, 'Apple', 3, 5, 23.1)
 Tuple concatenation can be applied along with the tuple repetition.
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples operations – Member operator - in
 The member operators are used to check the existence of the given
value in the tuple or not.
 The operator ‘in’ returns True when the given value exists in a tuple, else
it returns False. It is a binary operator.
>>> myTuple1=(111.23,-12,"xyz",23)
>>> 23 in myTuple1
True
>>> 42 in myTuple1
False
>>> "xyz" in myTuple1
True
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples operations – Member operator - not in
 The operator ‘not in’ returns True when the value does not exist in the
tuple, else it returns False.
 It works opposite to the ‘in‘ operator.
>>> myTuple1=(111.23,-12,"xyz",23)
>>> 42 not in myTuple1
True
>>> 23 not in myTuple1
False
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples operations – Comparison
 The comparison operations on tuples are done with the help of the
relational operators ==. !=, <, >, <=, >=.
 These operators can be applied only to tuple literals and variables.
 Python compares the tuples' values one by one on both sides.
>>> myTuple1=(111.23,-12,"xyz",23)
>>> myTuple2=(111.23,-12,"xyz",23)
>>> myTuple1==myTuple2
True
>>> myTuple1>myTuple2
False
>>> myTuple2=(123,-12,34)
>>> myTuple1<myTuple2
True
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
17
Tuple() function
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples function – tuple()
 It is a built-in function used to create a tuple or convert an iterable
object into a tuple.
 The iterable objects are list, string, set, and dictionary.
>>> tuple("University") #converts string into tuple
('U', 'n', 'i', 'v', 'e', 'r', 's', 'i', 't', 'y')
>>> tuple([1,2,3,"xyz"]) #converts list into tuple
(1, 2, 3, 'xyz')
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Tuples function – tuple()
>>> s1={1,2,3,4}
>>> t1=tuple(s1) # converting set into tuple
>>> t1
(1, 2, 3, 4)
>>> tuple({1:"Apple",2:"Mango"}) #converts dictionary
into tuple
(1, 2)
>>> d1={1:"Apple",2:"Mango"} #converts dictionary items
into tuple
>>> tuple(d1.items())
((1, 'Apple'), (2, 'Mango'))
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
20
Indexing and slicing
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Indexing
 The tuple elements can be accessed by using the indexing operator.
 You can access individual elements by single indexing.
 The Python allows both positive and negative indexing on tuples.
 The positive indexing starts at 0 and ends at n-1, where n is the length of
the tuple.
 The negative indexing starts at -1 and ends at -n.
 The positive indexing follows a left-to-right order, whereas the negative
indexing follows a right-to-left order.
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Indexing
>>> myTuple=(11,23.5,-44,'xyz')
>>> myTuple[2]
-44
>>> myTuple[0]
11
>>> myTuple[-3]
23.5
>>> myTuple[3]
'xyz'
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Slicing
 A subset or slice of a tuple can be extracted by using the index operator
as in a string.
 The index operator takes three parameters, which are separated by a
colon.
 The parameters are the desired start and end indexing values as well as
an optional step value.
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Indexing
>>> myTuple[-3:-1]
(78.98, 'xyz')
>>> myTuple[-4:]
(-34, 78.98, 'xyz', 8)
>>> myTuple[:-2]
(23, -34, 78.98)
>>> myTuple[:]
(23, -34, 78.98, 'xyz', 8)
>>>
(23, -34, 78.98, 'xyz', 8)
>>> myTuple=(23,-34,78.98,"xyz",8)
>>> len(myTuple)
5
>>> myTuple[1:3]
(-34, 78.98)
>>> myTuple[:4]
(23, -34, 78.98, 'xyz')
>>> myTuple[2:]
(78.98, 'xyz', 8)
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
25
Built-in Functions
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Built-in Functions
Function Description
len(tuple) Gives the total length of the tuple.
max(tuple) Returns item from the numerical tuple with max value.
min(tuple) Returns item from the numerical tuple with min value.
sum(tuple)
Gives the sum of the elements present in the numerical
tuple as an output
tuple(seq) Converts an iterable object into tuple.
(already discussed in section 6.4)
sorted(tuple) Returns a sorted list as an output of a numerical tuple in
ascending order
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
27
Tuple Methods
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Methods
Function Description
tuple.count(value) Returns the number of times a specified value occurs in a tuple
if found. If not found, returns 0.
tuple.index(value) Returns the location of where a specified value was found after
searching the tuple for it. If the specified value is not found, it
returns error message.
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
Methods
>>> myTuple1=(13,-34,88,23.5,78,'xyz',88)
>>> myTuple1.count(13)
1
>>> myTuple1.count(88)
2
>>> myTuple1.count(99)
0
>>> myTuple1.index(78)
4
>>> myTuple1.index(103)
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
myTuple1.index(103)
ValueError: tuple.index(x): x not in tuple
Government Arts and Science College
Tittagudi – 606 106
Department of Computer Science
Python Programming
30
End

More Related Content

Similar to Python programming UNIT III-Part-2.0.pptx

ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
A unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexityA unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexity
eSAT Journals
 

Similar to Python programming UNIT III-Part-2.0.pptx (20)

Python Tuple.pdf
Python Tuple.pdfPython Tuple.pdf
Python Tuple.pdf
 
Economic Load Dispatch (ELD), Economic Emission Dispatch (EED), Combined Econ...
Economic Load Dispatch (ELD), Economic Emission Dispatch (EED), Combined Econ...Economic Load Dispatch (ELD), Economic Emission Dispatch (EED), Combined Econ...
Economic Load Dispatch (ELD), Economic Emission Dispatch (EED), Combined Econ...
 
Tuple.py
Tuple.pyTuple.py
Tuple.py
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
Average sort
Average sortAverage sort
Average sort
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
Tuple Operation and Tuple Assignment in Python.pdf
Tuple Operation and Tuple Assignment in Python.pdfTuple Operation and Tuple Assignment in Python.pdf
Tuple Operation and Tuple Assignment in Python.pdf
 
DSA-Day-2-PS.pptx
DSA-Day-2-PS.pptxDSA-Day-2-PS.pptx
DSA-Day-2-PS.pptx
 
Python PPT2
Python PPT2Python PPT2
Python PPT2
 
python ppt
python pptpython ppt
python ppt
 
Selection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time BoundsSelection Sort with Improved Asymptotic Time Bounds
Selection Sort with Improved Asymptotic Time Bounds
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting AlgorithmComparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
 
A unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexityA unique sorting algorithm with linear time &amp; space complexity
A unique sorting algorithm with linear time &amp; space complexity
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Python programming UNIT III-Part-2.0.pptx

  • 1. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Python Programming Dr. S. P. Ponnusamy Assistant Professor and Head 1 Unit – 3
  • 2. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Syllabus • UNIT III Tuples – creation – basic tuple operations – tuple() function – indexing – slicing – built-in functions used on tuples – tuple methods – packing – unpacking – traversing of tuples – populating tuples – zip() function - Sets – Traversing of sets – set methods – frozenset. –.
  • 3. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 3 Tuples
  • 4. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples - Introduction  Tuple is a collection of objects that are ordered, immutable, finite, and heterogeneous (different types of objects).  The tuples have fixed sizes.  Tuples are used when the programmer wants to store multiples of different data types under a single name.  It can also be used to return multiple items from the function.  The elements of the tuple cannot be altered as strings (both are immutable).
  • 5. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples - Creation  The tuple is created with elements covered by a pair of opening and closing parenthesis.  An empty tuple is created with empty parenthesis.  The individual elements of tuples are called "items." >>> myTuple = (1, 2.0, 4.67, "Chandru", 'M') >>> myTuple (1, 2.0, 4.67, 'Chandru', 'M') >>> myTuple=() >>> myTuple () >>>type(myTuple) <class 'tuple'>
  • 6. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples - Creation  The tuple is created with elements covered by a pair of opening and closing parenthesis.  An empty tuple is created with empty parenthesis.  The individual elements of tuples are called "items." >>> myTuple = (1, 2.0, 4.67, "Chandru", 'M') >>> myTuple (1, 2.0, 4.67, 'Chandru', 'M') >>> myTuple=() >>> myTuple () >>>type(myTuple) <class 'tuple'>
  • 7. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples - Creation  Covering the elements in parenthesis is not mandatory.  That means, the elements are assigned to tuples with comma separation only. >>> myTuple = 1, 2.0, 4.67, "Chandru", 'M' >>> myTuple (1, 2.0, 4.67, 'Chandru', 'M’)
  • 8. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples - Creation  A tuple with single item creation is problem in Python as per the above examples.  A single item covered by parenthesis is considered to be its own base type. >>> myTuple=(8) >>> myTuple 8 >>> type(myTuple) <class 'int'> >>> myTuple =("u") >>> myTuple 'u' type(myTuple) <class 'str'>
  • 9. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples - Creation  A tuple with a single item can be created in a special way.  The elements should be covered with or without parenthesis, but an additional comma must be placed at the end. >>> myTuple=(8,) >>> myTuple (8,) >>> type(myTuple) <class 'tuple'> >>> myTuple=("God",) >>> myTuple ('God',) >>> type(myTuple) <class 'tuple'> >>> myTuple=8, >>> myTuple (8,)
  • 10. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 10 Basic Tuple operations
  • 11. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples operations - Concatenation  The tuple concatenation operation is done by the "+" (plus) operator.  Two or more tuples can be concatenated using the plus operator . >>> myTuple1=(1,2.4,"ABC") >>> myTuple2=(111.23,-12,"xyz",23) >>> myTuple3=myTuple1+myTuple2 >>> myTuple3 (1, 2.4, 'ABC', 111.23, -12, 'xyz', 23) >>> (12,"University",33.8)+(23,345.12) (12, 'University', 33.8, 23, 345.12)
  • 12. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples operations - Repetition  You must be careful when you apply tuple concatenation.  The other type of value cannot be concatenated with tuple literals using the concatenation operator.  If you try this, the interpreter will raise a TypeError message. >>>(12,"University",33.8)+ 8 Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> (12,"University",33.8)+ 8 TypeError: can only concatenate tuple (not "int") to tuple
  • 13. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples operations - Repetition  The given tuple is repeated for the given number of times using the ‘*’ (multiplication) operator.  The operator requires the tuple as the first operand and an integer value (the number of times to be repeated) as the second operand. >>> (1,2,3,"Apple")*3 (1, 2, 3, 'Apple', 1, 2, 3, 'Apple', 1, 2, 3, 'Apple') >>> (1,2,3,"Apple")*2 +(3,5,23.1) (1, 2, 3, 'Apple', 1, 2, 3, 'Apple', 3, 5, 23.1)  Tuple concatenation can be applied along with the tuple repetition.
  • 14. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples operations – Member operator - in  The member operators are used to check the existence of the given value in the tuple or not.  The operator ‘in’ returns True when the given value exists in a tuple, else it returns False. It is a binary operator. >>> myTuple1=(111.23,-12,"xyz",23) >>> 23 in myTuple1 True >>> 42 in myTuple1 False >>> "xyz" in myTuple1 True
  • 15. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples operations – Member operator - not in  The operator ‘not in’ returns True when the value does not exist in the tuple, else it returns False.  It works opposite to the ‘in‘ operator. >>> myTuple1=(111.23,-12,"xyz",23) >>> 42 not in myTuple1 True >>> 23 not in myTuple1 False
  • 16. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples operations – Comparison  The comparison operations on tuples are done with the help of the relational operators ==. !=, <, >, <=, >=.  These operators can be applied only to tuple literals and variables.  Python compares the tuples' values one by one on both sides. >>> myTuple1=(111.23,-12,"xyz",23) >>> myTuple2=(111.23,-12,"xyz",23) >>> myTuple1==myTuple2 True >>> myTuple1>myTuple2 False >>> myTuple2=(123,-12,34) >>> myTuple1<myTuple2 True
  • 17. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 17 Tuple() function
  • 18. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples function – tuple()  It is a built-in function used to create a tuple or convert an iterable object into a tuple.  The iterable objects are list, string, set, and dictionary. >>> tuple("University") #converts string into tuple ('U', 'n', 'i', 'v', 'e', 'r', 's', 'i', 't', 'y') >>> tuple([1,2,3,"xyz"]) #converts list into tuple (1, 2, 3, 'xyz')
  • 19. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Tuples function – tuple() >>> s1={1,2,3,4} >>> t1=tuple(s1) # converting set into tuple >>> t1 (1, 2, 3, 4) >>> tuple({1:"Apple",2:"Mango"}) #converts dictionary into tuple (1, 2) >>> d1={1:"Apple",2:"Mango"} #converts dictionary items into tuple >>> tuple(d1.items()) ((1, 'Apple'), (2, 'Mango'))
  • 20. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 20 Indexing and slicing
  • 21. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Indexing  The tuple elements can be accessed by using the indexing operator.  You can access individual elements by single indexing.  The Python allows both positive and negative indexing on tuples.  The positive indexing starts at 0 and ends at n-1, where n is the length of the tuple.  The negative indexing starts at -1 and ends at -n.  The positive indexing follows a left-to-right order, whereas the negative indexing follows a right-to-left order.
  • 22. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Indexing >>> myTuple=(11,23.5,-44,'xyz') >>> myTuple[2] -44 >>> myTuple[0] 11 >>> myTuple[-3] 23.5 >>> myTuple[3] 'xyz'
  • 23. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Slicing  A subset or slice of a tuple can be extracted by using the index operator as in a string.  The index operator takes three parameters, which are separated by a colon.  The parameters are the desired start and end indexing values as well as an optional step value.
  • 24. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Indexing >>> myTuple[-3:-1] (78.98, 'xyz') >>> myTuple[-4:] (-34, 78.98, 'xyz', 8) >>> myTuple[:-2] (23, -34, 78.98) >>> myTuple[:] (23, -34, 78.98, 'xyz', 8) >>> (23, -34, 78.98, 'xyz', 8) >>> myTuple=(23,-34,78.98,"xyz",8) >>> len(myTuple) 5 >>> myTuple[1:3] (-34, 78.98) >>> myTuple[:4] (23, -34, 78.98, 'xyz') >>> myTuple[2:] (78.98, 'xyz', 8)
  • 25. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 25 Built-in Functions
  • 26. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Built-in Functions Function Description len(tuple) Gives the total length of the tuple. max(tuple) Returns item from the numerical tuple with max value. min(tuple) Returns item from the numerical tuple with min value. sum(tuple) Gives the sum of the elements present in the numerical tuple as an output tuple(seq) Converts an iterable object into tuple. (already discussed in section 6.4) sorted(tuple) Returns a sorted list as an output of a numerical tuple in ascending order
  • 27. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 27 Tuple Methods
  • 28. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Methods Function Description tuple.count(value) Returns the number of times a specified value occurs in a tuple if found. If not found, returns 0. tuple.index(value) Returns the location of where a specified value was found after searching the tuple for it. If the specified value is not found, it returns error message.
  • 29. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming Methods >>> myTuple1=(13,-34,88,23.5,78,'xyz',88) >>> myTuple1.count(13) 1 >>> myTuple1.count(88) 2 >>> myTuple1.count(99) 0 >>> myTuple1.index(78) 4 >>> myTuple1.index(103) Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> myTuple1.index(103) ValueError: tuple.index(x): x not in tuple
  • 30. Government Arts and Science College Tittagudi – 606 106 Department of Computer Science Python Programming 30 End