← Teacher guide Markdown for Prepare

Lists and dictionaries: cheat sheet

Deeper Python in the console · In any playground, open Reference to copy these.

while loop

Repeat the indented lines for as long as the condition is true.

while True:
    pass
Example
r = 10
while r < 60:
    circle(r)
    r = r + 15
What it draws
A circle each time, while r < 60

break

Leave the loop straight away; the program carries on after it.

break

dictionary value

The value stored under that key. The key goes in quotes. Assign to it to change it: star["speed"] = 8.

star["speed"]

join

Join a list of texts into one, with the text in quotes between each: "lamp, key".

", ".join(bag)

remove from a list

Take an item out of a list. It stops with a ValueError if the item is not there, so check with in first.

bag.remove("lamp")

menu loop

A menu that keeps showing until a break runs.

while True:
    choice = input("1 add, 2 quit: ")
    if choice == "1":
        pass
    elif choice == "2":
        break
    else:
        print("Please choose 1 or 2.")

list

Keep many values in order under one name. The first item is place 0.

colours = ["red", "blue", "green"]

dictionary

Values stored under names (keys). One dictionary can hold everything about one thing.

star = {"speed": 5, "points": 1}

in

True if the item is in the list (or the key is in the dictionary, or the letters are in the text).

if item in bag:
    pass