Week 5 - Mouse input, list methods, dictionaries#

Goals#

  • Read mouse input

  • Learn about list methods and dictionaries

  • Draw images

Week 5a - Mouse Input and More Lists#

Mouse input#

  1. What does the mouseclick handler receive as an input?

  2. Joe recommends:

    … write a little bit debug it, write a little bit debug it. What is the advantage of this?

List Methods#

  1. We have been introduced list indexing in the first part. Which other list operations do you know now?

  2. How can we rewrite list.remove(value) using list.index() and list.pop()?

List Examples#

  1. What is the difference between

    • if ball_pos in ball_list:

    • for ball_pos in ball_list:

    ?

  2. Scott uses a flag to note whether he changed the color of a ball or not. If no color was changed, then he adds a new ball to the list. Which datatype has this flag?

  3. When Scott clicks on the intersection of multiple balls, then all these balls turn green. What is the reason? Here is an excerpt:

    def click(pos):
    	changed = False
    	for ball in ball_list:
    		if distance([ball[0], ball[1]], pos) < ball_radius:
    			ball[2] = 'Green'
    			changed = True
    
    	if not changed:
    		ball_list.append([pos[0], pos[1], 'Red'])
    
  4. Scott recommends:

    Modify it a little bit, keep it working, run it again. What is the advantage of this?

Iteration#

  1. What is iteration?

  2. Typically, iterating over a list and changing the items at the same list does not work. What can we do in this case?

Week 5b - Dictionaries and Images#

Dictionaries#

  1. Why do you think the dict datatype is called dictionary?

  2. What do you get when you iterate over a dict? Key or value?

  3. What do you get when you iterate over dict.items()?

  4. You see that a variable name in Python is written in capital letters. What does this mean?

Images#

  1. Imagine you have images on your harddrive that you want to use in Codeskulptor. How can you to that?

  2. If you were confused about the question at the end of the video, here is an explanation.

    … in the upper left corner of the canvas?

    means that you want to paste the 50x50 image to a 100x100 canvas, but you want to include (0, 0) of the canvas when you paste it. For that you have to increase the pasted size to 100x100.

Visualizing Iteration#

  1. John mentions the following expression: [n ** 2 for n in numbers].

    1. What is the output of this expression?

    2. How is this called?

  2. How can we rewrite the following code as a list comprehension?

    r = []
    for x in xs:
        if x % 2 == 0:
            r.append(x)
    
  3. John mentions the concept unpacking in context of the following code:

    for ch in message:
        for key, value in CIPHER.items():
            if ch == value:
                dmsg += key
    

    What does get unpacked here?

Programming Tips -5#

  1. John mentions that the order of the elements in a dict will not have the same order as you typed them in. It is not true in modern Python — the order will be kept.

  2. Why does the following code fail?

    d = dict()
    d[(1,2)] = 1
    d[[1,2]] = 1
    

Mini-project #5 - Memory#