SlideShare a Scribd company logo
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

PDF
Amcat automata questions
PDF
Amcat automata questions
PDF
tuples chapter (1).pdfhsuenduneudhhsbhhd
PPTX
1.10 Tuples_sets_usage_applications_advantages.pptx
PDF
Python-Tuples
PDF
Distance Sort
PPT
UNIT III_Python Programming_aditya COllege
PPT
UNIT III_Python Programming_aditya COllege
Amcat automata questions
Amcat automata questions
tuples chapter (1).pdfhsuenduneudhhsbhhd
1.10 Tuples_sets_usage_applications_advantages.pptx
Python-Tuples
Distance Sort
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege

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

PDF
Advance data structure & algorithm
PDF
Python Tuple.pdf
PDF
Economic Load Dispatch (ELD), Economic Emission Dispatch (EED), Combined Econ...
PPTX
Tuple.py
PDF
Python Interview Questions And Answers
PPTX
data structures using C 2 sem BCA univeristy of mysore
PPTX
Time and Space Complexity Analysis.pptx
PDF
Average sort
DOCX
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
PPTX
“Python” or “CPython” is written in C/C+
PPT
Genetic Algorithms
PPT
chap 06 hgjhg hghg hh ghg jh jhghj gj g.ppt
PDF
Tuple Operation and Tuple Assignment in Python.pdf
PDF
Tuple in Python class documnet pritened.
PDF
Python Is Very most Important for Your Life Time.
PPTX
DSA-Day-2-PS.pptx
PPTX
Python PPT2
PPTX
python ppt
PDF
Selection Sort with Improved Asymptotic Time Bounds
PPT
Algorithms with-java-advanced-1.0
Advance data structure & algorithm
Python Tuple.pdf
Economic Load Dispatch (ELD), Economic Emission Dispatch (EED), Combined Econ...
Tuple.py
Python Interview Questions And Answers
data structures using C 2 sem BCA univeristy of mysore
Time and Space Complexity Analysis.pptx
Average sort
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
“Python” or “CPython” is written in C/C+
Genetic Algorithms
chap 06 hgjhg hghg hh ghg jh jhghj gj g.ppt
Tuple Operation and Tuple Assignment in Python.pdf
Tuple in Python class documnet pritened.
Python Is Very most Important for Your Life Time.
DSA-Day-2-PS.pptx
Python PPT2
python ppt
Selection Sort with Improved Asymptotic Time Bounds
Algorithms with-java-advanced-1.0
Ad

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Trump Administration's workforce development strategy
PDF
RMMM.pdf make it easy to upload and study
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
What if we spent less time fighting change, and more time building what’s rig...
Trump Administration's workforce development strategy
RMMM.pdf make it easy to upload and study
Updated Idioms and Phrasal Verbs in English subject
Anesthesia in Laparoscopic Surgery in India
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
History, Philosophy and sociology of education (1).pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
01-Introduction-to-Information-Management.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Yogi Goddess Pres Conference Studio Updates
Orientation - ARALprogram of Deped to the Parents.pptx
Ad

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