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

Introduction to Python Part I

Apologies for the length of time between posts, I have been trying to determine the best way to ensure the focus of this blog stays on bioinformatics and doesn’t get side tracked by programing. On that note, this post will be the first in a two part introduction to Python. I will then begin introducing small bioinformatic programs I have written along with explanations of what the program does, other ways of solving the same problem, and why I chose that solution structure.  I will also include notes on key concepts or new syntax being introduced as well as anything I learned in the process of writing the program.

What is Python?

Python is a high level programming language* known for its clear syntax and readability

*a high level programming language is a language that is strongly abstracted from the computer. It employs natural language elements that make program development simpler, and more understandable (as opposed to machine language which can present as binary 010101).

Why Python?

Python, Perl and R are the three main languages that the bioinformaticians I have interacted with use. I began with Python because it has a reputation for being easier* to learn then other languages due to its clearer (in comparison with other languages) syntax. R is mainly for statistical data analysis (and I will be learning the basics at a workshop next week!). Eventually I also hope to learn Perl.

*Python may be easier but it is NOT easy! Sometimes people with lots of programming experience describe it as easy (because they have totally forgotten what it is like to be a beginner) which can be really discouraging to actual beginners because even if it is easier to learn than other programming languages, learning how to think in a programming language can be frustrating and difficult. Nothing is more discouraging then being told you are struggling with something “easy”.

 

What you need

You will need to make sure Python is installed on your computer as well as IDLE. IDLE is Python’s Integrated Development Environment. It works as a source code editor and a python interpreter graphical user interface (GUI). It’s basically a text editor with a few special features that help with writing and executing code.

If you are working on a Mac you most likely have an older version of Python installed by default, but not IDLE. Go to the Python download page and download the 2.7.7 version appropriate for your platform and operating system. I recommend downloading Python version 2.7.7 because that is what am using and will be explaining syntax specific to run the installer (by clicking on it in your downloads folder) and go through the prompts (the default options should be fine)

Now that you know all about the command line Open the terminal and check if you have Python (by typing “python” and hitting enter) If you have python it will return the version of python you have. 

Next check to see if you have IDLE (by typing “IDLE”)

If you have IDLE a shell will open listing your python version, followed by

“>>>”

sometimes the first time you try to open IDLE after installing Python you will get an error message, but a spaceship looking object will pop on your dock (if you hover over it it’ll say Python) just double click on the spaceship and the Python shell should appear.

>>> is the equivalent of $ in the command line, it lets you know IDLE is ready to do your bidding!

However we are using IDLE because we can write programs longer then one line in it (unlike in the command line)

In the shell open a new file (file > new file) and type the following

D=’Green eggs’

E=’and Ham’

print D + ‘ ‘ + E

Run the program by going to Run > Run Module

You will need to save the program in order to run it. I recommend creating a new folder (Python_programs) to contain all of the programs you are about to create!

If any of this was confusing feel free to check out my video

Congratulations you have just run your first program!

You may have noticed that as you were typing some words were a different color. This is one of the advantages of using IDLE over a text editor. The program recognizes various aspects of Python syntax and colors the words accordingly. This can be super helpful when you are writing code.

These are some additional resources that may be helpful:

http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/execution.html

http://www.artofproblemsolving.com/Wiki/index.php/Getting_Started_With_Python_Programming

http://rosalind.info/problems/ini1/

https://software.rc.fas.harvard.edu/training/scraping/install/

In my next post I will explain some basic Python commands and how to think in programming languages.