def rowsum(matrix): results = [] for row in matrix: results.append(sum(row)) return results def columnsum(matrix): rows = len(matrix) cols = len(matrix[0]) result = [] for c in range(cols): sum = 0 for r in range(rows): sum += matrix[r][c] result.append(sum) return result def diagonal(matrix): diagonal = [] rows = len(matrix) for row in range(rows): diagonal.append(matrix[row][row]) return diagonal def show(m): print matrixstring(m) def matrixstring(m): rows = len(m) cols = len(m[0]) result = "" for r in range(rows): for c in range(cols): result += str(m[r][c]).rjust(4) result += "\n" return result def generate(rows, cols): import random m = [] for r in range(rows): m.append([]) for c in range(cols): m[r].append(random.randrange(100)) return m