Skip to main content
Engineering LibreTexts

10: Tuples

  • Page ID
    122403
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    • 10.1: Tuples are Immutable
      This page explains that a tuple in Python is an immutable sequence of values, differing from lists in that its elements cannot be altered after creation. Tuples can contain various data types, are indexed by integers, and can serve as dictionary keys. They are typically denoted by parentheses, and a single-element tuple can be formed using a trailing comma. While tuples allow indexing and slicing, direct modifications are prohibited, and changes can only be made by generating a new tuple.
    • 10.2: Comparing Tuples
      This page explains Python's comparison operators and the sort function for sequences like tuples, highlighting the DSU pattern: Decorate, Sort, and Undecorate. It provides an example of sorting words by length, resolving ties alphabetically, resulting in a list of words ordered from longest to shortest.
    • 10.3: Tuple Assignment
      This page explains Python's tuple unpacking feature in assignment statements, allowing simultaneous assignment of multiple variables from sequences. It illustrates this with a list example and shows how tuple assignment can easily swap variable values. The summary emphasizes the requirement that the number of variables must match the unpacked values and notes that any sequence, including strings and lists, can be unpacked, exemplified by splitting an email into username and domain.
    • 10.4: Dictionaries and Tuples
      This page explains how Python dictionaries have an 'items' method that turns them into a list of tuples of key-value pairs. Although the list is unordered, it can be sorted since tuples are comparable. This allows for sorting the dictionary contents by key, displaying key-value pairs in ascending alphabetical order after conversion and sorting.
    • 10.5: Multiple assignment with dictionaries
      This page explains how to traverse a Python dictionary's keys and values using loops and tuple assignment to print them. It also details how to sort the dictionary by values by creating and sorting a list of tuples, enabling access to the dictionary's values in a desired sequence.
    • 10.6: The most common words
      This page details a Python program that analyzes "Romeo and Juliet" to identify the ten most common words. It processes the text by removing punctuation and converting it to lowercase, counts word occurrences, and sorts the results by frequency and alphabetically in case of ties. The program effectively showcases Python's capabilities in data analysis through its concise design.
    • 10.7: Using Tuples as Keys in Dictionaries
      This page explains that tuples are hashable and can be used as keys in dictionaries, unlike lists. An example provided illustrates how to create a telephone directory using last-name and first-name tuples as keys to map to telephone numbers. It also describes how to traverse this dictionary with a for loop to print names and numbers.
    • 10.8: Sequences- strings, lists, and tuples - Oh My!
      This page covers Python's sequence types: strings, lists, and tuples. Strings are immutable and consist of characters, while lists offer mutability, making them more common. Tuples, although immutable, are useful in specific situations such as return values and as dictionary keys. They help avoid aliasing in function arguments, and although they can't be modified directly, Python offers methods like sorted and reversed to generate new rearranged sequences.
    • 10.9: Debugging
      This page discusses compound data structures, addressing the risk of shape errors due to incorrect types, sizes, or compositions. It offers four debugging approaches: code review, experimentation, contemplation of errors, and simplifying code. The chapter emphasizes a balanced approach to debugging, warning against beginners relying on a single method, which can be counterproductive. It also highlights the benefits of taking breaks and explaining problems to others for enhanced problem-solving.
    • 10.E: Tuples (Exercises)
      This page presents three Python programming exercises centered on file analysis and data processing. Exercise 1 focuses on parsing "From" lines to identify the top sender based on message counts. Exercise 2 involves counting hourly message frequencies, and Exercise 3 requires calculating letter frequencies while ignoring non-letter characters. Overall, the exercises aim to enhance skills in data manipulation and analysis using Python.
    • 10.G: Tuples (Glossary)
      This page defines key programming concepts related to data structures and types, including "comparable" types, lists, and dictionaries. It discusses the DSU pattern for creating and sorting tuples, and the terms "gather" and "scatter" for handling arguments. The text also covers "hashable" types with hash functions, defines a "singleton" as a single-element sequence, and explains "tuples" as immutable sequences along with "tuple assignment."


    This page titled 10: Tuples is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform.