Loops
Dictionary {}
A dictionary is key and value pairs. There are a few ways to do this. One way is to do it this way: students = { "Harry": "Gryffindor", "Hermione": "Gryffindor", "Ron": "Gryffindor", "Draco": "Slytherin", } for student in students: print(student, students[student], sep = ": " Harry: Gryffindor Hermione: Gryffindor Ron: Gryffindor Draco: Gryffindor If there are multiple things for each student such as house, patronus, etc then we need to make a list of dictionaries!
Can multiply variables in print
In python you can multiply inside of functions Ex: print("meow\n" *3, end = "") python cat.py meow meow meow Ex: def main(): square(3) def square(size): for i in range(size): for j in range(size): print("#", end ="") print() instead of doing this, we can do this def main(): square(3) def square(size): for i in range(size): print("#" * size)
update()
Method to add multiple keys at once. Update takes an arguemtn as an input into another dictionary. It will take that dictionarys keys and values and just add them to the dictionary. Ex: spacecraft.update({"distance": "0.01", "orbit": "sun"})
.get()
Method used to try and access a key. If the key doesn't exist then we will get some other value we specified instead. Ex: Distance: {spacecraft.get("distance", "unknown")} This will return the distance of spacecraft or if there is no distance it will return unknown.
variable.insert()
This adds into a specific location in the list. First you put the location that you want and then the value you want added. results.insert(0, "Bowser")
for loop for strings
This is the better one to use for strings. names = ["Mario", "Luigi", "Daisy", "Yoshi"] for name in names: print(name) name is the variable we create for "Mario", etc to iterate over the names list! For loops work really well to iterate over lists.
variable.append()
This is to append(add) a value onto the end of a list. results.append("Mario")
variable.remove()
This is to remove an item from the list. results.remove("Mario") removing a few values results.remove(["Mario", "Luigi"])
for loops in dictionaries
When you use a for loop in python to iterate over a dictionary, by design it will iterate over the keys. You have to use the key to index into the dictionary to iterate over the value. Ex: students[student] if the students name is the key then it will go to this location and get the value
while
While is a way that we can express a loop, a while loop *You have to be careful with while loops because you can cause an infinite loop, looping forever!! ctrl c will stop an infinite loop. Ex: i = 3 while i != o: print("Meow") i = i - 1 or i -=1 3, loop back, 2 loop back, 1 loop back and stop when it hits 0. SAME CODE i = 0 while i < 3: print("Meow") i = i +1 OR more generally i += 1
lists[ ]
store values in a list and is denoted by square brackets
Dictionaries{}
Dictionaries are useful when you want to store similar kinds of information such as key-value pairs. Dictionaries use curly braces. They come with keys and values. A key is simply some name, so accessing some particular value inside of this dictionary to get the value.
For Loop
For loops are useful when you know how many times you want to loop or when you want to do something for each item you have in some list or more generally, something that can be iterated over. Allows you iterate over a list of items.
None
Special keyword in python that means there is no value and the first letter needs to be capitalized!
variable.extend()
This adds a list onto the end of the list. results.extend(["Bowser", "Mario"])
range()
range() This will iterate a certain amount of times that you specify. Ex: for i in range(3): print("meow") Common when doing this to use an _ instead of i so for _ in range(3) To get the length of a list: Ex: names = ["Mario", "Luigi", "Daisy", "Yoshi"] for i in range(len(names)): print(i). This will print numbers so 0, 1, 2, 3 \With range, you always have to index into the location that you want since it is integers, but this will print out the name Ex: names = ["Mario", "Luigi", "Daisy", "Yoshi"] for i in range(len(names)): print(names[i]) this indexes into i's location in names and prints out the names, but there is an easier way to do this.
List of dictionary [{ }]
students = [ {"name": "Harry", "house": "Gryffindor", "patronus": "Stag"}, {"name": "Hermione", "house": "Gryffindor", "patronus": "Otter"}, {"name": "Ron", "house": "Gryffindor", "patronus": "Jack Russell Terrier"}, {"name": "Draco", "house": "Slytherin", "patronus": None} ] for student in students: print(student["name"], student["house"], student["patronus"], sep = ": " Harry: Gryffindor: Stag Hermione: Gryffindor: Otter Ron: Gryffindor: Jack Russell Terrier Draco: Slytherin: None Each student is a dictionary and required curly braces!!! So we create a list of dictionaries for each student.
