Python Syntax for Bioinformaticians/ First Python lesson for biologists

As we mentioned before, I am going to use python 2.X instead of version 3.X. Even if you have installed Python, you won’t need it for now. First, let’s learn the rules/syntax using Codeacademy’s python 2. You can the app on your phone or the computer.

I might give commands to the computer using python but how to get to hear from the computer? The print command. In front of the print, you can add what you want the computer to return/output. For E.g. writing the code below will return only the words inside the quote marks after running the code.

Typed on the editor: Input NB: The function/argument print starts with small letters.

print "Learning Bioinformatics Algorithms"

Seen in the console after running: Output

Learning Bioinformatics Algorithms

Anything denoted using quote marks is a string – either ” or “”. This can even be added like below.

print "Hello " + "Sarah"
 Hello Sarah

Did you notice the space after hello? If it wasn’t there, the result would have been

If one quote ‘ is used together with two quotes ” an error message will result. Instead of panicking, one can simply read the message and debug the problem i.e. match quotes.
Apart from the String (Str) data type, there are integers (int) for whole numbers and float for numbers with decimal points. Boolean data types return either True or False.
The datatypes can be exchanged too from let’s say str by add int(value to be exchanged) to it.
Just like a calculator, python can also perform arithmetic operations like +, -, *, / (quotient), and % (modulo)-This returns the remainder of an operation.
The result can be stored in a Variable which as the name suggests, its value can be changed.
january_to_june_rainfall = 1.93 + 0.71 + 3.53 + 3.41 + 3.69 + 4.50
annual_rainfall = january_to_june_rainfall
This Variable can be updated by adding or subtracting the previous variable to get a new variable stored like so…

september_rainfall = 5.16
october_rainfall = 7.20
november_rainfall = 5.06
december_rainfall = 4.06

sep_dec_rainfall = 5.16 +7.20 +5.06 +4.06

Printing the final annual-rainfall will be a different value from the first.
If one wants the value of the variable to span many lines, triple quotes can be used. This can be used to make comments to a piece of code that isn’t run by python where the Variable isn’t added before it. This commenting can also be done by starting with #. After the comment, the piece of code is written.

Note Normally, when integers are divided, they will return an integer result even when the result has a decimal point by rounding it. If you want to keep the decimal, you can do so by adding float in the operation or putting a decimal point in one of the numbers.

whole_cucumbers_per_person= 100/6
print whole_cucumbers_per_person
float_cucumbers_per_person =100./6
print float_cucumbers_per_person
quotient1 = float(7)/2
# the value of quotient1 is 3.5

Did you notice the symbol used to assign values to variables? =

That’s about it. However, the best thing to do is to go through everything by yourself. However, if you know the things below, you can good to proceed to the next blog.

  • Print statements
  • How to create, modify, and use variables
  • Arithmetic operations like addition, subtraction, division, and multiplication
  • How to use comments to make your code easy to understand
  • Different data types, including strings, ints, floats, and booleans
    Converting between data types