"Groceries" Exercise

Learning Objectives

  • Gain familiarity with the Python programming language, focusing on datatypes and looping.

  • Practice processing in-memory Python data structures into a desired information output.

  • Practice using a text editor to edit and save files of Python code.

  • Practice incorporating version control into your development process.

  • Practice importing and accessing functionality provided by Python modules.

Instructions

Iteratively develop a Python program which will process a provided data structure into a desired human-friendly output. The program's functionality should adhere to the "Basic Requirements" below.

Before attempting to implement the basic requirements, take some time to configure your project repository according to the "Setup" instructions below. After doing so, you'll have a remote repo on GitHub.com and a local copy on your computer within which to develop.

When developing, as you reach key milestones, use the command-line or GitHub Desktop software to intermittently "commit", or save new versions of, your code. And remember to push / sync / upload your work back up to your remote project repository on GitHub.com at least once before you're done.

If you need help breaking the problem up into more manageable pieces, consult the "Guided Checkpoints" and/or the "Guided Screencast" (links below).

Setup

Repo Setup

Use the GitHub.com online interface to create a new remote project repository called something like "groceries-exercise". When prompted by the GitHub.com online interface, let's get in the habit of adding a "README.md" file and a Python-flavored ".gitignore" file (and also optionally a "LICENSE") during the repo creation process. After this process is complete, you should be able to view the repo on GitHub.com at an address like https://github.com/YOUR_USERNAME/groceries-exercise.

After creating the remote repo, use GitHub Desktop software or the command-line to download or "clone" it onto your computer. Choose a familiar download location like the Desktop.

After cloning the repo, navigate there from the command-line:

Use your text editor or the command-line to create a file in that repo called "groceries.py", and then place the following contents inside:

Make sure to save Python files like this whenever you're done editing them. After setting up a virtual environment, we will be ready to run this file.

Environment Setup

Create and activate a new Anaconda virtual environment:

From within the virtual environment, demonstrate your ability to run the Python script from the command-line:

If you see the provided "products" data structure, you're ready to move on to project development. This would be a great time to make any desired modifications to your project's "README.md" file (like adding instructions for how to setup and run the app like you've just done), and then make your first commit, with a message like "Setup the repo".

Basic Requirements

Printing Products

Inside the "groceries.py" script, write Python code which will transform the provided data structure into the following output:

HINT: for formatting numbers as currency, feel free to use the to_usd function provided here.

Printing Departments

Prerequisites: Lists and the "List Comprehensions" Exercise

In addition to displaying the products in a human-friendly format, also display the departments in a human-friendly format. For each department, include the number of products associated with that department.

Example desired output:

HINT: use the filter() function or the filtering capabilities of a list comprehension to lookup all products associated with any given department, then use the len() function to count them

Feel free but not obligated to follow these guided "checkpoints", which provide one example strategy for breaking-up the requirements into smaller, more manageable pieces.

For a more in-depth guided exercise walkthrough, follow the screencast, but keep in mind a few caveats:

  1. The screencast will instruct you to use the GitHub.com online interface to "commit", but you should be using GitHub Desktop instead.

  2. Some of the links reference a previous course repository, but you should be able to find related documents in this course repository as well.

  3. During the virtual environment creation step, make sure to specify the python version as 3.7 (was omitted in the video).

  4. For simplicity, when sorting with the sorted() function, it is reasonable to prefer the operator.itemgetter() approach over the custom function approach.

Last updated

Was this helpful?