Home Musical Periodic Table Contact

Python for Chemists

The information presented throughout this page and subsequent pages loosely follows "Python for Chemists" by Christian Hall

Table of contents

  1. A Brief Introduction to Python
    1. "Hello World"
    2. Using Python for Arithmetic
    3. Using Python for Algebra
    4. Installing Python Packages & Miniconda
    5. A Brief Overview of Python Packages (Math, Numpy, Scipy)

1. A Brief Introduction to Python

Back to Table of Contents

Python is an open-source programming language, with multiple ways to use it. Python is an autocompiler meaning all you need to do is run it, without any extra steps that other languages (such as C or C++) require. Any Python libraries (such as math, numpy, or scipy) are similarly free and open source, helping to make Python an excellent choice for a variety of fields and disciplines, including chemistry.

Python can be utilized in many ways to help any chemist, regardless of topic area. It can solve equations ranging from a theoretical yield calculation to the Schroedinger Equation. It can also be used for data analysis and plotting, and can even take data saved in a CSV file (such as an Excel spreadsheet or Google sheet) and manipulate it. Instead of remembering countless formulas, you can create functions in Python which will be able to compute anything from unit conversions to molecular dynamics methods.

Due to its open-source nature, one can run Python in a few ways. It is recommended if you are a beginner programmer to use Jupyter Notebook which is commonly installed through Anaconda. It is recommended that you download Anaconda and access Jupyter Notebook through there. For more advanced programmers, an integrated development environment, or IDE is recommended (VScode is a common choice).

1.1 "Hello World"

Back to Table of Contents

"Hello World" refers to the first step that any programmer takes on their journey. This is the foundation of anything going forward. "Hello World" will produce what every programmer wants: an output. Most of the time, you have to tell your code that you want it to show, or print an answer based on the code you've given it. A computer that knows what 1+1 equals is useless if it does not show the answer. Luckily, it is simple to tell Python you want to see the output. Python, just like any human language, has vocabulary and grammar to it. Just as one can learn to speak Spanish or German fluently, so can you learn to "speak" fluent Python. First, let's look at what the code looks like if we want to have Python print "Hello World":

print("Hello World")

Now that we see what the code looks like, let's break down each part. First, we have print. This is a function. Any function looks of the form [name](parameter), where in this case the name of the function is print and its parameter (this can also be thought of as a variable, which will become more obvious later on) is "Hello World" . For this function, we are telling Python to print (again, this is the function) the parameter ("Hello World"). And, just as in any writing, quotes indicate what we want to say (in this case, print).

Let's look at what this code snippet looks like in a Jupyter Notebook:

Jupyter Notebook 'Hello World'

Congratulations! You have written your first successful Python program.

1.2 Using Python for Arithmetic

Back to Table of Contents

Python, especially in scientific fields, is commonly used for calculations. To form a strong foundation of scientific analysis using Python, it is best to start with "grade school" examples and slowly increase our knowledge to more complex topic areas. For example, say you want to compute the addition of two numbers. Python handles this in a way that will feel natural to the users. From something simple like 1+1 to exponentially larger numbers such as 1589978603+3749562, Python can handle it. There are a few operators that Python recognizes:

+ Addition

- Subtraction

* Multiplication

/ Division

** Exponents

Python is also capable of handling both integers (whole numbers) and floats (numbers with decimal values). Python can also interpret complex numbers (usually denoted as "i" or j in Python). Complex numbers will become a larger focus for various topics in quantum mechanics and will be addressed later.

Python also has some more "advanced" operations:

// Integer division

% Remainder

Here, integer division refers to a whole number division, meaning the answer will be rounded down to a whole number. The remainder will provide the remainder of a division operation. Python also follows standard order of operations rules, including exponents and parentheses. It is a good habit to include parentheses when there is more than one operation for clarity purposes. This usually helps to ensure you and your code have a clear understanding of what is occurring. Oftentimes, you may get an error due to a forgotten parenthesis (such as if you tried to run print("Hello World" or 2 * (1 + 3). Your error may look like this:

Closing parenthesis error for 2 * (1 + 3

Here, EOF stands for end of file, meaning the code finished reading, but was missing a closing parenthesis. If you are using Jupyter Notebook or an IDE, the right-side parenthesis or other closing syntax is applied automatically. Both Jupyter Notebook and most IDEs have syntax highlighting as well, meaning if you put your cursor on the right-hand side of a character such as a parenthesis, its pair will highlight automatically. If no pair exists, the character will usually be highlighted in red:

Open parenthesis highlighted in red

To fix this error, all you have to do is add an extra parenthesis. In the example above, you see that we are not using the `print` function like we discussed earlier. In Jupyter Notebook, you do not need to call the print function if and only if you type just the expression of interest in a cell. For example, consider the following two examples:

2*(1+3)`
x=2*(1+3)

Here, you can see that the first example is our expression of interest, while the second example is our expression of interest assigned to a variable. If we had just the first example in a cell and ran it in Jupyter Notebook, we would get an output printed. However, if we run just the second example in a cell, nothing will print. This is because we must call the new variable representing the expression. So, to get it to print, we can do it one of two ways:

x=2*(1+3)
print(x)
or
print(2*(1+3))

Using either method, the result will be printed in the output.

Now that we have the basics of arithmetic in Python under our belts, let's try out some examples.

  1. 2*32
  2. 47/2.56
  3. Student A has twelve homework problems. The student completed eight problems. How many problems does the student have left?
  4. 13% of students in a class are passing. The total number of students in the class is 58. How many students are passing? How many students are failing?
  5. There are three pizzas. Each pizza has 8 slices. How many total slices of pizza are there? There are ten students splitting the pizza evenly among the group. How many whole slices can each student have?

1.3 Using Python for Algebra

Back to Table of Contents

Using Python for algebra is quite similar to its use with arithmetic. The main difference is we will now assign variables to our numbers. You have many options for variables, including singular letters which are commonly used in mathematics courses (x, y, or z for example) or words or phrases. Variables are generally structured as follows, and are case sensitive (i.e. VARIABLE and variable are two different variables in Python):


1.4 Installing Python Packages & Miniconda

Back to Table of Contents

Python is a powerful open-source tool, meaning there are endless packages created and developed by its users for a variety of tasks. Installing said packages can be done in a few ways, and there are some packages native to Python itself (The math package, for example, is built into Python and does not require installment). A few different terms are used virtually interchangeably: packages, libraries, and modules all correspond to loadable content in Python.


There are a few ways to install packages in the command line:


Using miniconda and conda allows for the creation of virtual environments. Each environment is able to contain specific packages, making miniconda/conda an ideal choice for project-specific codes. For example, if one project requires an older version of python (2.7 for example) but a different project requires a newer version of python (3.9 for example), two different environments with two different python (and corresponding packages) versions can be created. This helps to mitigate any bugs related to incompatible software versions.


Note: This is a more advanced method. For the purposes of Python for Chemists, virtual environments are not needed. To learn how to implement virtual environments, please look here. This link also has great information on how to install anaconda (miniconda/conda).

Terms to Know

functions. [name](parameter)

note: Press shift+enter on your keyboard to run a cell in Jupyter Notebook.

note: Python uses the ** syntax for exponents. If the ^ symbol is used, you will get an error!

note: Python will automatically convert fractions to decimals. If you have the fraction 2/3 this value will be converted to 0.66666...7

note: The current cell you are in in Jupyter Notebook has a green square around it.