APCSP Unit 3

AP Vocab

  • A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.

  • An index is a common method for referencing the elements in a list or string using natural numbers.

  • An element is an individual value in a list that is assigned a unique index.


Groups

  • A group is a collection of shapes we treat as a single shape.

  • g = Group(shape1, shape2, shape3) makes a group with three shapes.

  • g.centerX += 5 will move every shape in the group to the right by 5.

  • Make sure each shape in the group is separated by a comma, and you have both parentheses.

Group Properties
  • Only the following properties work with groups.

  • centerX and centerY refer to the center of the group.

  • right, left, top, and bottom refer to the edges of the group if you drew a box around every shape in the group.

  • fill, opacity, visible will change these properties for every shape in the group.

  • rotateAngle rotates the entire group around the center of the group.

  • group.children is a list of every shape in the group.

Traversing Groups
  • If you want to do the same thing to every shape in a group, loop through group.children.

  • for shape in group.children: lets you access the properties of each individual shape.

  • Test shape.hits(mouseX, mouseY) in a loop to see which shape in a group is hit.

  • If a shape in the group is made invisible, it is removed from the group!


Variables

Global Variables
  • Global variables are defined outside of functions, and can be used anywhere in your code below the line where you define your variable.

  • Only use globals when storing shapes or groups that you want to change later.

Local Variables
  • Function parameters are local variables.

  • Locals can only be used within the function in which it is defined.

  • Helper variables are a useful kind of local variable that make your coded easier to read.

  • Using a color variable might help you set the color of multiple shapes to the same thing.

  • Use locals as much as you can!

  • Do not name them the same thing as a global variable.


Lists

  • Lists can store multiple values like colorList = ['red', 'green']

  • group.children is a list.

  • for color in colorList: will loop through each color in the list, assigning it to color.

  • Indexing into a list is how you access elements of the list without a loop.

    • Indices start at 0!

    • colorList[0] is 'red'

    • colorList[1] is 'green'

    • colorList[2] causes an out of bounds error! (there are only two elements)

  • colorList[0] = 'purple' changes colorList to ['purple', 'green']

Cycling Through Lists
  • You need a counter for your index which goes back to 0 when you reach an invalid index.

  • Use the mod operator! % is a remainder operator. (4 % 3 is 1)

  • counter % length of list will never be out of bounds, and rotates through the indices.


List Methods

If we have a list like colorList = ['red', 'red', 'yellow'] we can use these methods:

  • colorsList.append('green') puts 'green' at the end.

    • ['red', 'red', 'yellow', 'green']

  • colorList.insert(1, 'orange') inserts 'orange' at index 1.

    • ['red', 'orange', 'red', 'yellow', 'green']

  • colorList.remove('red') removes the first 'red' from the list.

    • ['orange', 'red', 'yellow', 'green']

  • colorList.pop(2) removes the element at index 2.

    • ['orange', 'red', 'green']

  • AP Exam: APPEND and INSERT are the same on the AP Exam, but REMOVE works like pop()


Back to Lessons