Copyright © 2013 Dr. Martin Jones

 i

Copyright ? 2013 Dr. Martin Jones

This work is licensed under a Creative Commons Attribution-NonCommercialShareAlike 3.0 Unported License.

For more information, visit

Set in PT Serif and Source Code Pro

ii

About the author

Martin started his programming career by learning Perl during the course of

his PhD in evolutionary biology, and started teaching other people to

program soon after. Since then he has taught introductory programming to

hundreds of biologists, from undergraduates to PIs, and has maintained a

philosophy that programming courses must be friendly, approachable, and

practical.

Martin has taught introductory programming as part of the Bioinformatics

MSc course at Edinburgh University for the past five years, and is currently

Lecturer in Bioinformatics.

iii

Preface

Welcome to Python for Biologists.

Before you read any further, make sure that this is the most recent version of

the book. Python for Biologists is being continually updated and improved to

take into account corrections, amendments and changes to Python itself, so

it's important that you are reading the most up-to-date version.

This file is revision number 189. The number of the most recent revision can

always be found at:



If the revision number listed at the URL is higher than the one in bold, then

this is an out-of-date copy, and you need to download the latest version from



You'll notice from the copyright page that the contents of this book are

licensed under a Creative Commons Attribution ShareAlike license. This

means that you're free to do what you like with it ¨C copy it, email it to your

friends, wallpaper your lab with it ¨C as long as you keep the attribution. You

can also modify it, as long as you license your modification under the same

terms. The only thing that the license doesn't allow is commercial use ¨C if

you'd like to use the contents of this course for commercial purposes, get in

touch with me at

martin@

Happy programming!

iv

Table of Contents

About the author ? ii

Preface ? iii

1: Introduction and environment

1

Why have a programming book for biologists? ? 1

Why Python? ? 2

How to use this book ? 5

Exercises and solutions ? 7

Getting in touch ? 8

Setting up your environment ? 8

Text editors ? 11

Reading the documentation ? 12

2: Printing and manipulating text

13

Why are we so interested in working with text? ? 13

Printing a message to the screen ? 14

Quotes are important ? 15

Use comments to annotate your code ? 16

Error messages and debugging ? 18

Printing special characters ? 21

Storing strings in variables ? 21

Tools for manipulating strings ? 24

Recap ? 34

Exercises ? 36

Solutions ? 39

3: Reading and writing files

Why are we so interested in working with files? ? 52

Reading text from a file ? 53

Files, contents and file names ? 55

Dealing with newlines ? 57

Missing files ? 60

52

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download