Book contents
- Frontmatter
- Contents
- Preface
- Acknowledgements
- 1 Prologue
- 2 A beginners’ guide
- 3 Python basics
- 4 Program control and logic
- 5 Functions
- 6 Files
- 7 Object orientation
- 8 Object data modelling
- 9 Mathematics
- 10 Coding tips
- 11 Biological sequences
- 12 Pairwise sequence alignments
- 13 Multiple-sequence alignments
- 14 Sequence variation and evolution
- 15 Macromolecular structures
- 16 Array data
- 17 High-throughput sequence analyses
- 18 Images
- 19 Signal processing
- 20 Databases
- 21 Probability
- 22 Statistics
- 23 Clustering and discrimination
- 24 Machine learning
- 25 Hard problems
- 26 Graphical interfaces
- 27 Improving speed
- Appendices
- Glossary
- Index
- Plate section
4 - Program control and logic
Published online by Cambridge University Press: 05 February 2015
- Frontmatter
- Contents
- Preface
- Acknowledgements
- 1 Prologue
- 2 A beginners’ guide
- 3 Python basics
- 4 Program control and logic
- 5 Functions
- 6 Files
- 7 Object orientation
- 8 Object data modelling
- 9 Mathematics
- 10 Coding tips
- 11 Biological sequences
- 12 Pairwise sequence alignments
- 13 Multiple-sequence alignments
- 14 Sequence variation and evolution
- 15 Macromolecular structures
- 16 Array data
- 17 High-throughput sequence analyses
- 18 Images
- 19 Signal processing
- 20 Databases
- 21 Probability
- 22 Statistics
- 23 Clustering and discrimination
- 24 Machine learning
- 25 Hard problems
- 26 Graphical interfaces
- 27 Improving speed
- Appendices
- Glossary
- Index
- Plate section
Summary
Controlling command execution
On the whole, a program will normally run by executing the stated commands, one after the other in sequential order, like reading the lines of a book. Frequently, however, you will need the program to deviate from this, to jump to a line that is not the next line. There are three main ways of diverting from the line-by-line paradigm. The first way is through the use of functions (subroutines), where the program’s execution jumps from a particular line of code to an entirely different spot, even in a different file or module, to do a task before (usually) jumping back again. You can even jump to a function’s subroutine from inside another function, and do this repeatedly, so that there is a stack of jumps between the current line and the first jump point. Given their importance, functions will be discussed in a dedicated chapter that follows this one.
The second way of jumping between program lines is through the use of conditional statements. Here you can check if some statement or expression is true, and if it is then you continue on with the following block of code, otherwise you might skip it or execute a different bit of code. The third way is by performing repetitive loops through the same lines of code, where each time through the loop different values may be used for the variables. Usually such loops are either done a specific number of times or until something in particular happens. When you have a collection of items, like a list, a loop can be used to consider all of the items in turn; the loop iterates over the items of the collection. A significant number of people who are new to programming find this the hardest idea to get to grips with, although Python’s syntax makes it about as easy as it can be.
- Type
- Chapter
- Information
- Python Programming for BiologyBioinformatics and Beyond, pp. 43 - 62Publisher: Cambridge University PressPrint publication year: 2015