Arrays and Lists

Jed Rembold & Fred Agbo

March 3, 2025

Announcements

  • Curving on Midterm #1 completed.
  • Problem Set 4 is due today at 10pm
  • Project 2 Breakout will be released today and due next week Monday at 10pm
  • Polling continues here

Quick General Poll

Comparatively speaking, how would you say you are doing in absorbing and keeping up with the material in this class?

  1. “Probably better than the majority of my peers”
  2. “Probably about the same as the majority of my peers”
  3. “Probably worse than the majority of my peers”
  4. “Probably way worse than the majority of my peers”

Arrays and Lists

  • From the earliest days, programming languages have supported the idea of an array, or an ordered sequence of values.
  • Individual values in an array are called elements, and the number of elements is the length of the array.
  • Each element’s position in the array is given by its index, with index numbers starting at 0 and extending up to 1 less than the length of the array
  • Python implements the array concept in a bit more general form called a list.

Reminder: Making Lists

  • Created using square brackets with elements separated by commas:

    COIN_VALUES = [1, 5, 10, 25, 50, 100]
    COIN_NAMES = [ "penny", "nickle", "dime",
                   "quarter", "half-dollar", "dollar" ]
  • Lists are commonly represented visually or conceptually as a series of numbered boxes:

What we already know

  • Can retrieve the value of any element in a list by writing the index of that element in square brackets after the list name

    • COIN_VALUES[3]25
    • COIN_NAMES[2]"dime"
  • Can concatenate two lists to form a new list with elements from both

    • [1,2,3] + [4,5,6][1,2,3,4,5,6]
  • Can loop through each of the list elements in turn

    for elem in my_list:
      # do stuff with elem

    where elem is the name of the variable that will sequentially get assigned the value of every element in my_list

Sequences

  • Sound familiar? Lists are just like more general strings!
  • Strings and lists are examples of a more general class of object in Python called sequences
  • We already knew that all sequences support:
    • The len function
    • Index numbering starting at 0
    • Concatenation using + or +=
    • Selection of an individual element using square brackets
    • Looping over elements
  • We can now add:
    • Negative index numbering counting backwards from the end
    • Slicing in all forms
    • Comparing sequences
    • Repetition using *
    • Inclusion testing using in operator

Understanding Check!

What would the below expression evaluate to?

[“One”, 2, True][-1:1:-1][1]
  1. "n"
  2. 2
  3. True
  4. None of the above, or this will error

Project 2: Breakout!

  • Project 2 is recreating the classic arcade game Breakout!
  • Guide is posted here, and due by Monday March 10 at 10 PM
  • Start early to have more time to learn and try out things!

Breakout History

  • The popular Breakout arcade game was released by Atari in 1976
  • Atari founder Nolan Bushnell wanted a new game that would build on the success of the earlier game Pong. He assigned Steve Jobs to develop the game, promising a bonus if the game required a small number of chips.
  • As Wikipedia tells the story, “Jobs had little specialized knowledge of circuit board design but knew [his friend Steve] Wozniak was capable of producing designs with a small number of chips. He convinced Wozniak to work with him, promising to split the fee evenly between them.”
  • Wozniak completed the game design in four days, but Jobs never told him about the bonus offer. Jobs was paid \(\$5,000\), but Wozniak received only \(\$350\).
  • Jobs and Wozniak co-founded Apple Computer the following year, which has grown to be the largest corporation in the world by market capitalization.

Breakout Basics

  • Breakout is a game in which the player attempts to break all the colored bricks by causing a bouncing ball to collide with them
  • The player controls a paddle at the bottom of the screen which the ball will bounce off
    • The paddle can only move left and right
  • If the ball makes it past the paddle to the bottom of the screen, the player loses a life
    • Lose 3 lives and it is game over!

Breakout Milestones

  • Breakout is broken up over 5 milestones
  • You have already seen or written pieces of similar code to many of the milestones!
    • Milestone 1: PS4 brick pyramid
    • Milestone 3: Section bouncy ball problem (this week)

Milestone 1

Milestone 2

Milestone 3

Milestone 4

Milestone 5

// reveal.js plugins