Skip to main content
Engineering LibreTexts

10: Tuples

[ "article:topic-guide", "authorname:severancec", "Tuples", "python (language)", "license:ccbyncsa" ]
  • Page ID
    2945
    • 10.1: Tuples are Immutable
      A tuple1 is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers. The important difference is that tuples are immutable. Tuples are also comparable and hashable so we can sort lists of them and use tuples as key values in Python dictionaries.
    • 10.2: Comparing Tuples
      The comparison operators work with tuples and other sequences. Python starts by comparing the first element from each sequence. If they are equal, it goes on to the next element, and so on, until it finds elements that differ. Subsequent elements are not considered (even if they are really big).
    • 10.3: Tuple Assignment
      One of the unique syntactic features of the Python language is the ability to have a tuple on the left side of an assignment statement. This allows you to assign more than one variable at a time when the left side is a sequence.
    • 10.4: Dictionaries and Tuples
      Dictionaries have a method called items that returns a list of tuples, where each tuple is a key-value pair.
    • 10.5: Multiple assignment with dictionaries
    • 10.6: The most common words
    • 10.7: Using Tuples as Keys in Dictionaries
      Because tuples are hashable and lists are not, if we want to create a composite key to use in a dictionary we must use a tuple as the key.
    • 10.8: Sequences: strings, lists, and tuples - Oh My!
    • 10.9: Debugging
      in this chapter we are starting to see compound data structures, like lists of tuples, and dictionaries that contain tuples as keys and lists as values. Compound data structures are useful, but they are prone to what I call shape errors; that is, errors caused when a data structure has the wrong type, size, or composition, or perhaps you write some code and forget the shape of your data and introduce an error.
    • 10.E: Tuples (Exercises)
    • 10.G: Tuples (Glossary)