Lists

Mon W1

Announcements

  • Welcome to IDS-101-13: Thinking Machines!
  • It's other first week of class!
  • Today we're doing something interesting I hope
    • "Arithmetic is boring." -everyone except me
    • "Long division? I'm in college." -everyone except me
    • "Can I teach them something cool?" -Shouvik
    • "We don't have to be having fun for it to be fun." -me
  • How about "data" - I hear "data" is cool.

Data

  • Today's lecture brought to you by the University's "Strategic Plan"
  • There are three "areas of focus":
    • Big data and human-centered computing
    • Democratic Institutions
    • Climate Change

Be sure to introduce yourselves!

Group 1Group 2Group 3Group 4

Question #1

Whose values are reflected in the University's Strategic Goals?
  • Think about a possible answer on your own
  • Discuss your answers with the rest of the group
  • Record a summary of each group’s discussion

Big data and human-centered computing

Democratic Institutions

Climate Change

Question #2

How can thinking machines help us explore the idea of "data"?
  • Think about a possible answer on your own
  • Discuss your answers with the rest of the group
  • Record a summary of each group’s discussion

Python

  • Recall we have thematically introduced annoyance to resolve in latter lectures.
  • I found writing out this expression "annoying": triple_check = lambda a, b, c: print(a*a + b*b == c*c)
  • Thinking machines are better at some things than I am, like:
    • Arithmetic
    • Doing the same task over and over.
  • So I should find a way to ask the machine to do this for me.
    • I want a way to say "take these variables and do the same the same thing to all of them.
    • Python has my favorite way to do that of any programming language!
    • Those "things" could be information about electors... or whatever.

Tuple

  • Tuples are the (one of the) ways Python solves this problem.
  • Instead of saying, here are three variables "a", "b", and "c" a, b, c = 6, 8, 10
  • We can make a variable for the whole triple, say "t". t = 6, 8, 10
  • This, uh, does so happen to break "triple_check" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-10-267dfa60672e> in <cell line: 1>() ----> 1 triple_check(t) TypeError: triple_check() missing 2 required positional arguments: 'b' and 'c'

Question #3

Read the error message.
What do you think it means?
How might you fix (or try to fix) it?
  • Think about a possible answer on your own
  • Discuss your answers with the rest of the group
  • Record a summary of each group’s discussion

List

  • Okay so we have our triple: t = 6, 8, 10
  • I always get nervous about this way of writing because to me it feels ambigious.
    • What would the following mean: b = x, y a, b = 1,2,3
  • Here is another, more explicit, way to put multiple things together. t = [6, 8, 10]
  • This forms a list, and I like using lists better because...
    • Visually, they are enclosed in square brackets "[]" which I find easy to see.
    • Lists have a nice feature - comprehension - that we can use.

Comprehension

  • Now say we want to "check" this list t = [6, 8, 10]
  • Previously we did every square individually triple_check = lambda a, b, c: print(a*a + b*b == c*c)
  • List comprehension gives us a way to do more than just one arithmetic operation at a timesquares = [x*x for x in t]

Comprehension

  • Exercise A.7 Write a function that prints a list with 7 added to all numbers in the list.
    • Consider the following: squares = [x*x for x in t]
    • What does this code do?
      • What is 'squares'?
      • What is 'x*x'?
      • What is 'for x'?
      • What is 'in t'?
    • Write a function, say "add_seven()"...

Question #4

What process do you use to understand code?
  • Think about a possible answer on your own
  • Discuss your answers with the rest of the group
  • Record a summary of each group’s discussion

Indices

  • Exercise A.8 Write a function prints the final element of a list.
    • Consider the following: conts = ['Africa', 'Americas', 'Antarctica', 'Eurasia', 'Oceania'] print(conts[1])
    • What does the 1 in this code do?
    • What happens if you change it to
      • Large values, like 400 or 400 ** 400?
      • Non-integer values, like 0.5?
      • Anything else?
    • Write a function, say "print_last()"...

Question #5

What are the uses and limitations of creativity in Python?
  • Think about a possible answer on your own
  • Discuss your answers with the rest of the group
  • Record a summary of each group’s discussion

Homework

  • Homework 2 Order of Operations
    • There is this arithmetic concept called "order of operations" sometimes called PEMDAS:
      • Parenthesis
      • Exponentiation
      • Multiplication/Division
      • Addition/Subtraction
    • Conduct a series of tests to determine whether Python follows PEMDAS rules or not.
    • You may make a copy of this Notebook and share it with me and anyone with whom you collaborate.
      • Give Shouvik and I edit permissions.
      • Due Fri @ 12 Noon (so we're able to discuss it in class)
      • You may only give your peers view or comment permissions.