Slides 3: Types

Exercise 3.b.1

What data type would you use to represent the following?

  1. A person's first name
  2. The first names of all the students in this course
  3. The Quiz 1 scores for each of those students
  4. A person's birth date (year, month, and day)
  5. An address book entry (name, email, major, …)
  6. A person's age in years
  7. A person's height in meters

Write an example assignment statement for each.

Example Assignment Statements

a = "Alice"
b = ["Alice", "Bob", "Charlie"]
c = [90, 85, 95]
# or: c = {"Alice": 90, "Bob": 85, "Charlie": 95}
d = (2000, 1, 1)
e = {"name": "Alice", "email": "ak12@calvin.edu", "major": "Biology"}
f = 20
g = 1.75

Assume x = 'alphabet' How could you get the b?

  • x(6)
  • x[6]
  • x[5]
  • x(-4)
  • x{-3}

Exercise 3.b.2

Generally speaking, one can view strings as lists of characters, but …

Exercise 3.b.2.a

Can you predict what the code segments do to the value of x and explain why they work that way?

x = '12'
x[1] = '3'
x = [1, 2]
x[1] = 3

Exercise 3.b.2.b

Can you predict what the code segments do to the values of x and y and explain why they work that way?

x = 'hi'
y = x
x.upper()
x = ['h', 'i']
y = x
x[1] = 'o'

Exercise 3.b.2.c

How would similar code segments work on integers, floats, dictionaries and tuples?

Name-Object Diagrams

Draw the name-object diagram for the following code:

s = "bht"
s.upper()
print(s)

Note that the upper() method creates a string with all uppercase letters.

Strings are immutable. The upper() method returns a new string.

Name-Object Diagrams with Lists

a = [3, 2, 1]
b = a
b[0] = 50
c = b + a
print(c)
  • [50, 2, 1, 3, 2, 1]
  • [50, 2, 1, 50, 2, 1]
  • [100, 4, 2]
  • [53, 4, 2]
  • None of the above

Name-Object Diagrams with Lists, 2

a = [3, 2, 1]
b = a.copy()
b[0] = 50
c = b + a
print(c)
  • [50, 2, 1, 3, 2, 1]
  • [50, 2, 1, 50, 2, 1]
  • [100, 4, 2]
  • [53, 4, 2]
  • None of the above

Exercise 3.c

Which of the following assertions makes the most sense to you?

  1. “[Technology is] essentially amoral, a thing apart from values, an instrument which can be used for good or ill.” — R. Buchanan, Technology and Social Progress, 1965.

  2. “Embedded in every tool is an ideological bias, a predisposition to construct the world as one thing rather than another, to value one thing over another, to amplify one sense or skill or attitude more loudly than another.” — N. Postman, Technopoly: The Surrender of Culture to Technology, 1992.