''' Purpose: introductory take on examining a table of values ''' # define table # You can think of it like an excel spreadsheet with rows and columns. table = [ ["A", "B", "C" ], [ "D", "E", "F" ], [ "G", "H", "I" ], [ "J", "K", "L", "M" ] ] # You can have varying numbers or rows/columns. # [[1],[2,3],[4,5,6]] is also a valid table. # A table/dataset (same term) is a list of lists! # [[],[],[]] # Each smaller list in your larger list is a row! # Each element in your smaller list is a cell. print( "table:", table ) # prints out the list of lists print() # determine number of rows in table nrows = len( table ) # Remember how len( list ) gives you the number of elements? # The elements in a dataset are the small lists (the rows!) # So to get the number of rows (number of small lists), # we can do len( dataset ) print( "the table has", nrows, "rows" ) print() # print each row of the table along withs number of columns for row in table : # We are looking at each small list in our table ncols = len( row ) # Get the number of elements in each small list (row) print( "row", row, "has", ncols, "columns" ) # print it out # Ex. dataset is [[1,2],[3,4,5]] # len(dataset) in this case is 2 (2 rows = 2 small lists) # for row in table: # ncols = len( row ) # in my example it will be 2 columns for the first # small list [1,2] and 3 columns in the second small list [3,4,5] # Yes, each element in the smaller list is a cell/column. # [ [ 1 , 2 , 3 ] , [4 ,5 ]] # cell cell cell cell cell # row row print() # print each row of the table using row indices for r in range( 0, nrows ) : # we are looping through the indexes for our rows row = table[ r ] # We are getting the row in the dataset using the index print( "row", r, ":", row ) # [[], [], [],[]] # 0 1 2 3 # nrows = len( table ) = number of rows / number of small lists # 4 rows indexes 0-3 print() # individually print each row and column of the table for r in range( 0, nrows ) : # Loop through indexes for our rows (0 to #rows-1) row = table[ r ] # Get the row in the table using the index ncols = len( row ) # Get the number of cells in the row # (# elements in small list) print( "row", r, ":", row ) # print the row and the row index! for c in range( 0, ncols ) : # cell = row[ c ] # get the element within the row at index c cell = table[r][c] # lines 68 and 69 are the same thing print( " column", c, "of row", r, ":", cell ) table[r][c] = cell.lower() # convert each cell to lowercase string print() # print the table in people form for r in range( 0, nrows ) : ncols = len( table[ r ] ) for c in range( 0, ncols ) : cell = table[ r ][ c ] print( cell, end=" " ) print()