U-M CIS 400 - Groceries Exercise Part I

Unformatted text preview:

8 2 Groceries Exercise Part I Intro to Python Programming Michael Rossetti Accessing and Manipulating Data in Python Working with Lists and Dictionaries In this exercise we are provided with a list of dictionaries called products Each dictionary in products represents a product and has keys for id name department aisle and price Accessing List Items To access an item in the list we use square brackets and pass in the index of the item we want to access Remember that the first item in a list has an index of 0 Accessing Dictionary Keys To access a value in a dictionary we use square brackets and pass in the key of the value we want to access We can also use the keys method to get a list of all the keys in a dictionary Printing Data We can print data in Python using the print function We can concatenate strings and variables using the operator or use string interpolation with f strings Exercise Our goal is to display a header that shows the number of products in products and then print each product s name and price on its own line Header We can use one of three methods to print our header depending on our preference Using commas to separate items in the print function Concatenating strings and variables with the operator Using f strings for string interpolation Main Objective To print each product s name and price on its own line we can loop through the products list and use string interpolation to format the output Code Header print There are len products products print There are str len products products print f There are len products products Print each product s name and price for product in products print f product name product price Looping Through and Printing Products To start we want to loop through the list of products and print each one We will use a for loop to iterate through the list of products and print the name and price of each product We will start by printing the entire product for each item in the list for product in products print product Next we will print just the name and price of each product for product in products print product name product price To format the price as a dollar sign we will use a function called 2usd This function rounds the price and adds a dollar sign to the beginning for product in products print product name 2usd product price


View Full Document

U-M CIS 400 - Groceries Exercise Part I

Documents in this Course
Load more
Download Groceries Exercise Part I
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Groceries Exercise Part I and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Groceries Exercise Part I 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?