Practical Python For Beginners - Course Guide
Practical Python For Beginners - Course Guide
Learning objectives
After completion of the course, the successful learner will be able to:
• Explain the rationale for scripting
• Install Anaconda and navigate Spyder
• Use the IPython command line as a calculator and to assign variables
• Use the basic data types and some simple functions
• Create lists and select elements from them
• Use For Loops to perform operations iteratively
• Explain what a library is, import a Python library and use functions it contains
• Read biochemical data from a file into a Python
• Understand computing concepts such as what is meant by the working directory, absolute
and relative paths and be able to apply these concepts to data import
• Analyse and visualise biochemical data using powerful Python packages such as NumPy,
Pandas, Sklearn and Matplotlib
• Run examples of more complex analyses in Jupyter notebooks
Definitions
Fill in the below table of definitions as you complete the course.
Command
Script
Comment
Syntax Highlighting
Operator
Operand
Assignment
Function
Integer
Float
String
Reproducibility
List
Indexing a list
Method
For loops
1
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
If
Else
Break
Continue
Elif
Library
Alias
Element-wise
NumPy
Array
Pandas
DataFrame
Working directory
Csv file
2
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Modules
Module 0 - Welcome to the course
The analysis and reporting phase should be entirely reproducible - given the data generated, we
should be able to reproduce every aspect of its processing, analysis and reporting. Scripting, in which
each step is explicitly articulated, is the best way to achieve this.
The more reproducible your analysis, the faster you, and others, will be able to apply similar
analyses in the future and the more transparent and open your work; this leads to better science.
Notes:
3
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
1.2 - Anaconda
Q. What is Anaconda?
Download and install Anaconda following the instructions on the course site. Start Anaconda
navigator as you normally start a programme i.e. the Start menu (Windows) or Dock (Mac).
1.3 - Spyder
The bottom right pane is the Interactive Python (IPython) console. This is where commands are
executed.
• In [#]: is the prompt, a command is typed after this.
• Pressing enter will send a command and return the result on a line starting: Out [#]
• For the rest of this course, we won't show In [#] or Out [#].
1.7 – Commenting
It is good practice to comment your code. You can write as much information in comments as you
like by adding a hash, #, to the start of the line.
4
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Example:
Notes:
5
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Example:
In [ ]: 4 + 3
Out[ ]: 7
Here the ‘+’ is known as an operator and both ‘4’ and ‘3’ are known as operands.
2.4 – Assignment
The = is the assignment operator. To assign the numbers 4 and 3 to variables we can use:
num1 = 4
num2 = 3
A function has a name followed by brackets. Inside the brackets go arguments which tell the
function what to act on and often how to act on it.
For example the function type() returns the datatype of an object:
type(num1)
Out[ ]: int
num3 = 4.0
type(num3)
Out[ ]: float
Integers and floats are two different kinds of numerical data. Words are of the data type ‘string’.
6
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
word1 = 'hello'
word2 = 'Emma'
word1 + word2
Out[ ]: 'helloEmma'
In this case, writing word1 will print out the word but in other scenarios we must tell python to
print using the print() function:
print(word1)
Out[ ]: hello
Example:
pi = 3.14
radius = 5
2 * pi * radius
pi * radius**2
Notes:
7
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
A list is denoted by using square brackets with a comma between each element.
To make a list called values:
values = [2, 5, 3, 7]
We can use the function type() on a list to reveal its data structure:
type(values)
Out[ ]: list
Some other useful functions for finding the number of elements of a list and the biggest element:
len(values)
Out[ ]: 4
max(values)
Out[ ]: 7
index 0 1 2 3
values[ 2, 5, 3, 7 ]
We denote the index of an element with square brackets. Thus, the first element of values is
extracted with:
values[0]
Out[ ]: 2
values[2]
Out[ ]: 3
8
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
type(names[0])
Out[ ]: str
tells us what type of variable is in the first element of the names list.
3.9 - Methods
names.count('maria')
Out[ ]: 1
names.index('maria')
Out[ ]: 0
An example of a method that doesn’t require an argument and that changes a list object as well as
returning a value is the pop method.
names.pop()
Out[ ]: 'jamie'
Pop returns the last element of names but it also changes names!
print(names)
Out[ ]: ['maria', 'isaac', 'sam']
Notes:
9
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Q. What is a For loop? And what data structures do they work with?
Examples:
for i in 'Spam':
print(i)
Out[ ]: S
Out[ ]: p
Out[ ]: a
Out[ ]: m
With For Loops you can repeat a function a specified number of times.
for i in range(3):
print('Hello')
Out[ ]: Hello
Out[ ]: Hello
Out[ ]: Hello
For Loops are also powerful in their ability to access data from inside data structures in an iterative
fashion, which you then could manipulate, transform, store, or do anything else you can think of.
10
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
past = []
for verb in present:
past.append(verb + 'ed')
print(past)
Out[ ]: ['kicked', 'licked', 'chucked']
4.8 - If Statements
If statements specify that the code should only be run if a condition is satisfied. They take the
general form:
if condition
code
Example:
Break will end a loop entirely, whereas continue will end the current iteration of the loop and move
on to the next.
Examples:
11
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
continue
print(number)
Out[ ]: 0
Out[ ]: 2
Out[ ]: 3
Elif (else if) statements can be stacked multiple times, but they need to come after an if statement.
Notes:
12
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Module 5 – NumPy
NumPy
It is the fundamental package for scientific computing in Python. It enables the use and manipulation
of data arrays (really important for data analysis), basic algebra and statistics.
Pandas
Pandas is a specialist data analysis library which enables you to import data from .csv files easily and
put them into DataFrame.
Seaborn
This is a data visualisation library which enables you to draw some spectacularly pretty graphs within
Python.
In order to use an object defined within the NumPy library, we will have to import it. We can do this
by typing import <name of library>.
Example:
import numpy
a = numpy.array([1,2,3])
Example:
import numpy as np
b = np.array([1,2,3])
13
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
a = np.array([(1,2,3), (4,5,6)])
print(a)
c = a + b
Other operations:
cc = c * 10
np.log(c)
Example:
z = np.array([(1,2,3), (4,5,6), (7,8,9), (10,11,12)])
x = np.arange(0,8,1)
Example:
x = np.zeros((3,3))
Notes:
14
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
A Pandas DataFrame object can be made using python code, or by importing data from txt, csv and
excel files. Pandas can also create objects called Series.
Example:
import pandas as pd
List1 = [('Maria', 98, 70, 11),('Isaac', 20, 87,
34),('Sam', 93, 60, 100),('Jamie', 100, 68, 0)]
df = pd.DataFrame(data = List1)
print(df)
df is a common name to assign a DataFrame to. The extra column on the left is the index. This is how
to access data from the structure at a particular entry, since each row will have a unique number.
You can also set columns as the indices.
Example:
Before importing any files, use the os library to find our working directory.
Making a folder/directory:
os.mkdir('python_work')
15
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Change directory:
os.chdir('python_work')
df = pd.read_csv('filename.csv')
df = pd.read_csv('filename.txt')
df = pd.read_excel('filename.xlsx', sheet_name='Sheet1')
For bigger data frames you will need to explicitly tell Python to print all of the data.
pd.set_option('display.max_rows', df2.shape[0])
pd.set_option('display.max_columns', df2.shape[1])
print(df2)
You can print specific rows and/or columns of the DataFrame using loc and iloc (location and
integer-location).
Example:
print(df.loc['Maria':'Isaac','maths'])
print(df.iloc[0:2,0])
Notes:
16
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
• A notebook has one code ‘cell’ at first. You can type Python code and comments into it.
• Run the code by pressing the Run button which is on the tool bar at the top.
• The output will appear underneath along with an additional code cell.
• A notebook can have as many cells as you like to lay out your work. Cells can be code or text.
You can use a text cell to write about your work.
• To turn a cell from code to text (or vice versa), we use the dropdown menu on the right of
the toolbar and choose the 'markdown' option.
• If you want to alter one of the cells, click on it, edit as you require and run it again.
Notes:
17
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Notes:
18
Biochemical Society Online Training Course
Practical Python for beginners: a Biochemist's guide
Notes:
19