I am following https://automatetheboringstuff.com/2e/chapter4 CHAPTER 4, lists. x = [1,2,5] x[0] ## 1. x = [1,2,[10,11,12]] x[2][2] ## 12. x[-1] ## means the last element of x. x[0:2] ## means x[0] and x[1]. It does not include x[2]. This is called a slice. With lists, you can do + and *. [1, 2, 3] + ['A', 'B', 'C'] ## [1, 2, 3, 'A', 'B', 'C'] a = [1,2,8] * 4 ## [1,2,8,1,2,8,1,2,8,1,2,8] del a[2] ## removes the 2nd element of a. 4 in a 8 not in a a = [1,2,2] size, age, hair = a ## enumerate stores the index and the value. supplies = ['pens', 'staplers', 'flamethrowers', 'binders'] for index, item in enumerate(supplies): print('Index ' + str(index) + ' in supplies is: ' + item) random.shuffle is the same as sample and random.choice = random.shuffle[0] Methods. x = [1,2,1,5,6] x.index(1) ## first time it appears. x.index(5) ## 3. x.append(5) ## makes it 1,2,1,5,6,5. x.insert(1,7) ## makes it 1,7,2,1,5,6,5. x.remove(1) ## removes the first instance. x.sort() x.reverse() ## reverses the order. ## These methods only work on lists. ## y = x.sort() doesn't work. Do x.sort(); y = x. For sorting in regular alphabetical order, do spam = ['a', 'z', 'A', 'Z'] spam.sort(key=str.lower) You can also split up a single instruction across multiple lines using the \ line continuation character at the end. Sequence data types. The Python sequence data types include lists, strings, range objects returned by range(), and tuples. You can act on these the way you do with lists. x = range(6,10) x[0] x[1] x = "rick" x[0] x[-1] "i" in x But strings are immutable. You can't do x[2] = "u". you have to instead do newx = x[0:2] + "u" + x[3] Tuples. They are just like lists but have ( instead of [. And tuples are immutable. You can use tuple() or list() to convert an object to that type. LISTS ARE STORED AS REFERENCES! So if you do y = x, and x is a list, then y is actually a reference to the same list, and if you change y you will change x. This is not true with tuples though. x = [1,2,3,4,5] y = x y[2] = 12 x y x = (1,2,3,4,5) y = list(x) z = list(x) y[2] = 12 x y z ## still 1,2,3,4,5. copy. import copy y = copy.copy(x) y[1] = 10 y x id(x) id(y) ## they are different references now! y = x id(y) ## Use copy.deepcopy(x) if the list contains other lists. The coin flip streaks exercise at the end of ch4 is good. CHAPTER 5. Dictionaries. A dictionary is a list indexed by general keys, instead of integers. Use {. x = {"house": 12, "car": 7, "shirt": 14, "bicycle": -2} x["bicycle"] The elements are not ordered. You can do list(x). x.keys() x.values() x.items() ## both These are lists. x.get("house",0) will be 12 if house is in x or 0 otherwise. x.setdefault("color",3) ## sets color to 3 only if color's not already in there. This is pretty cool. import pprint message = "It was a bright cold day in April, and the clocks were striking thirteen." count = {} for character in message: count.setdefault(character, 0) count[character] = count[character] + 1 pprint.pprint(count) CHAPTER 6. Strings. x = "It\"s mine." ## \" = " inside quotes. \' = '. \t = tab. \n = newline. \\ = \. r makes something a raw string. Backslashes will appear. Useful for directories. x = r"Users\Desktop\myfolder" x ## this will have double slashes now. print(x) ## this will look correct. Multiline strings. Use """ to start and stop. Newlines, tabs, and even quotes are saved correctly! This is great. str(x) makes the number x into a string. % seems useful. x = 'My name is %s. I am %s years old.' % (name, age) You can also use f before the quotes. x = f"My name is {name}. Next year I will be {age + 1}." upper() makes it all upper case. cool.