''' Purpose: introductory take on examining a table of values ''' # MAKE SURE YOU RECOGNIZE THIS # define table / dataset # a table is a list of lists !!! # each sublist = row # each row = column/element in that sublist table = [ [ "A", "B", "C" ], [ "D", "E", "F" ], [ "G", "H", "I" ], [ "J", "K", "L", "M" ] ] # table[0] is a list in itself --> ['A', 'B', 'C'] print( "table:", table ) print() # determine number of rows in table # for a list of lists, the number of elements is how many sublists there are in the biggest list # aka how many rows are there in the table/dataset nrows = len( table ) # if we find the len( a_row ) --> tells us the number of columns in that row a_row # each row does not have to have the same number of columns print( "the table has", nrows, "rows" ) print() # print each row of the table along withs number of columns # row is not a keyword, we're just calling it row because it represents a sublist in the table # which we refer to as a "row" for row in table : print( row ) ncols = len( row ) # since row is a list in itself, we can use len() to find the number of elements # or number of columns in this row # print( "row", row, "has", ncols, "columns" ) # 1st time: row = ['A', 'B', 'C'] --> this is a LIST!!! --> can use len() to find the number of things # in this list (which is 3)! the number of elements in each of these sublist is also the number of columns! # 2nd time: row = ['D', 'E', 'F'] --> same process # so on # going through one for loop in a table always processes each row in that table print() # print each row of the table using row indices # remember range of indices always starts at 0 and go up to but not including the length of the list for r in range( 0, nrows ) : row = table[ r ] # get current index of the table --> the element at each index is a list/row # we can also get a specific element from a list using the [] notation # in between the [] is the index/position in the list you want to peek into print( "row", r, ":", row )