Tuple

Calvin (Deutschbein)

W10Mon: 28 Oct

Announcements

  • Problem Set 5 tonight.
  • Project 3 next Monday.
    • Milstones :3 by Tuesday
    • Milestone 3 by Wednesday
    • Milestone 4a by Thursday
    • Milestone 4b by Friday
    • Milestone 4c by Saturday

Today

  • Tuples
    • Unpacking
    • Multiple Assignment
    • Enumerate
    • Zip

Tuples

  • Sometimes you have more than one thing.
    • An image size: height by width
    • A class at Willamette: prefix and number
    • A favorite CS 151 instructor: type of professor and name
    • Favorite movie: Franchise and Entry, or Name and Year, or...
  • There are many ways to store these together. One is list.
  • The default in Python, though, is the tuple. >>> x = 1,2 >>> type(x) <class 'tuple'>

Tuples

  • Writing tuples
    • With commas >>> x = 1, >>> type(x) <class 'tuple'> >>> x = 1,2 >>> type(x) <class 'tuple'> >>> x = 1,2, >>> type(x) <class 'tuple'> >>> x = x, 3 >>> type (x) <class 'tuple'>

Tuples

  • Writing tuples
    • Can also be in a return statement >>> def eminem(x): ... return min(x), max(x) ... >>> x = [5,3,7,2,4,6,8] >>> mnm = eminem(x) >>> mnm (2, 8) >>> type(mnm) <class 'tuple'>

Tuples

  • Writing tuples
    • Like lists, but with parens
    • This is how they show up when printed. x = ("hello", "world") >>> type(x) <class 'tuple'>
    • I strongly prefer this because it is easier to read for me.

Tuples v Lists

  • Wait these look just like lists with different parens?
    • Kinda... >>> tup = (1,2,3) >>> lst = [1,2,3] >>> tup[0] 1 >>> lst[0] 1 >>> tup[:2] (1, 2) >>> lst[:2] [1, 2] >>> tup[::-1] (3, 2, 1) >>> lst[::-1] [3, 2, 1]

Tuples v Lists

  • Exactly one difference.
    • The elements of a tuple cannot be changed (like strings) >>> tup = (1,2,3) >>> lst = [1,2,3] >>> tup[1] = '4' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>> lst[1] = 4 >>> tup, lst ((1, 2, 3), [1, 4, 3])

Today

  • ✓ Tuples
    • Unpacking
    • Multiple Assignment
    • Enumerate
    • Zip

Unpacking

  • We can use "single equals assignment" to get things out of tuples.
  • Basically, by saying multiple variables, equal one value...
  • That value is sliced and diced up into the variables.
  • Usually works "intuitively" (for some people) >>> course = "CS", 151, "Intro Py", "Fall", 2024 >>> course ('CS', 151, 'Intro Py', 'Fall', 2024) >>> pre, num, name, sem, yr = course >>> print(pre, num) CS 151
  • Mostly: make sure there is one variable for each "thing" in the tuple.

Unpacking

  • By the way you can unpack lists.
  • I do this sometimes, not too often. >>> course = ["CS", 151, "Intro Py", "Fall", 2024] >>> pre, num, name, sem, yr = course >>> print(pre, num) CS 151

Today

  • ✓ Tuples
    • ✓ Unpacking
    • Multiple Assignment
    • Enumerate
    • Zip

Multiple Assignment

  • This means we can assign multiple variables to values in one line.
  • I love this. >>> c151, c152, d151, d152 = "intro py", "data struct", "intro r", "intro stat" >>> c151 'intro py'
  • Extremely good when trying to swap things around.>>> x, y = 16, 10 >>> x, y = min(x,y), max(x,y) >>> x 10
  • "Technical interviews" for software jobs tend to ask stuff that is easier if you do this.

Multiple Assignment

  • Ascending values between two values.
  • This is the maximally correct solution to div6/7, which didn't check if the first value was the lower value.
  • I love this. >>> def vals_btw(x,y): ... x, y = min(x,y), max(x,y) ... return range(x,y) ... >>> for i in vals_btw(15,10): ... print(i) ... 10 11 12 13 14

Today

  • ✓ Tuples
    • ✓ Unpacking
    • ✓ Multiple Assignment
    • Enumerate
    • Zip

Enumerate

  • Some of you have found 'enumerate' already.
  • Gives you an element of a list/tuple and also its index.
  • It is literally impossible for me to use it because I forget if index or value is first, so I only ever use indices, but you are allowed to use it of course.
  • Also, if you haven't done PS5.1b by now, that is bad >>> hist = [0, 2, 1, 2, 1, 4, 1, 1, 1, 3] >>> for index, value in enumerate(hist): ... print(index, ": " + value * "*") ... 0 : 1 : ** 2 : * 3 : ** 4 : * 5 : **** 6 : * 7 : * 8 : * 9 : ***

Enumerate

  • This is the other way.
  • Also, if you haven't done PS5.1b by now, that is bad >>> hist = [0, 2, 1, 2, 1, 4, 1, 1, 1, 3] >>> _ = [print(i, " " + hist[i] * "*") for i in range(len(hist))] 0 1 ** 2 * 3 ** 4 * 5 **** 6 * 7 * 8 * 9 ***

Today

  • ✓ Tuples
    • ✓ Unpacking
    • ✓ Multiple Assignment
    • ✓ Enumerate
    • Zip

Zip

  • Some of you have found 'zip' already.
  • I have never used it
    • I just do a quick list comprehension
  • Very useful on "Green Screen" (milestone 3).
  • Could have seen use on wordle. >>> [a == b for a, b in zip("hello","world")] [False, False, False, True, False]

Zip

  • Can always do with list comps. >>> h, w = "hello", "world" >>> [h[i] == w[i] for i in range(len(h))] [False, False, False, True, False]
  • Easy to argue that is pretty inelegant.
  • On Milestone 3, use a zip probably over:
    • Rows of pixels
      • One row from a foreground image.
      • One row from a background image.
    • Check something (if its green) about the foreground pixel.
    • If it is, set the background pixel to be equal to the foreground pixel.
    • Wait - that sounds like an enum and a zip?

Today

  • ✓ Tuples
    • ✓ Unpacking
    • ✓ Multiple Assignment
    • ✓ Enumerate
    • ✓ Zip
  • Bonus slides: Records.

Records

  • We can "record" things on a "record"
  • A tuple, a list, or a mysterious third thing (a class, covered Wednesday) can be a record.
  • Usually, we interact with records without thinking about whether a list or tuple is being used. >>> course = "CS", 151, "Intro Py", "Fall", 2024 >>> course ('CS', 151, 'Intro Py', 'Fall', 2024) >>> pre, num, name, sem, yr = course >>> print(pre, num) CS 151

Records

  • We can visualize records using the advising system. A course:
    • Has some prefix, like "CS" or "MATH"
    • Has some level, like "100" or "300"
      • CS: 100-level is 1yr of programming, 200-level is application, 300-level is pretty much just CS 351, which requires MATH 251.
        • CS 370 Python Data Science, offered next term, would be a 100-level class under thes guidelines - it only requires CS 151! Go take it!
        • CS 280 Human-Computer Interaction, offered every fall, is similar. If you don't want to major, CS 151 is enough to take that course!
      • MATH: 100-level is calculus/pre-calculus, 200-level requires calculus, 300-level require writing proofs (MATH 251), 400-level requires linear algebra (MATH 353)

Records

  • We can visualize records using the advising system. A course: course = "CS", 1, "Sp", 2025

Records

  • We can visualize records using the advising system. A course: course = "MATH", 2, "Sp", 2025

Records

  • Both of the following correspond to the idea of a record.
    • Tuples, and course = "CS", 1, "Sp", 2025
    • Lists course = ["CS", 1, "Sp", 2025]

Records

  • A record is an "abstraction" - a thing we use a computer to do.
  • A tuple or list is an "implementation" - a technology we use to do a thing
  • A class, taught Wednesday, is another way to work with records.

Announcements

  • Problem Set 5 tonight.
  • Project 3 next Monday.
    • Milstones :3 by Tuesday
    • Milestone 3 by Wednesday
    • Milestone 4a by Thursday
    • Milestone 4b by Friday
    • Milestone 4c by Saturday