SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
 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.
mohammed.sikander@cranessoftware.com 3
 A tuple is created by placing all the
items (elements) inside a parentheses
(), separated by comma.
 The parentheses are optional but is a
good practice to write it.
 A tuple can have any number of items
and they may be of different types
(integer, float, list, string etc.).
 Creating a tuple with one element is a bit tricky.
 Having one element within parentheses is not enough.We
will need a trailing comma to indicate that it is in fact 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.
 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.
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.
 We cannot change the elements in a tuple. That also means we
cannot delete or remove items from a tuple.
 But deleting a tuple entirely is possible using the keyword del.
 Methods that add items or remove items are not available with tuple.
Only the following two methods are available.
Method Description
count(x) Return the number of items that is equal to x
index(x) Return index of first item that is equal to x
 We can test if an item exists in a tuple or not,
using the keyword in.
 Using a for loop we can iterate though each item in a tuple.
Function Description
all() ReturnTrue if all elements of the tuple are true (or if the tuple is empty).
any() ReturnTrue if any element of the tuple is true. If the tuple is empty, return False.
enumerate()
Return an enumerate object. It contains the index and value of all the items of
tuple as pairs.
len() Return the length (the number of items) in the tuple.
max() Return the largest item in the tuple.
min() Return the smallest item in the tuple
sorted()
Take elements in the tuple and return a new sorted list (does not sort the tuple
itself).
sum() Retrun the sum of all elements in the tuple.
tuple() Convert an iterable (list, string, set, dictionary) to a tuple.

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Python list
Python listPython list
Python list
 
Stack
StackStack
Stack
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Structure in C
Structure in CStructure in C
Structure in C
 
Array ppt
Array pptArray ppt
Array ppt
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Array in c
Array in cArray in c
Array in c
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 

Semelhante a Python tuple

Semelhante a Python tuple (20)

Lecture 09.pptx
Lecture 09.pptxLecture 09.pptx
Lecture 09.pptx
 
Tuples.pptx
Tuples.pptxTuples.pptx
Tuples.pptx
 
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptxPYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
PYTHON LISTsjnjsnljnsjnosojnosojnsojnsjsn.pptx
 
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
 
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
 
1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx1.10 Tuples_sets_usage_applications_advantages.pptx
1.10 Tuples_sets_usage_applications_advantages.pptx
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
Chapter 13 Tuples.pptx
Chapter 13 Tuples.pptxChapter 13 Tuples.pptx
Chapter 13 Tuples.pptx
 
Notes8
Notes8Notes8
Notes8
 
Chapter 13 Tuples.pptx
Chapter 13 Tuples.pptxChapter 13 Tuples.pptx
Chapter 13 Tuples.pptx
 
13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss13- Data and Its Types presentation kafss
13- Data and Its Types presentation kafss
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
Data structures in Python
Data structures in PythonData structures in Python
Data structures in Python
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
dataStructuresInPython.pptx
dataStructuresInPython.pptxdataStructuresInPython.pptx
dataStructuresInPython.pptx
 
lists
listslists
lists
 
tupple.pptx
tupple.pptxtupple.pptx
tupple.pptx
 
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
 

Mais de Mohammed Sikander

Mais de Mohammed Sikander (19)

Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python strings
Python stringsPython strings
Python strings
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
Signal
SignalSignal
Signal
 
File management
File managementFile management
File management
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macros
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 

Último

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

Python tuple

  • 1.
  • 2.  Tuples are very similar to lists, except that they are immutable (they cannot be changed).  They are created using parentheses, rather than square brackets.
  • 3.  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. mohammed.sikander@cranessoftware.com 3
  • 4.  A tuple is created by placing all the items (elements) inside a parentheses (), separated by comma.  The parentheses are optional but is a good practice to write it.  A tuple can have any number of items and they may be of different types (integer, float, list, string etc.).
  • 5.  Creating a tuple with one element is a bit tricky.  Having one element within parentheses is not enough.We will need a trailing comma to indicate that it is in fact a tuple.
  • 6.  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.
  • 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. 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.  We cannot change the elements in a tuple. That also means we cannot delete or remove items from a tuple.  But deleting a tuple entirely is possible using the keyword del.
  • 10.  Methods that add items or remove items are not available with tuple. Only the following two methods are available. Method Description count(x) Return the number of items that is equal to x index(x) Return index of first item that is equal to x
  • 11.  We can test if an item exists in a tuple or not, using the keyword in.
  • 12.  Using a for loop we can iterate though each item in a tuple.
  • 13. Function Description all() ReturnTrue if all elements of the tuple are true (or if the tuple is empty). any() ReturnTrue if any element of the tuple is true. If the tuple is empty, return False. enumerate() Return an enumerate object. It contains the index and value of all the items of tuple as pairs. len() Return the length (the number of items) in the tuple. max() Return the largest item in the tuple. min() Return the smallest item in the tuple sorted() Take elements in the tuple and return a new sorted list (does not sort the tuple itself). sum() Retrun the sum of all elements in the tuple. tuple() Convert an iterable (list, string, set, dictionary) to a tuple.