SlideShare a Scribd company logo
1 of 11
DISCOVER . LEARN . EMPOWER
PYTHON BASICS
UNIVERSITY INSTITUTE OF
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGINEERING
Programming in Python
Tuple
2
Tuple
• Tuples are very similar to lists, except that they are immutable (they cannot be
changed).
They are created using parentheses, rather than square brackets.
• We generally use tuple for heterogeneous (different) datatypes and list for
homogeneous (similar) datatypes.
• Since tuple are immutable, iterating through tuple is faster than with list. So there
is a slight performance boost.
• Tuples that contain immutable elements can be used as
• key for a dictionary. With list, this is not possible.
• If you have data that doesn't change, implementing it as tuple will guarantee that it
remains write-protected.
3
Creating a Tuple
• A tuple is created by placing all
the items(elements) inside a
parentheses (), separated by
comma.
• Theparenthesesareoptional
but isa goodpracticeto write
it.
• Atuple canhaveanynumberof
items and they may be of
different types (integer,float,
list,stringetc.).
4
Creating a Tuple
• Creatingatuple with
oneelementisabit
tricky.
Havingone element
within parenthesesis
not enough. We will
needatrailingcomma
to indicatethat it isin
factatuple.
5
Accessing Elements in a Tuple
• You can access the values in the tuple with their index, just as you did with lists:
• Nested tuple are accessed using nested indexing
• Negative indexing can be applied to tuples similar to lists.
• We can access a range of items in a tuple by using the slicing operator
• Trying to reassign a value in a tuple causes a TypeError.
6
Changing A Tuple
7
• Unlike lists, tuples are immutable.
• This means that elements of a tuple cannot be changed once
it has been assigned. But, if the element is itself a mutable
datatype like list, its nested items can be changed.
Changing a Tuple
8
Similar to List,
• We can use + operator to combine two tuples. This is also called
concatenation.
• We can also repeat the elements in a tuple for a given number of times
using the * operator.
• Both + and * operations result into a new tuple.
Deleting a Tuple
9
• We cannot change the elements in a tuple. That also meanswe cannot
delete or remove items from a tuple.
• But deleting a tuple entirely is possible using the keyword del.
Tuple Methods
10
• Methods that add items or remove items are not available withtuple.
Only the following two methods are available.
Tuple Membership Test
11
• Wecan testifanitemexistsinatupleornot, usingthe keywordin.

More Related Content

Similar to Tuples.pptx

Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
kalai75
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
lailoesakhan
 

Similar to Tuples.pptx (20)

Chapter 13 Tuples.pptx
Chapter 13 Tuples.pptxChapter 13 Tuples.pptx
Chapter 13 Tuples.pptx
 
Computer Sience, This is my presentation for beginners coding in python
Computer Sience, This is my presentation for beginners coding in pythonComputer Sience, This is my presentation for beginners coding in python
Computer Sience, This is my presentation for beginners coding in python
 
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
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdfCOMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
COMPUTER SCIENCE SUPPORT MATERIAL CLASS 12.pdf
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx2. Values and Data types in Python.pptx
2. Values and Data types in Python.pptx
 
Datastrucure
DatastrucureDatastrucure
Datastrucure
 
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptxQ-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
Q-Step_WS_06112019_Data_Analysis_and_visualisation_with_Python.pptx
 
Python Collections - List, Tuple and Set.
Python Collections - List, Tuple and Set.Python Collections - List, Tuple and Set.
Python Collections - List, Tuple and Set.
 
computer science tuple in python project.pdf
computer science tuple in python project.pdfcomputer science tuple in python project.pdf
computer science tuple in python project.pdf
 
List and tuple in python
List and tuple in pythonList and tuple in python
List and tuple in python
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Python_basics.pptx
Python_basics.pptxPython_basics.pptx
Python_basics.pptx
 
mbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxmbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptx
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdfProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
ProgFund_Lecture_3_Data_Structures_and_Iteration-1.pdf
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 

Recently uploaded

electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
benjamincojr
 

Recently uploaded (20)

Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon15-Minute City: A Completely New Horizon
15-Minute City: A Completely New Horizon
 
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
The Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptxThe Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptx
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 

Tuples.pptx

  • 1. DISCOVER . LEARN . EMPOWER PYTHON BASICS UNIVERSITY INSTITUTE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Programming in Python
  • 3. Tuple • Tuples are very similar to lists, except that they are immutable (they cannot be changed). They are created using parentheses, rather than square brackets. • We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes. • Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost. • Tuples that contain immutable elements can be used as • key for a dictionary. With list, this is not possible. • If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected. 3
  • 4. Creating a Tuple • A tuple is created by placing all the items(elements) inside a parentheses (), separated by comma. • Theparenthesesareoptional but isa goodpracticeto write it. • Atuple canhaveanynumberof items and they may be of different types (integer,float, list,stringetc.). 4
  • 5. Creating a Tuple • Creatingatuple with oneelementisabit tricky. Havingone element within parenthesesis not enough. We will needatrailingcomma to indicatethat it isin factatuple. 5
  • 6. Accessing Elements in a Tuple • You can access the values in the tuple with their index, just as you did with lists: • Nested tuple are accessed using nested indexing • Negative indexing can be applied to tuples similar to lists. • We can access a range of items in a tuple by using the slicing operator • Trying to reassign a value in a tuple causes a TypeError. 6
  • 7. Changing A Tuple 7 • Unlike lists, tuples are immutable. • This means that elements of a tuple cannot be changed once it has been assigned. But, if the element is itself a mutable datatype like list, its nested items can be changed.
  • 8. Changing a Tuple 8 Similar to List, • We can use + operator to combine two tuples. This is also called concatenation. • We can also repeat the elements in a tuple for a given number of times using the * operator. • Both + and * operations result into a new tuple.
  • 9. Deleting a Tuple 9 • We cannot change the elements in a tuple. That also meanswe cannot delete or remove items from a tuple. • But deleting a tuple entirely is possible using the keyword del.
  • 10. Tuple Methods 10 • Methods that add items or remove items are not available withtuple. Only the following two methods are available.
  • 11. Tuple Membership Test 11 • Wecan testifanitemexistsinatupleornot, usingthe keywordin.