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 tuples in Python, describing them as ordered, immutable sequences that can hold any type of value and are indexed by integers. They can be created with comma-separated values and may use parentheses for clarity. A single-element tuple requires a trailing comma. While tuples support operations like indexing and slicing, they can't be modified directly. Instead, they can be replaced entirely.
- 10.2: Comparing Tuples
- This page explains the use of comparison operators with tuples and sequences in Python, emphasizing the prioritization of first elements. It introduces the DSU pattern (Decorate, Sort, Undecorate) for sorting by specific keys and provides an example of sorting words by length. The example illustrates creating tuples of word lengths, sorting them in descending order, and unpacking to show the final list of words from longest to shortest, showcasing the DSU method's effectiveness.
- 10.3: Tuple Assignment
- This page explains tuple assignment in Python, which allows multiple variables to be assigned values from a sequence in a single line. It highlights the ability to swap values between variables and notes that the number of variables must match the number of values. The versatility of tuple assignment is showcased through its application with different sequences, including lists and strings, demonstrating its usefulness in scenarios like splitting an email address.
- 10.4: Dictionaries and Tuples
- This page explains the 'items' method in Python dictionaries, which retrieves key-value pairs as tuples. It details how to sort these tuples for alphabetical order by key, providing an example of converting a dictionary to a sorted list of tuples from 'a' to 'c'. It emphasizes that the initial order of dictionary entries is not guaranteed because dictionaries do not maintain order.
- 10.5: Multiple assignment with dictionaries
- This page discusses a Python code pattern for iterating over a dictionary using tuple assignment in loops to print key-value pairs. It also explains how to sort dictionary contents by value by creating and sorting a list of tuples, enabling organized output based on values rather than keys.
- 10.6: The most common words
- This page describes a Python program that analyzes "Romeo and Juliet" to identify the ten most common words. It outlines the process of reading the text, cleaning it by eliminating punctuation and converting to lowercase, and counting word frequencies. The program creates a list of tuples from the word counts, sorts them, and displays the top ten words with their frequencies. The author highlights Python's simplicity and effectiveness in executing complex data analysis tasks.
- 10.7: Using Tuples as Keys in Dictionaries
- This page explains how tuples can be used as composite keys in dictionaries, which is particularly beneficial for mapping name pairs to phone numbers in a directory. It demonstrates the method of defining a dictionary with tuple keys and provides a way to traverse it using loops to display names alongside their corresponding numbers.
- 10.8: Sequences- strings, lists, and tuples - Oh My!
- This page discusses Python sequence types: strings, lists, and tuples. It highlights that strings are immutable, while lists are preferred for their mutability. Tuples, though immutable, serve specific purposes like serving as dictionary keys or in function arguments. The text notes that tuples lack methods such as sort or reverse, but Python provides built-in functions for reordering sequences.
- 10.9: Debugging
- This page highlights the significance of grasping compound data structures, like lists and dictionaries, to avoid shape errors. It presents four debugging strategies: code review, experimentation, contemplation, and simplification. Each method has its challenges, particularly for novices who might overemphasize one approach. The text stresses the importance of hypothesizing errors and collaborating with others, advocating for code simplification when tackling complex issues.
- 10.E: Tuples (Exercises)
- This page summarizes exercises that involve creating Python programs for data analysis from text files. The first exercise counts messages per email address and identifies the one with the most commits. The second analyzes message distribution by hour using timestamps. The third counts letter frequency (a-z) while ignoring other characters, facilitating cross-language comparisons.
- 10.G: Tuples (Glossary)
- This page defines important programming concepts such as "comparable" types, "data structures," and "DSU." It discusses operations like "gather" and "scatter," along with "hashable" types and the notion of "shape" for data structure characteristics. It also explains "singleton" as a single-element sequence, "tuple" as an immutable sequence, and "tuple assignment" for managing variable assignments.