Lecture Notes on 27 Oct 2017 def is_magic (b): # sum the first row canon_sum = 0 for j in range (len(b[0])): canon_sum += b[0][j] # sum each row and comapre with canon_sum for i in range (len(b)): sum_n = 0 for j in range (len(b[i])): sum_n += b[i][j] if (sum_n != canon_sum): return False # sum each column and compare with canon_sum for j in range (len (b[0])): sum_n = 0 for i in range (len(b)): sum_n += b[i][j] if (sum_n != canon_sum): return False # sum the diagonal going left to right sum_lr = 0 for i in range (len(b)): sum_lr += b[i][i] if (sum_lr != canon_sum): return False # sum the diagonal going right to left sum_rl = 0 for i in range (len(b)): sum_rl += b[i][len(b) - 1 - i] if (sum_rl != canon_sum): return False return True def main (): # create a 2-D list b = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] # sum the elements in a 2-D list sum_n = 0 for row in b: for col in row: sum_n += col print (sum_n) print () sum_n = 0 for i in range (len(b)): for j in range (len(b[i])): sum_n += b[i][j] print (sum_n) print () # get the maximum in a 2-D list max_n = b[0][0] for i in range (len(b)): for j in range (len(b[i])): if (b[i][j] > max_n): max_n = b[i][j] print (max_n) print # make an exact copy of the 2-D list c = [] for i in range (len(b)): d = [] for j in range (len(b[i])): d.append (b[i][j]) c.append (d) # transpose a 2D list c = [] for j in range (len(b[0])): d = [] for i in range (len(b)): d.append (b[i][j]) c.append (d) print (c) print () # add two 2-D lists d = [] for i in range (len(b)): e = [] for j in range (len (b[i])): f = b[i][j] + c[i][j] e.append (f) d.append (e) print (d) print() main()