Skip to main content
Ctrl+K
CH40208: Topics in Computational Chemistry - Home CH40208: Topics in Computational Chemistry - Home
  • About this Book

Introduction

  • CH40208 course contents
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5
    • Week 6
    • Week 7
    • Week 8
    • Week 9

Jupyter Notebooks

  • What is a Jupyter Notebook?
  • Getting started with Noteable
  • The Jupyter Notebook interface

Python

  • Python First, Computational Chemistry Later
  • Why We Encourage Typing Out the Code Examples
  • Using Python as a calculator
  • Getting started with strings
  • Print statements
  • Types
  • Variables
    • Naming Conventions for Variables
  • Arithmetic
  • Lists
  • Loops
  • Tuples
  • Dictionaries
    • Iterating over Dictionaries
  • Functions
  • Comparisons and Flow Control

Working with Data

  • NumPy
    • More NumPy functions
  • Matplotlib
  • File input and output
  • Synoptic exercises
    • Modelling the Hydrogen Emission Spectrum
  • Model Fitting
    • Statistical Models: Turning Data into Understanding
    • Empirical and Theoretical Models
    • Model Complexity and the Goal of Fitting
    • Finding the Best Fit
    • Exercises

Good Practice

  • Writing readable code
  • Modularisation
  • Markdown

Computational Chemistry Methods

  • Geometry Optimisation
    • Analytical Solution for a Harmonic Potential
    • Grid Search Method
    • Gradient Descent Method
    • The Newton-Raphson Method
    • Exercise: Geometry Optimisation of a Lennard-Jones Potential
    • Minimisation with scipy.optimize.minimize
  • Molecular Dynamics
    • Understanding Molecular Dynamics
    • The Mechanics of Molecular Dynamics
    • Numerical Integration Methods
    • Choosing the Right Timestep
    • Moving to Real Systems
    • Exercises
  • Monte Carlo Simulation
    • Estimating \(\pi\)
    • Sampling Molecular Systems
    • The Metropolis Algorithm
    • Monte Carlo Simulation of the 1D Ising Model
  • 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

Example Coursework Notebooks

  • Determination of the HCl bond length by IR spectroscopy

Planning Your Code

  • Planning Your Code and Writing A Code Schematic
  • .md

Print statements

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

Contents
  • Displaying data
  • A brief intro to functions
  • Slightly more complex print statements

By Andrew R. McCluskey & Benjamin J. Morgan

© Copyright 2023.