An introduction to solving biological problems with Python

Day 1 - Session 1: Variables

Printing values

The first bit of python syntax we're going to learn is the print statement. This command lets us print messages to the user, and also to see what Python thinks is the value of some expression (very useful when debugging your programs).

We will go into details later on, but for now just note that to print some text you have to enclose it in "quotation marks".

We will go into detail on the arithmetic operations supported in python shortly, but you can try exploring python's calculating abilities.


In [ ]:
print("Hello from python!")

In [ ]:
print(34)

In [ ]:
print(2 + 3)

You can print multiple expressions you need to seperate them with commas. Python will insert a space between each element, and a newline at the end of the message (though you can suppress this behaviour by leaving a trailing comma at the end of the command).


In [ ]:
print("The answer:", 42)

To start the Python interpreter, open a terminal window, type the command python3, then enter Python commands after the prompt >>> and press Enter when you're done.

Python will run the code you typed, and might display some output on the line below, before leaving you with another prompt which looks like >>>.

If you want to exit the interactive interpreter you can type the command quit() or type Ctrl-D.

Exercises 1.1.1

  1. In Jupyter, insert a new cell below this one to print your name. Execute the code by pressing run cell from the menu bar or use your keyboard Ctrl-Enter.
  2. Do now the same using the interpreter

Using variables

In the print commands above we have directly operated on values such as text strings and numbers. When programming we will typically want to deal with rather more complex expressions where it is useful to be able to assign a name to an expression, especially if we are trying to deal with multiple values at the same time.

We can give a name to a value using variables, the name is apt because the values stored in a variable can vary. Unlike some other languages, the type of value assigned to a variable can also change (this is one of the reasons why python is known as a dynamic language).

A variable can be assigned to a simple value...


In [ ]:
x = 3
print(x)

... or the outcome of a more complex expression.


In [ ]:
x = 2 + 2
print(x)

A variable can be called whatever you like (as long as it starts with a character, it does not contain space and is meaningful) and you assign a value to a variable with the = operator. Note that this is different to mathematical equality (which we will come to later...)

You can print a variable to see what python thinks its current value is.


In [ ]:
serine = "TCA"
print(serine, "codes for serine")
serine = "TCG"
print("as does", serine)

In the interactive interpreter you don't have to print everything, if you type a variable name (or just a value), the interpreter will automatically print out what python thinks the value is. Note though that this is not the case if your code is in a file.


In [ ]:
3 + 4

In [ ]:
x = 5
3 * x

Variables can be used on the right hand side of an assignment as well, in which case they will be evaluated before the value is assigned to the variable on the left hand side.


In [ ]:
x = 5
y = x * 3
print(y)

or just y in the interpreter and in Jupyter notebook


In [ ]:
y

You can use the current value of a variable itself in an assignment


In [ ]:
y = y + 1
y

In fact this is such a common idiom that there are special operators that will do this implicitly (more on these later)


In [ ]:
y += 1
y

Exercises 1.1.2

In the interpreter:

  1. Create a variable and assign it the string value of your first name, assign your age to another variable (you are free to lie!), print out a message saying how old you are
  2. Use the addition operator to add 10 to your age and print out a message saying how old you will be in 10 years time

Next session

Go to our next notebook: python_basic_1_2