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

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

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

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 densitytable.py. 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.

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.