''' Purpose: dataset nuance introduction ''' best_sellers = [ # outer list (each inner list is a row) [ "Name", "Author", "Language", "Date", "Sales" ], [ "Don Quixote", "de Cervantes", "Spanish", 1605, 500000000 ], [ "A Tale of Two Cities", "Dickens", "English", 1859, 200000000 ], [ "The Lord of the Rings", "Tolkien", "English", 1954, 150000000 ], [ "The Little Prince", "de Saint-Exupery", "French", 1943, 140000000 ], [ "Harry Potter", "Rowling", "English", 1997, 120000000 ], [ "The Hobbit", "Tolkien", "English", 1937, 100000000 ], [ "And Then There Were None", "Christie", "English", 1939, 100000000 ], [ "Dream of the Red Chamber", "Xueqin", "Chinese", 1754, 100000000 ], [ "Alice's Adventures in Wonderland", "Carroll", "English", 1865, 100000000 ], ] # ending bracket of the outer list (table) # print the dataset for row in best_sellers : print( row ) # print each inner list one by one print() # separate out dataset elements # best_sellers is the name of our table (our list of lists) header = best_sellers[ 0 ] # the very first row is table[0] books = best_sellers[ 1 : ] # every row after the first row (row[0]) # This is another way of slicing! # Remember we can get multiple indexes from sequences. # list1 = [[1,2],[3,4],[5,6]] # 0 1 2 # list[0] is [1,2] # list1[1: ] says give me index 1 all the way to the end! [[3,4],[5,6]] # Python will know the types of values in your lists and data even # it it's not displayed explicitly in the console when you print it. # determine Sales column sales_column = header.index( 'Sales' ) print( 'sales column:', sales_column ) print() # determine total book solds amongs the top best sellers of all time total = 0 # accumulator (start off with 0 and build up the total by # adding the sales value to this total so that we sum up the # sales value for all the books in our dataset) for book in books : # loop through each row sold = book[ sales_column ] # get the column in our row (sales column) # book[sales_column] will look into column 4 (index of 'Sales') and get the # sales value within the row total = total + sold # add the sales value from the column in each # row to our total billions_of_books = total / 1000000000 print( 'Total sold:', billions_of_books, 'billion' )