Density computation activity


In this activity you will sample Python programming to calculate density and match the results to a dataset to identify materials.

Basic math


Math in Python uses few operators: +, -, /, *, and **.

The first few are self explanatory, but the last (**) is an exponent.

x**2 is equivalent to @@0@@

Try out some math in the cell below, Shift + Enter to run a selected cell. Test out order of operations, do all of the operations work as expected?



When programming, we use functions to modularize code so that making changes is easy and we don't have to repeat ourselves. In Python, a function takes the form of:

This is the equivalent of @@0@@ in mathematical terms. The def keyword tells Python to make a function called f that takes one argument/parameter and output x + 5. f(5) should return 10, does it?

What happens if you give f a mathematical expression as an argument/parameter? Can you apply f to itself?

Functions can take multiple parameters, just like they can in math. In programming, it's good to pick function names that describe what they actually do, single letter names are considered bad practice. This goes for parameters as well, they can and should have longer, more descriptive names.

Does average(5, 3) return what you think it should? What happens if you give average() the function f() as a parameter?



You can store data in variables. z = 7 stores the value of 7 in x. Notice how f

Density function


Using the information above, make a function called density that takes a mass argument and a volume argument.

Python modules and Dictionaries


A Python module is a way to use functions and data from outside of your program. Long story short, you need to use import densitytable to load data from This file contains three data sets in the format of a "dictionary": solids, liquids, and gasses. Because the data we want is in another file, you would access the table of solid material densities with densitytable.solids. Try accessing the three tables as previously described and see what happens.

Loading output library...

This data structure is called a dictionary because it has a "key" and a "value" for each entry. This dictionary tells you that silver has a density of 10.5 g/cm^3 in the same way that a physical dictionary might tell you that "abecedarian" means "of or relating to the alphabet". In Python:

We can see all the contents of the dictionary, but what if we want to look something up, knowing only the word and not its meaning? We'd have to do something like this:

Try looking up the density of some of the materials in the densitytable.solids, densitytable.liquids, and densitytable.gasses dictionaries.

Now, what if we want find the material knowing the density? densitytable.solids.values() will output the values, the word definitions in the dictionary analogy.

Search function


Using all of this knowledge we can create a function to identify materials based on a density. The code comments below explain the details of how this works.