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
>>>candy=”Snickers”
>>>a=3
>>>b=2.0

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 (+)
>>>2+2
Returns 4
>>>2.3+3.5
Returns 5.8

Subtraction(-)
>>>4-1
Returns 3
>>>3.3-2.2
Returns 1.1

Multiplication (*)
>>>7*3
Returns 21
>>>3.1*4.2
Returns 13.02

Division (/) is special,
An integer divided by an integer returns the quotient as an integer (without the decimals place) rounded down
>>>7/3
Returns 2
>>>15/4
Returns 3
A Float divided by a float returns the a float (with the decimals place)
>>>8.6/4.4
Returns 1.9545454545454544
>>>3.3/3.3
Returns 1.0
A float divided by an integer (or an integer divided by a float) returns a float (with the decimals place)
>>>3.3/2
Returns 1.65
>>>2/3.3
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 (//)
>>>3.3//3
Returns 1.0

Exponentials (**)
>>>6**3
Returns 4096
>>>6**2.0
Returns 36.0

The Modulus (%) Performs division and returns the remainder
>>>36%7
Returns 1
>>>100%80
Returns 20
>>36%8.6
Returns 1.6
 
For more information on using python’s mathematical functions check out the python tutorial https://docs.python.org/2/tutorial/introduction.html#using-python-as-a-calculator
 
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
Kanye

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.append(‘new_item’)
Now
list_name=[‘string1’, ‘string2’, ‘string3’, ‘new_item’]
The new item is appended to the end of the list
>>>Celebs.append(‘Kim’)
Now
Celebs=[‘Beyoncé’, ‘Jay-Z, ‘Kanye’, ‘Kim’]

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

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

If you only want part of the list you can use list slicing
>>>list_name[a:b]
This will return items from a up to but not including b
>>>Celebs[0:2]
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
>>>Celebs[0:2]
will return the same thing as
>>>Celebs[:2]

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
>>>list_name[a:b:c]
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
>>>Numbers=[1,3,4,7,2,9,3,8,3,9,5,6,4,8,8]
and you wanted to return every other number from the 2nd through 8th positions
>>>Numbers[1:9:2]
Would return
[3,7,9,8]

You can also use a negative index to go backwards through the list
>>>Numbers[::-1]
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
>>>Numbers.count(4)
Would return
2
This command also works for strings
>>>string_name.count(‘a’)
would return the number of times ‘a’ occurs in the string

For more information on lists check out the python tutorial https://docs.python.org/2/tutorial/datastructures.html

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
nt_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

Advertisements