3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] Python Type "help", "copyright", "credits" or "license" for more information. # Lists # Lists are another python type # Unlike the types we've seen so far, lists can store # *multiple* data types at the same time s = 'csc' n = 120 t = True # We can create a list using [ ] L = [s, n, t] L ['csc', 120, True] type(L) # you can use literals to create lists (without variable names) L2 = ['hello', 120] L2 ['hello', 120] L3 = [1, 5654, 32, 90] # Usually only one type in the list, but in some cases you might # want multiple types L3 [1, 5654, 32, 90] # A list is ordered # just like strings, values are ordered from left to right # We can index the list using square brackets, just like strings L ['csc', 120, True] L[0] 'csc' L[1] 120 L[2] True L[3] Traceback (most recent call last): Python Shell, prompt 26, line 1 builtins.IndexError: list index out of range # each value in the list is reffered to as an 'element' # "L[0] is the element in L at index 0" type(L[0]) type(L[1]) type(L[2]) type(L) # List slicing # just like with strings, we can get a slice of a list in Python L4 = [3, 4, 6, 7, 8] L4[0:3] [3, 4, 6] # L[start:stop] slices the list from start up to but not including # stop L4[3:4] [7] L4[3:5] [7, 8] L4[2:-1] [6, 7] L4[-1] 8 L4[::2] [3, 6, 8] # Nested lists # you can put lists inside of lists K = [[1, 2, "csc120"], [3, 4, "csc108"]] K [[1, 2, 'csc120'], [3, 4, 'csc108']] len(K) 2 # len(K) gives back the number of elements in K # any list by itself is considered one element # since K has two lists in it, len(K) = 2 K[0] [1, 2, 'csc120'] K[1] [3, 4, 'csc108'] # how do we get just 'csc120'? K[0][0] 1 K[0][2] 'csc120' K[0] [1, 2, 'csc120'] K[0][2] 'csc120' K[0] [1, 2, 'csc120'] K[1] [3, 4, 'csc108'] # these two lists are the 'inner' lists of K # we can index values within the inner lists by first indexing # K to get the one we want # and then indexing the values within that inner list K[0][1] 2 # ^ we first indexed the inner list at K[0] # and then indexed the inner list at index 1 - K[0][1] K[1][0] 3 K[1][1] 4