Lesson 1: Welcome, .py files, and IPython

(c) 2016 Justin Bois. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.

This tutorial was generated from a Jupyter notebook. You can download the notebook here.

Greetings!

Welcome to the Introduction to Programming for the Biological Sciences Bootcamp! We will be using Python as the programming language, and we will use this first lesson to make sure your Python distribution is working properly. We will also make sure you have a way to use the command line on your machine.

Prior to starting the bootcamp, you should have completed Lesson 0: Configuring your computer.

The Python interpreter

Before diving into the Python interpreter, I pause here to remind you that this programming bootcamp is not meant to just teach Python syntax. The things you learn here are meant to help you understand, and ultimately do, computer programming more generally. Think of it this way: this bootcamp is meant to help you unleash the power of your computer on your biological problems. Python is just the language of instruction. That said, let's start talking about how Python works and how it related to IPython, which we will use extensively in the course.

Python is an interpreted language, which means that each line of code you write is translated, or interpreted, into a set of instructions that your machine can understand by the Python interpreter. This stands in contrast to compiled languages. For these languages (the dominant ones being Fortran, C, and C++), your entire code is translated into machine language before you ever run it. When you execute your program, it is already in machine language.

So, whenever you want your Python code to run, you give it to the Python interpreter. To launch the Python interpreter, just type

python

on the command line, and you will get a prompt. You then have to enter your code in line-by-line at the prompt. Let's try it with a simple code.

Hello, world. and the print() function

Traditionally, the first program anyone writes when learning a new language is called "Hello, world." In this program, the words "Hello, world." are printed on the screen. The original Hello, world. was likely written by Brian Kernighan, one of the inventors of Unix, and the author of the classic and authoritative book on the C programming language. In his original, the printed text was "hello, world" (no period or capital H), but people use lots of variants.

So, now at a Python prompt on your terminal, let's give the interpreter a simple code to execute.

In [4]:
print('Hello, world.')
Hello, world.

Hooray! We just printed Hello, world. to the screen. To do this, we used Python's built-in print() function. The print() function takes as an argument a string. It then prints that string to the screen. We will learn more about function syntax later, but we can already see the rough syntax with the print() function.

Now, let's exit this instance of the Python interpreter. You can do this either by pressing ctrl-d or by typing exit().

Jupyter notebooks for bootcamp lessons

To help you understand the structure of the document you are reading right now, I pause for a moment to describe how it was generated and how to read it. This HTML document was generated using a Jupyter notebook. In these notebooks, code is shown and executed in special cells, like the one below.

In [3]:
# This is a code cell.

# It does not contain any executable code, just comments.

In looking at this code cell, we have learned a bit of Python syntax. Lines beginning with the pound sign (#) are comments and are ignored.

Ok, now the code cell with Hello, world. again.

In [7]:
print('Hello, world.')
Hello, world.

So, as you are working through the bootcamp, entries in code cells are meant to be executed by the Python interpreter.

At the very end of the bootcamp, we will discuss Jupyter notebooks and how to use them. But now, we will talk about other ways to enter and execute Python code.

.py files

Now let's use our new knowledge of the print() function to have our computer say a bit more than just Hello, world.

In [9]:
# The first few lines from The Zen of Python by Tim Peters
print('Beautiful is better than ugly.')
print('Explicit is better than implicit.')
print('Simple is better than complex.')
print('Complex is better than complicated.')
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

As you typed these lines into the interpreter, it executed a line every time you hit enter. This can get very tiresome and tedious. A better option is to store them in a file, and then have the Python interpreter run the lines in the file. This is how you typically store Python code, and the suffix of such files is .py.

So, let's create a .py file. To do this, use Atom to make a new file, enter in the lines of code you want executed (in our case, the four print statements above), and save the file as zen.py. Be sure to save it in the directory where you are working in your terminal.

To run the code in this file, you invoke the Python interpreter at the command line, followed by the file name. I.e., enter

python zen.py

at the command line. Note that when you run code this way, the interpreter exits after completion of running the code, and you do not get a prompt.

So, now you can have the Python interpreter run many lines of code without waiting for you to press enter over and over again!

IPython

The Python prompt you were using before is useful for running commands, but it is limited in its functionality. To have a greatly enhanced Python experience, use IPython!

IPython is a feature-rich, enhanced interactive Python. To launch it, enter

ipython

on the command line. Once you launch it, try printing Hello, world. You will notice nice looking input and output that looks something like this:

Competing hypotheses

Notice the syntax highlighting. The function print() is in green, and the string 'Hello, world.' is in orange.

To get an idea of all of IPython's features, just enter a question mark (?) at a prompt, and you can read about many (but not all!) of its features. At this point in the bootcamp, you will not understand the significance of all of its features.

For now, I direct you to the first two sentences of the overview:

IPython offers a combination of convenient shell features, special commands and a history mechanism for both input (command history) and output (results caching, similar to Mathematica). It is intended to be a fully compatible replacement for the standard Python interpreter, while offering vastly improved functionality and flexibility. [emphasis mine]

Indeed, henceforth, we will pretty much abandon the standard Python interpreter in favor of IPython.

To run our zen.py file using IPython from the command line, we can use the same syntax as with the standard interpreter:

ipython zen.py

Conveniently, if we already have IPython fired up and running and we're at an IPython prompt, we can use the %run magic function.

In [10]:
%run zen.py
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

Ok! You are up and running with IPython, with Atom to make your .py files. You now have the power Python interpreter by your side, and we will venture into the next steps of your programming training.