Week 5 - Mouse input, list methods, dictionaries
Week 5 - Mouse input, list methods, dictionaries#
Read mouse input
Learn about list methods and dictionaries
Week 5a - Mouse Input and More Lists#
What does the mouseclick handler receive as an input?
… write a little bit debug it, write a little bit debug it. What is the advantage of this?
We have been introduced list indexing in the first part. Which other list operations do you know now?
How can we rewrite
What is the difference between
if ball_pos in ball_list:
for ball_pos in ball_list:
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?
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, ball], pos) < ball_radius: ball = 'Green' changed = True if not changed: ball_list.append([pos, pos, 'Red'])
Modify it a little bit, keep it working, run it again. What is the advantage of this?
What is iteration?
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#
Why do you think the
dictdatatype is called dictionary?
What do you get when you iterate over a
dict? Key or value?
What do you get when you iterate over
You see that a variable name in Python is written in capital letters. What does this mean?
Imagine you have images on your harddrive that you want to use in Codeskulptor. How can you to that?
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.
John mentions the following expression:
[n ** 2 for n in numbers].
What is the output of this expression?
How is this called?
How can we rewrite the following code as a list comprehension?
r =  for x in xs: if x % 2 == 0: r.append(x)
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#
John mentions that the order of the elements in a
dictwill not have the same order as you typed them in. It is not true in modern Python — the order will be kept.
Why does the following code fail?
d = dict() d[(1,2)] = 1 d[[1,2]] = 1