from IPython.core.display import HTML
css_file = '../styles.css'
HTML(open(css_file, "r").read())
Python is a modern computing language that is designed to be simple and easy to use. It is excellent for writing functional code quickly, and supports modules (libraries) that make achieving complicated tasks relatively simple. It was created in the early 90's by the Benevolent Dictator For Life (BDFL), Guido van Rossum.
You need to be aware that there are two versions of the Python language around at the moment. Python 2 was first released in 2000, and the latest version is 2.7. Python 3 is a slightly different language, and the one we will use for this course. It was released in 2008 and is rapidly becoming the standard version. Python 3 is not backwards compatible with Python 2.
These points mean that Python can be installed by anyone, anywhere. The simple syntax means Python is easy to learn. Most importantly however, the third party modules that exist for Python allow us to do very complex tasks with very little code. Let's look at an example - we'll use a third party module to quickly analyse survival statistics from the Titanic:
# This is a comment line
# The next lines import external modules so we can use them
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')
# open titanic survival data
titanic = sns.load_dataset("titanic")
# plot it
ax= plt.figure(figsize=(10,6))
sns.barplot(x="sex", y="survived", hue="class", data=titanic);
In 2015, Python is the most popular programming language amongst employers. It's used in the tech industry, financial services, web development and, of course, in science.
Take a look at the recent jobs listings for Python developers. In fact, the question should be, why not learn python?
Python is what is known as a dynamically typed language. That means that if I want to store a value in a variable I just type
x = 3
What you are doing here is storing a value in computer memory. In this case we are storing a value (3) in a box in memory, which is labelled "x". Python hides a lot of complicated stuff from us here. Python figures out for me that x
is a variable that should store an integer (3). If I try to add two variables together, Python will try and guess what I want to do, and do something sensible. So
x = 3
y = 4
print (x+y)
7
works exactly as we expect. Many computer languages are not like this! For example, let's look at the same code in C
int x, y;
int total;
x = 3;
y = 4;
total = 3+4;
std::cout << total << std::endl;
First of all, you can see that we have to tell C what type of object goes into each variable. You can see from these examples just how clean and easy to understand Python's syntax is in comparison to C. But wasn't I supposed to be explaining why not to use Python?
The point is that Python is slow. To calculate x+y
above, the computer needs to convert the code you write into a series of instructions for the computer. Because C knows it is adding two integers, it can just run the instructions to add two integers. When running Python, the computer doesn't know in advance the type of the things we are asking it to add. So the computer has to execute the following instructions:
This extra overhead makes Python slow to run compared to languages like C and Fortran. However, the speed of writing the Python often makes up for this, and when it does not, there are workarounds. The most common is to use Python as glue to stick together bits of C code. In fact, many of the libraries you can use in Python actually run C behind the scenes to speed things up.
This leads us to an important realisation about the best way to use Python. Many people have written efficient third-party libraries, many of which run C behind the scenes. The best Python code uses snippets of Python to glue these libraries together. We'll see this approach a lot in this bootcamp.
Now we have (hopefully) motivated you to learn Python - let's look at how to install it on your own laptop.