Introduction to Python: Variables, Arithmetic and Lists

Python Terminology and Syntax

Variable-A variable is like a box storing a piece of data, giving a specific name to a value
A variable can be a string or a number
String-a string can contain letters, numbers and symbols and it MUST be within quotes (“a” or ‘a’)
A number can be an integer or a float
Integer-an integer is a number without a decimal point
Float-a float is a number with a decimal point
numbers  can be used in mathematical expressions and are not contained within quotes
To set a variable

String              >>> candy = “Twix”
Integer           >>>a=7
Float               >>>b=7.3

To change the value of a variable you can reassign it the same way you originally assigned it

one equals sign (=) assigns a value to a variable
two equals signs (==) denotes equality
an exclamation point followed by a an equals sign (!=) means not equal

Python Arithmetic
In python you can perform mathematical operations on floats and numbers

Addition (+)
Returns 4
Returns 5.8

Returns 3
Returns 1.1

Multiplication (*)
Returns 21
Returns 13.02

Division (/) is special,
An integer divided by an integer returns the quotient as an integer (without the decimals place) rounded down
Returns 2
Returns 3
A Float divided by a float returns the a float (with the decimals place)
Returns 1.9545454545454544
Returns 1.0
A float divided by an integer (or an integer divided by a float) returns a float (with the decimals place)
Returns 1.65
Returns 0.6060606060606061
To perform division with a float and return a whole number (still a float-ending in .0, also rounded down) you can use (//)
Returns 1.0

Exponentials (**)
Returns 4096
Returns 36.0

The Modulus (%) Performs division and returns the remainder
Returns 1
Returns 20
Returns 1.6
For more information on using python’s mathematical functions check out the python tutorial
List-A list is a variable that can hold multiple pieces of data at one time
A list can hold integers, floats and strings
You can even make lists of lists!

To create a list
>>>list_name=[‘string1’, ‘string2’, ‘string3’]
>>>Test_scores=[96, 83, 75]
>>>Celebs =[‘Beyoncé’, ‘Jay-Z, ‘Kanye’]

To access a particular list item use its index-a number indicating its place in the list
>>>print Celebs[2]

2012-12-05 10.59.49

2012-12-06 14.12.40
Kanye on an elephant

2012-12-05 16.38.46

I bet you expected it to be Jay-Z, but in python (and I think most computer languages) the index starts at 0 (this is called 0-based numbering) so to print Jay-Z you would need to write >>>Celebs[1]

To add an item to a list
list_name=[‘string1’, ‘string2’, ‘string3’, ‘new_item’]
The new item is appended to the end of the list
Celebs=[‘Beyoncé’, ‘Jay-Z, ‘Kanye’, ‘Kim’]

To remove a list item
list_name=[‘string1’, ‘string2’, ‘string3’]
If ‘item’ occurred multiple times in the list, only the first occurrence would be removed
Celebs=[‘Jay-Z, ‘Kanye’, ‘Kim’]

To change or replace a list item
list_name=[‘string1’, ‘new_item’, ‘string3’]
This will replace the second item in the list with new_item
Celebs=[‘Kris, ‘Kanye’, ‘Kim’]

If you only want part of the list you can use list slicing
This will return items from a up to but not including b
Would return ‘Kris’ and ‘Kanye’ (but not ‘Kim’)
If the first index is unspecified python will assume the slice begins at the beginning of the list
will return the same thing as

If the second index is unspecified python will assume the slice ends at the end of the list
You can also include a third index
This tells python to include list items from a to b going by c (so if c was 2 it would include every other item)
For example if you had a list of numbers
and you wanted to return every other number from the 2nd through 8th positions
Would return

You can also use a negative index to go backwards through the list
Would return
[8, 8, 4, 6, 5, 9, 3, 8, 3, 9, 2, 7, 4, 3, 1]

String slicing works exactly the same way as list slicing (just replace the list_name with the string_name)

To count the number of times a particular item occurs in a list you can use the list_name.count() command
Would return
This command also works for strings
would return the number of times ‘a’ occurs in the string

For more information on lists check out the python tutorial

Sometimes the formatting on wordpress gets screwed up with different window sizes so I am also including screen shots (at the bottom of this post) of these commands and their results in the python shell.

Because I am using this blog as a repository for my programming notes as well as an educational/community building tool, I have decided to provide a more extensive documentation of python syntax then I had originally planed. However because I want to keep the focus of this blog on bioinformatics I am going to try to include a bioinformatics problem that is solvable with the syntax that has already been introduced. I will then post my solution to the problem at the top of the next blog post.

The first bioinformatics problem!
Write a program to count the number of each base (ATCG) and the number of ambiguities(N) in the given nucleotide sequence
Don’t forget python is case sensitive
Feel free to post your solution, or any questions in the comments section.

Good Luck!

Screen Shot 2014-06-19 at 2.10.53 PM

Screen Shot 2014-06-19 at 2.11.21 PM

Screen Shot 2014-06-19 at 2.11.42 PM