aa ibm cat msft ge msft ibm here is declarative-style program that calculates the total cost by summing up the second column multiplied by the third columnlines open("portfolio txt"fields (line split(for line in linesprint(sum(float( [ ]float( [ ]for in fields)in this programwe really aren' concerned with the mechanics of looping line-by-line over the file insteadwe just declare sequence of calculations to perform on all of the data not only does this approach result in highly compact codebut it also tends to run faster than this more traditional versiontotal for line in open("portfolio txt")fields line split(total +float(fields[ ]float(fields[ ]print(totalthe declarative programming style is somewhat tied to the kinds of operations programmer might perform in unix shell for instancethe preceding example using generator expressions is similar to the following one-line awk commandawk 'total +$ $ end print total }portfolio txt the declarative style of list comprehensions and generator expressions can also be used to mimic the behavior of sql select statementscommonly used when processing databases for exampleconsider these examples that work on data that has been read in list of dictionariesfields (line split(for line in open("portfolio txt")portfolio {'namef[ ]'sharesint( [ ])'pricefloat( [ ]for in fieldssome queries msft [ for in portfolio if ['name'='msft'large_holdings [ for in portfolio if ['shares']* ['price'> in factif you are using module related to database access (see )you can often use list comprehensions and database queries together all at once for examplesum(shares*cost for shares,cost in cursor execute("select sharescost from portfolio"if shares*cost > lib fl ff