Learning to code is much more fun – and useful – if you practice with things that are practical for you (not just petal lengths and titanic passengers). These are just a few suggestions to get you started. Try writing functions to….

Given a mass, calculate the number of moles (or nanomoles, etc.)

Given a mass and volume of a solution, calculate the molarity 

Given the molarity of a solution, calculate the concentration in terms of mass/volume 

Given a mass, calculate the volume to dilute it in to get to a desired concentration 

Do C1V1 = C2V2 dilution calculations 

Take a numeric value and determine what’s the best metric prefix based on size (choose prefix that avoids decimals)

  • Challenge: incorporate a “best” option into the above functions (do the calculation & return the answer with the best prefix)

Convert between metric units

  • Challenge: default to base units if desired unit not provided
  • Challenge: give option of “best”, which will choose the best prefix

Take a list of values and determine whether you need to convert any units

Challenge: Convert if needed

Level up: incorporate a unit check & conversion into your calculation functions (take a list with values having any units, convert if needed, do the calculations, and return the answer with the best prefix)

Big challenge: Write a buffer brewer function to calculate volumes of stock solutions needed for desired final solution. 

Bigger challenge: return a dataframe with initial concentrations, final concentrations, & needed input volumes. Also print out a formatted list of what to prepare. 

Bigger challenge: allow input to be given as a dictionary, dataframe, or csv file

I recommend figuring out the syntax outside of a function so you can troubleshoot more easily. Then incorporate what you came up with into a function. Collect your functions into a python script and then you can import that as a package into your other scripts and use those functions over and over anywhere!

  • You can import a module from a .py file containing functions you want to use (e.g. lab_calc_functions.py). 
  • If a module is not in your current directory, make sure Python knows where to look for it.
  • basic import (import module_name)
    • if you just use import “module_name” you will have access to the functions in the module – but each time you want to use one, you will need to direct Python to the module (using dot notation) by its full name 
      • i.e. lab_calc_functions()
  • import as (import module_name as nickname)
    • Module names can be long and you probably don’t want to have to type out long names each time you want to use a function, so you can use “import” module “as” nickname to simplify the typing.
      • i.e. import lab_calc_functions.py as lc
  • importing individual functions (from module_name import cool_function)
    • if there are only a couple of functions you need, or if there are some you want to use a lot, you can import the function directly and then you don’t need to specify the module
  • importing everything (from module_name import *)
    • Using the asterisk imports everything from a module with its names so you never have to specify the module name for them
    • This is typically not recommended because
      • Different modules may use the same names for different functions & then Python wouldn’t know which you wanted
      • It can make it harder to de-bug things if there’s a problem with a module (you can’t easily see where it comes from)
  • autoreload
    • Different modules may use the same names for different functions & then Python wouldn’t know which you wanted

Try yourself and if you get stuck, Google. But if you want to see how I did it (which I know is not the best!) I uploaded my Python function file here: https://drive.google.com/file/d/1-U4Y9_tge6Y7XKo27lncOwKywysjuxfc/view?usp=sharing 

And for more Python tips & resources, see: https://bit.ly/bb_python_tips 

more videos on coding stuff: https://youtube.com/playlist?list=PLUWsCDtjESrHbBVTt6wqWBGGbd0Q9pbRA 

YouTube: https://youtu.be/PNaUfFWl1Gg

Leave a Reply

Your email address will not be published. Required fields are marked *