In this part of my Learn to Program series we cover one of the most important topics which is lists.We’ll look at numerous list functions, how to sort lists with the Bubble Sort, List Comprehensions, simple Debugging and much more.
This tutorial series is all about learning how to solve problems, and those problems get more complex with each tutorial. Make sure you watch the first part before moving on. The code follows the video below.
If you like videos like this consider contributing a dollar on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# ---------- LEARN TO PROGRAM 6 ---------- import random import math # With lists we can refer to groups of data with 1 name # Each item in the list corresponds to a number (index) # just like how people have identification numbers. # By default the 1st item in a list has the index 0 # [0 : "string"] [1 : 1.234] [2 : 28] [3 : "c"] # Python lists can grow in size and can contain data # of any type randList = ["string", 1.234, 28] # Create a list with range oneToTen = list(range(10)) # An awesome thing about lists is that you can use many # of the same functions with them that you used with strings # Combine lists randList = randList + oneToTen # Get the 1st item with an index print(randList[0]) # Get the length print("List Length :", len(randList)) # Slice a list to get 1st 3 items first3 = randList[0:3] # Cycle through the list and print the index for i in first3: print("{} : {}".format(first3.index(i), i)) # You can repeat a list item with * print(first3[0] * 3) # You can see if a list contains an item print("string" in first3) # You can get the index of a matching item print("Index of string :", first3.index("string")) # Find out how many times an item is in the list print("How many strings :", first3.count("string")) # You can change a list item first3[0] = "New String" for i in first3: print("{} : {}".format(first3.index(i), i)) # Append a value to the end of a list first3.append("Another") # ---------- PROBLEM : CREATE A RANDOM LIST ---------- # Generate a random list of 5 values between 1 and 9 numList = [] for i in range(5): numList.append(random.randrange(1, 9)) # ---------- SORT A LIST : BUBBLE SORT ---------- # The Bubble sort is a way to sort a list # It works this way # 1. An outer loop decreases in size each time # 2. The goal is to have the largest number at the end of # the list when the outer loop completes 1 cycle # 3. The inner loop starts comparing indexes at the beginning # of the loop # 4. Check if list[Index] > list[Index + 1] # 5. If so swap the index values # 6. When the inner loop completes the largest number is at # the end of the list # 7. Decrement the outer loop by 1 # Create the value that will decrement for the outer loop # Its value is the last index in the list i = len(numList) - 1 while i > 1: j = 0 while j < i: # Tracks the comparison of index values print("\nIs {} > {}".format(numList[j], numList[j+1])) print() # If the value on the left is bigger switch values if numList[j] > numList[j+1]: print("Switch") temp = numList[j] numList[j] = numList[j + 1] numList[j + 1] = temp else: print("Don't Switch") j += 1 # Track changes to the list for k in numList: print(k, end=", ") print() print("END OF ROUND") i -= 1 for k in numList: print(k, end=", ") print() # ---------- MORE LIST FUNCTIONS ---------- numList = [] for i in range(5): numList.append(random.randrange(1, 9)) # Sort a list numList.sort() # Reverse a list numList.reverse() for k in numList: print(k, end=", ") print() # Insert value at index insert(index, value) numList.insert(5, 10) # Delete first occurrence of value numList.remove(10) for k in numList: print(k, end=", ") print() # Remove item at index numList.pop(2) for k in numList: print(k, end=", ") print() # ---------- LIST COMPREHENSIONS ---------- # You can construct lists in interesting ways using # list comprehensions # Perform an operation on each item in the list # Create a list of even values evenList = [i*2 for i in range(10)] for k in evenList: print(k, end=", ") print() # List of lists containing values to the power of # 2, 3, 4 numList = [1,2,3,4,5] listOfValues = [[math.pow(m, 2), math.pow(m, 3), math.pow(m, 4)] for m in numList] for k in listOfValues: print(k) print() # Create a 10 x 10 list multiDList = [[0] * 10 for i in range(10)] # Change a value in the multidimensional list multiDList[0][1] = 10 # Get the 2nd item in the 1st list # It may help to think of it as the 2nd item in the 1st row print(multiDList[0][1]) # Get the 2nd item in the 2nd list print(multiDList[1][1]) # ---------- MULTIDIMENSIONAL LIST ---------- # Show how indexes work with a multidimensional list listTable = [[0] * 10 for i in range(10)] for i in range(10): for j in range(10): listTable[i][j] = "{} : {}".format(i, j) for i in range(10): for j in range(10): print(listTable[i][j], end=" || ") print() # ---------- PROBLEM : CREATE MULTIPLICATION TABLE ---------- # With 2 for loops fill the cells in a multidimensional # list with a multiplication table using values 1 - 9 ''' 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9, 12, 15, 18, 21, 24, 27, 4, 8, 12, 16, 20, 24, 28, 32, 36, 5, 10, 15, 20, 25, 30, 35, 40, 45, 6, 12, 18, 24, 30, 36, 42, 48, 54, 7, 14, 21, 28, 35, 42, 49, 56, 63, 8, 16, 24, 32, 40, 48, 56, 64, 72, 9, 18, 27, 36, 45, 54, 63, 72, 81 ''' # Create the multidimensional list multTable = [[0] * 10 for i in range(10)] # This will increment for each row for i in range(1, 10): # This will increment for each item in the row for j in range(1, 10): # Assign the value to the cell multTable[i][j] = i * j # Output the data in the same way you assigned it for i in range(1, 10): for j in range(1, 10): print(multTable[i][j], end=", ") print() |
Leave a Reply