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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s