logo

CH40208: Topics in Computational Chemistry

  • Welcome to CH40208

Jupyter Notebooks

  • What is a Jupyter Notebook?
  • How to access the JupyterHub server?
  • Getting started with Jupyter Notebooks
  • The Jupyter Notebook interface
  • Exercises

Python

  • Python first, chemistry later
  • Why we encourage typing out the code examples
  • Using Python as a calculator
  • Getting started with strings
  • Print statements
  • Types
  • Variables
  • Arithmetic
  • Lists
  • Tuples
  • Dictionaries
  • Loops
  • Functions
  • Logical Operations and Flow Control

Good Practice

  • Writing readable code
  • Raising errors
  • Testing
  • Modularisation
  • Markdown

Working with Data

  • NumPy
  • Plotting
  • Investigation of the ideal gas law
  • File I/O
  • Model fitting
  • Exercise: Monte Carlo

Computational Chemistry Methods

  • Potential Energy Surfaces
  • Geometry Optimisation
  • Function minimisation
    • Gradient descent
    • Newton’s method
  • Minimization Libraries
  • Working with vectors and matrices
    • Vectors
    • Working with vectors in Python
    • Matrices
    • Working with matrices in Python
    • Eigenvalues and eigenvectors
    • Principal rotation axes and principal moments of inertia
Powered by Jupyter Book
Contents
  • Displaying data
  • A brief intro to functions
  • Slightly more complex print statements

Print statements¶

Displaying data¶

Until now, we have been using the built-in functionality of the Jupyter Notebook to display data on the screen.

Code blocks are not limited to single lines of code—this would make working in Jupyter Notebooks extremely tedious when writing more complicated programs.

But what happens if we run a cell that contains more than one statement?

Only the output from the last statement is displayed as the cell output. The first two statements have been executed when we run the cell, but no output is displayed.

The default setting for Jupyter Notebooks is to only display the output from the final statement in any code cell. While it is possible to change this setting so that all the output is displayed, the usual way to control what is displayed is to use the print function.

A brief intro to functions¶

A function is a piece of reusable code that takes zero or more inputs, performs a specific computation, and returns an output.

To use a function (known as calling the function) we type the function name, followed by a pair of brackets. Anything inside the brackets specifies the inputs to the function. These inputs are called arguments, and are said to be passed to the function:

print("Hello World!")

Here, we are calling the print function with one argument; the string "Hello World!.

If we run this in a code cell, the string is printed:

Returning to our previous example, the following set of print statements displays all three strings:

You might have spotted that these examples using print do not give an Out [ ]: output under the code cell.

Remember that a function is a piece of code that returns an output. So what is the output returned by print? 🤔

Somewhat confusingly, print returns None. None is a null object that represents no data.

Remember that when you run a code cell, whatever data is produced by the final statement in that cell is displayed as the cell output after Out [ ]:.

But calling print returns None. The examples above produce no data, and so there is nothing to display as the cell output.

If zero arguments are passed to an function, the function is still called by writing the function name followed by a pair of brackets. e.g.

Calling print with no arguments prints a blank line (and returns None, so no cell output is displayed).

Writing a function name without the following brackets is still valid Python. But instead of calling the function, and performing whatever computation you expected, you are simply referring to the data that represents that function. e.g.

Now the cell output shows a string representation of the data produced by the final statement; in this case this is the print function itself.

Slightly more complex print statements¶

The print function can also be passed more than one argument. print will try to convert each argument to a string and then concatenate these into a single output, e.g.

previous

Getting started with strings

next

Types

By Andrew R. McCluskey & Benjamin J. Morgan
© Copyright 2021.