100% found this document useful (1 vote)
37 views51 pages

L1 Chapters 1 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
37 views51 pages

L1 Chapters 1 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Introduction to

for
B������������� R�������
E-Learning Edition

Pouria Hadjibagheri

Developed by SysMIC®

2017-19
Contents

Contents iii

1 Ge�ing Started 1

1.1 Programming — why bother? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1.1 Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1.1.1 How to learn it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.1.2 Python: The Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.1.3 Ge�ing Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.1.3.1 Why Python 3? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.1.3.2 Why Anaconda? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.1.3.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Your first programme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2.1 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.2.1.1 Python files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 Variables, Types, and Operations 7

2.1 I/O Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.1.1 I/O Operations in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.1.1.1 Producing an output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Contents iii
2.1.1.2 Receiving an input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

� E������ 1: Navigation in the terminal environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.2 Variables And Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.2.1 Variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.2.1.1 Using variables with input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.2.2 Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2.2.2.1 Conversion of types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.2.2.2 Handling input variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.2.3 Variable scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.3 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

2.3.1 Mathematical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

2.3.1.1 Shorthands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.3.1.2 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

2.3.1.3 Non-numeric values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

2.3.2 Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

2.3.2.1 Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2.3.2.2 Disjunctions and Conjunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2.3.2.3 Complex logical operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3 Conditional Statements 45

3.1 The concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

3.2 Algorithms and algorithmic thinking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

� E������ 1: Algorithms in daily life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.3 Conditions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

3.3.1 Indentation Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

3.3.2 Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

� E������ 1: A familiar scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

4 Introduction to Arrays 57

4.1 List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4.1.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4.1.2 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

4.1.3 Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

4.1.4 From value to index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

iv Contents
4.1.5 Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

4.1.5.1 Addition of new members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

4.1.5.2 Modification of members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

4.1.5.3 Removal of members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

4.1.5.4 Method–mediated operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

4.1.6 List members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

4.1.6.1 Membership test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

4.1.6.2 Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

4.1.7 Weak References and Copies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

4.1.8 Conversion to list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.1.9 Useful methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

4.1.10 Nested arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4.1.10.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4.1.10.2 Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4.1.11 Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

4.1.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

4.2 Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

4.2.1 Conversion to tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

4.2.2 Immutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

4.2.3 Packing and unpacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

4.2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

5 Loops and Iteration 95

5.1 The concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

5.2 for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

� E������ 1: Extended example of iterations using for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

5.2.1 Retaining values generated in a loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

5.2.2 Enumeration: counting the iteration cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

5.2.2.1 Unpacking tuples in for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

5.2.2.2 Enumerating an array of tuples: unpacking nested tuples . . . . . . . . . . . . . . . . . 106

5.2.3 for-loop and conditional statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

5.2.4 Sequence of numbers in for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

� E������ 2: Sequence comparison, dot plots and for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

5.3 while-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

5.3.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

Contents v
5.3.2 Breaking a while-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

6 Associative Arrays and Sets 121

6.1 Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

6.1.1 Interacting a with a dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

6.1.2 Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.1.3 Nested dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

� E������ 1: Nested dictionaries in practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

6.1.4 Useful methods for dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

6.1.5 for-loop and dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

6.1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

� E������ 2: Frequency analysis with for-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

6.2 Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

6.2.1 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

6.2.1.1 Distinct values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

6.2.1.2 Immutable values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

6.2.1.3 Orderless . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

6.2.1.4 Not indexed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

6.2.2 Manipulation of sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

6.2.3 Unique functionalities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

6.2.4 Conversion to set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

6.2.5 Cheatsheet of methods for built-in arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

7 Strings and Files 155

7.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

7.1.1 Format string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

7.1.1.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

7.1.1.2 Named placeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

7.1.1.3 Format specification mini-language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

7.1.1.4 Format string and dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

7.1.1.5 Format strings since Python 3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

7.1.2 String operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

7.1.2.1 Case homogenisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

7.1.2.2 Finding substring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

vi Contents
7.1.2.3 String preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

7.1.2.4 Escape sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

7.1.2.5 Split and join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

7.2 File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

7.2.1 Reading from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

7.2.2 Writing to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

8 Functions 183

8.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

8.1.1 Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

8.1.1.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

8.1.2 Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

� E������ 1: Reading FASTA documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

8.1.3 Optional arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

8.1.3.1 Simplification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

8.1.4 Interconnectivity of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

� E������ 2: A mini toolbox for statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

8.2 Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

8.2.1 Comprehension techniques and Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

8.2.1.1 List comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

8.2.1.2 Dictionary comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

8.2.1.3 Generator expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

� E������ 1: Timing di�erent techniques to address a problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

8.2.2 Versatile functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

8.2.2.1 Functions as input arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

8.2.2.2 lambda functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

8.2.3 Built-in tools for functional programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

8.2.3.1 Iterables, iterators and generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

8.2.3.2 The range() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

8.2.3.3 The reversed() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

8.2.3.4 The map() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

8.2.3.5 The filter() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

8.2.3.6 The zip() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

8.2.3.7 The any() and all() functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

8.2.4 Recursive functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

Contents vii
End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

9 Regular Expressions 241

9.1 RegEx Library in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

9.2 RegEx Pa�erns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

9.2.1 Literal characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

9.2.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

9.2.2.1 Match objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

9.2.3 Case sensitivity and Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

� E������ 1: Finding a sequence of characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

9.3 Special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

9.3.1 Composition of pa�erns using special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

� E������ 1: Matching a consensus sequence of amino acids . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

9.3.2 Grouped pa�erns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264

9.3.2.1 �alifiers and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

9.3.3 Named Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268

9.3.4 Repetitive pa�erns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

9.3.5 Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

� E������ 2: Parsing with RegEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

9.4 RegEx Cheatsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

End of Chapter Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

A Programming Environment 281

A.1 Visual Studio Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

A.1.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

A.1.2 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

A.1.2.1 The Python extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

A.1.2.2 Ge�ing started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

A.1.2.3 The Code Runner extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

A.1.2.4 The Linter extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

A.1.2.5 Themes: Personalise your IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

A.1.3 Go futher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

DIY Answers 295

Index 305

viii Contents
1
P���������� — ��� ������?
Why Python?

G������ S������

1.1 P���������� — ��� ������?

As biologists, we routinely analyse the results of our experiments. Sometimes, we even


go further and combine our data with those already available in the field — e.g. genomics
data; to enable or facilitate additional investigations.
Nowadays, big data is one of the hottest topics in bioresearch. In October 2017, Wired,
the American technology magazine, published an � article in which the author outlines the
reasons why several established bioscience researchers working in reputable institutions
felt the need to learn coding — specifically in Python.
Whilst focusing primarily on the Python programming language; throughout this book,
we will discuss some universal principles shared amongst many programming languages.
Through a diverse range of examples, we will be training these principles by solving ev-
eryday problems that we face in biological research.

1.1.1 W�� P�����?

Bioinformatics is concerned with the development of software solutions for processing,


and thereby understanding biological data. Python is a general-purpose programming lan-
guage. This means that it can be exploited to design any type of software, from desktop
and web, to robotics and automation, to data analysis. The 2017 survey conducted by
the Python Software Foundation and JetBrains® found that approximately 50% of Python
programmers use it � to conduct data analysis as their first or second choice.

Chapter 1. Ge�ing Started 1


P���������� — ��� ������?
Ge�ing Started

In addition, the 2018 developer survey conducted by StackOverflow found that Python is
the � second most popular general-purpose programming language after Java. This means
that it enjoys a vibrant community that support and maintain its development, and the de-
velopment of the libraries associated with it. This is confirmed by the fact that the language
is ranked as the � most popular language on GitHub® , which is the primary code-sharing
platform amongst programmers.
The language is � fastest growing major programming language in the world, and is voted
as the � most wanted programming language by employers. So if you are not convinced
by its popularity or features; you can always look into the � employment prospects. Not to
mention that programming in general is ranked amongst top starting salaries for graduates,
is an in-demand skill for jobs in biosciences.

1.1.1.1 How to learn it?

Learning how to code as a beginner is akin to learning how to walk as a baby. You cannot
learn programming by memorising directives; no matter how well you memorise them.
To learn how to code, one must learn how think algorithmically; that is, how to break
different tasks down into logical procedures. The only way to learn how to think that way
is to practice, make mistakes, and how to overcome those mistakes. It is common to make
the same mistake more than once, especially in different contexts, and that can become
frustrating at times. However, once you get it, you will have it for life.
There are lots materials on the web, both free and paid, to assist with your learning. Use
them to your advantage. Great programmers are not the ones who know every technical
detail by heart; rather, they are the ones who know what they are looking for, and where
they can find the answer.
Although we focus on programming in the context of bioinformatics, throughout this course,
you will acquire many transferable skills that you may employ for other purposes or to learn
other specialities.

1.1.2 P�����: T�� P���������� L�������

Python is a general-purpose, high-level programming language. It was invented by the


� I���������� ���� Dutch computer programmer � Guido van Rossum and was released for the first time in
Unlike popular belief, the name Python has 1990.
nothing to do with the snake. It is in fact de-
rived from the popular British sketch com- A high-level programming language is a language that enjoys strong abstraction from com-
edy series � Monty Python’s Flying Circus,
of which van Rossum was a self-confessed
puter details. This means that it is closer to the language that is spoken and understood by
fan. humans; which put simply, makes the language more enjoyable to work with.
In terms of compilation, programming languages may be divided into two different cate-
gories:

Compiled These are languages whose code is translated (compiled) into machine lan-
� I���������� ���� guage en-masse, and in advance, using a designated compiler programme —
Although the default behaviour of Python e.g. C, Rust, Haskell.
is as an interpreted language; its code may
also be compiled using special tools and tech-
niques. See � Cython and � PyPy to find out Interpreted Rely on and must always be accompanied with an interpreter, whose job
more. is to translate the source code into machine language one line at a time —
e.g. Python, R, MATLAB® .

2 Chapter 1. Ge�ing Started


P���������� — ��� ������?
Ge�ing Started

1.1.3 G������ S������

In this subsection, we will learn about different versions of Python, and why should we use
the Anaconda distribution of Python 3.

1.1.3.1 Why Python 3?

You may have been told that you should learn Python 2. You may even have seen you
colleagues using or teaching Python 2. It is sometimes claimed that biologist should use
Python 2, because most biology related libraries in Python are written for that version.
This is wrong. There reason why Python 2 is still out there is that following the release
of Python 3.0 in December 2008, the CPython interpreter sustained several problems, and
was not backward compatible. This meant that, any code written in Python 2, could not be
run using Python 3 without modifications.
In 2008, Python was already a relatively popular language amongst professional program-
mers, and had a lot of open source libraries available, and maintained by the community.
This change meant that the existing libraries could no longer be used. This caused a little
bit of disruption, and led to some resistance in the community. Additionally, the release
of Python 2.7 coincided with the release of Python 3.1, and shared its new features; so the
community did not feel the need to move on.
Finally, in 2014, the Foundation announced that the support for Python 2.7 will cease in
2020, and encouraged the users to move on as soon as possible. The results of the Python
Developer’s Survey 2018 revealed that 84% of the community, and 90% of data scientists,
have � already adopted Python 3. It is considerably faster, offers more features, and is the
future of the language. Python 2 is obsolete. Do not use it.

1.1.3.2 Why Anaconda?

For the purpose of this book, we will be using the Anaconda release of � CPython, the
default Python interpreter released by the � Python Software Foundation, and maintained
by the � Anaconda Cloud.
The Anaconda distribution of CPython automatically installs almost every package you
would need for scientific purposes (over 720 open source packages). It is easier to in-
stall, and it takes care of all the dependencies. This is particularly important because
some of Python’s scientific libraries have Fortran– and C–based dependencies, which may
be somewhat challenging to install for beginners, specially on Microsoft® Windows® and
Mac OS X® .

1.1.3.3 Installation

To install the Anaconda distribution of Python, please visit the � installation instructions as
outlined in the Anaconda Cloud documentations, and follow the instructions for your oper-
ating system. Ensure that you use the Python 3.x graphical installer for Microsoft® Windows® and
Mac OS X® (there is no graphical installer for Linux). Once downloaded, you can proceed
to install the distribution as you would any other application on your computer.

Chapter 1. Ge�ing Started 3


Y��� ����� ���������
Documentation

1.2 Y��� ����� ���������

There are two types of programming languages: compiled and interpreted. In a compiled
language, such as the C programming language, the compiler translates our entire code into
a machine language programme, which we can then run. On the other hand, an interpreted
language would adopt one of the following three strategies:
• Parse the code (resolve into meaningful sections) and perform the instructions di-
rectly — e.g. early versions of the LISP programming language.
• Translate the code into a more efficient alternative before executing it — e.g. Python,
MATLAB® , Perl, Java, Julia.
• Explicitly execute a pre-compiled code — e.g. Pascal, JIT compilers (Python, Ju-
lia, . . . ), Java.
It is, however, important to appreciate that whilst compilation and interpretation are the
primary methods for the implementation of programming languages, they are not mutually
exclusive. In other words, an interpreted language such as Python or Java may perform
code translation in a manner that would imitate a compiled language.
To create a Python programme, we write our code in a simple text file and save it with
a .py format. We then use the Python interpreter to run our code. The Python interpreter is
an independent programme that we use to run our code. The Python interpreter checks our
code to ensure that it does not contain any syntactic mistakes. Once confirmed, it starts to
execute the code from the beginning, one line at a time. An important implications of this
principle is that we cannot reference a something — e.g. a variable or a function — before
its definition. We shall review such implications in more depth later in the book.

1.2.1 D������������

The source code of a software application must be accompanied with a text containing
certain information about the software. The text may be embedded within the code, be
placed in a separate document, or both.
How we embed the text within our code depends on which programming language we are
using. In Python, embedded documentations must be placed inside triple quotation marks
("""...""") at the very beginning of the file.
At the very least, our documentation must contain the following information:
• A very brief description of the code.
• The data on which the code was last modified.
• The copyright notice.
• The license.

� R�������
A code whose license is not explicitly indicated must always considered to be protected under exclusive
copyright. There are lots of reasons why you should elect to release your code as free so�ware. Visit
the � Open Source Initiative (OSI) website to find out more about open source, or drop by � ChooseALi-
cense.com to choose a license.

4 Chapter 1. Ge�ing Started


Y��� ����� ���������
Documentation

If you want to be as open as possible, consider using the � MIT License.

The following is an example of an embedded documentation for a Python script:


1 """
2 A short description of what your code.
3

4 Last modified: DAY/MONTH/YEAR (can include the time, too)


5

6 Copyright (C) YEAR - YOUR NAME.


7

8 License: LICENSE OF YOUR CODE - e.g. MIT


9 """

In Python, documentations that are embedded in the code using triple quotation marks are
referred to as docstring text.
It is always important to write a code that is explicit, legible, and easy to follow. However,
more often than not and due to a variety of reasons, the logic of the code may be somewhat
difficult to understand for someone else. To that end, it would be useful — if not essential
— to explain certain parts of the code and provide additional comments. Such explanations
will help us and other fellow programmers understand our code more easily in the future.
To provide comments on a specific line of code in Python, we use the octothorpe sign (#)
followed by a space:
1 This is a comment - it is not a part our code,
2 # and will be ignored by the interpreter.
3 x = 2 + 3

It is also possible to write comments on the same line as our code; however, we should do
so sparingly. To write such comments, we conventionally add two space characters before
the octothorpe.
1 x = 2 + 3 # This is an in-line comment.

1.2.1.1 Python files

Due to certain — primarily historical — issues; when we create a Python file, it is important
that we adhere to two conventional principles:

• Each line in a Python file may contain a maximum number of 79 characters. It is also
advised that we should limit docstrings and comments to a maximum of 72 characters
per line. For additional details, please visit the documentations for � Maximum Line
Length.

• Certain editors and operating systems requires two linebreaks at the end of a file. It
is therefore important to always leave one, and only one blank line at the very end
of a Python file. The blank line combined with the last line of our actual code would
produce two linebreaks at the end of our file.

Chapter 1. Ge�ing Started 5


V��������, T����,
��� O���������
In programming, we process data and produce outputs. When the data is being processed,
2
it is stored in the memory, so that it is readily available, and can therefore be subject to the
processes we want to apply.
Throughout this section, we will discuss how to handle data in Python. We start by learning
how to display data on the screen, and receive an input from a user. We then use these
techniques to perform different mathematical, and logical operations.
This chapter introduces the fundamental principles that we will employ every time we code
in Python. On that account, it is very important that you understand this chapter well before
moving on.

Chapter 2. Variables, Types, and Operations 7


I/O O���������
I/O Operations in Python

2.1 I/O O���������

In computer science, input or output operations refer to the communication between an


information processing system such as a computer, and the outside world, which may be
a user or another computer. Such communications are more commonly known as I/O op-
erations. In general, the outside world — especially in the context of this course, may be
loosely defined as anything that falls outside of the environment that is directly controlled
by an application.

� R�������
Only what we define within the environment of our application and store in the memory is directly con-
trolled by our application. We may access or take control over other environments through certain media;
however, such interactions are classified as I/O operations. An example of this is interacting with a file
on our computer, which we discuss in chapter 7. Whilst we have complete control over the file while we
are working on it (e.g. reading from it or writing to it), our access to the file and the transmission of data
is in fact controlled and managed by the operating system.
� A������� �����
If you are interested in learning more about
I/O systems and how they are handled at op-
erating system level, you might benefit from In programming, I/O operations include, but are certainly not limit to:
chapter 13 of Operating Systems Concepts, 8th
ed. by Abraham Silberschatz, Greg Gagne, • displaying the results of a calculation to the user;
and Peter Galvin.
• asking the user enter a value;

• writing or reading data to and from a file or a database;

• downloading data from the Internet;

• operating a hardware (e.g. a robot);

• any operation that allows our programme to communicate the data stored in the mem-
ory.

2.1.1 I/O O��������� �� P�����

� Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Input and Output

In this section, we learn about two very basic but most fundamental methods of I/O oper-
ations in Python. We will be using these methods throughout the course, so it is essential
that you feel comfortable with them and the way they work before moving on. We shall
return to I/O operations later in the course, especially in chapter 7.

2.1.1.1 Producing an output

� Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Print

The term “output” in reference to an application typically refers to the data that has either
been generated, or manipulated by that application.

8 Chapter 2. Variables, Types, and Operations


I/O O���������
I/O Operations in Python

For example; we have two number and we would like to calculate their sum. The action
of calculating the sum is itself a mathematical operation (discussed in section 2.3.1). The
result of our calculation is called an output. Once we obtain the result, we might want to
save it in a file or display it on the screen, in which case we will be performing an I/O
operation.
The simplest, most frequently used method for generating an output in almost every modern
programming language is to display something on the screen.
We normally run our Python scripts and programmes from the terminal; so the typical (and
the easiest) method for producing an output is to display it in the terminal. The way we do
this is by calling a dedicated built-in function called print() .

FUN
� R�������
In programming, a function is essentially an isolated piece of code. It usually to takes some inputs, does
something to or with them, and produces an output. The pair of parenthesis that follow a function are
there so that we can provide the function with the input arguments it needs when we call it, so that it can
do what it is supposed to do using our data. We will explore functions in more details in chapter 8.

The print() function can take several inputs and performs different tasks. Its primary
FUN

objective, however, is to take some values as input and display them in the terminal. Here
is how it works:
Suppose we want to display some text in the terminal. To do so, we write:
In [1]: 1 print('Hello world!')

in your favourite editor or IDE and save it as script_a.py in a file. This is now a fully
functioning python programme that we can run using the Python interpreter.

� R�������
To name a Python script, we must adhere the file naming protocol:
• The file name must not start with a number.
• The file name should ideally not contain capital le�ers.
• Except for underscores or _ , the file name must not contain any special characters or spaces.
• Conventionally, the file name should not be wri�en in camel case — e.g. scriptA.py
• The file name should be kept short, ideally less than 30 characters.
• The file must be given a .py extension — e.g. script_a.py

If you are using an Integrated Development Environment (IDE) — e.g. Visual Studio Code,
you may execute your code using the inernal tools provided by that IDE. The specifics of
how you do so depends on the IDE that you are using.
Alternatively, we may always execute Python scripts manually. To do so, we open the ter-
minal or the command prompt (CMD) in Microsoft® Windows® and navigate to the directory
where we saved script_a.py. � N���
If you don’t know how to navigate in the ter-
Once in the correct directory, we run the script by typing python3 script_a.py in our minal, see the example 1 the end of this sec-
tion.
terminal as follows:

Chapter 2. Variables, Types, and Operations 9


I/O O���������
I/O Operations in Python

python3 script_a.py

This will call the Python 3 interpreter to execute the code we wrote in script_a.py. Once
executed, which in this case should be instantaneous, we should see the output:
Out [1]: Hello world!

Congratulations. . . you have now successfully written and executed your first programme
in Python.

� R�������
We know that print() is a function because it ends with a pair of parenthesis, and it is wri�en
FUN

entirely in lowercase characters. Conventionally, functions in Python should always be defined using
lower case characters (� PEP-8: Function Names). Some IDEs change color when they encounter built-in
functions in the code so that we won’t accidentally overwrite them. We shall discuss functions in more
details in chapter 8.

Note We never overwrite a build-in function. Doing so would means that we lose that function
during the execution of our programme. For instance, if we overwrite print() , we

FUN
would no longer be able to use it for displaying the outputs of that programme using
print() . This is because as far as Python is concerned, we have redefined the name
FUN

print; which means that it no longer represents the original definition for displaying
outputs.
Python is referred to by its programmers known as an adults’ language. This means that
it does not impose many of the restrictions that exist in other languages. One example
of such restrictions is that in most programming languages, the programmer is unable to
overwrite built-in functions. Conversely, in Python, it is up to the programmer to make
a decision. This makes Python a very powerful language, because it allows the program-
mer to micro-manage the behaviour of the language at its core as and when necessary;
however, it must be used very cautiously for with great power, comes great responsiblity.
To that end, Python relies on a set of conventions (� PEP-8: Function Names) to let the
programmer know that, for instance, a specific name is reserved for a built-in function.
IDEs tend to interpret these conventions in di�erent ways (e.g. by changing the colour).
This is in fact one the reasons why we should use an IDE instead a simple text editor. If
you are interested, changes to the language or its conventions are proposed and then put
to vote by the Python developers community in a process that is faciliated by the � Python
So�ware Foundation. You are now a member of the Python developers community, so feel
free to contribute, vote, or voice your opinion.

We can pass more than a single value to the print() function, provided that they are
FUN

separated with a comma. For instance, if we write:


In [2]: 1 print('Hello', 'John')

and run the script, the results would be:


Out [2]: Hello John

Notice that there is a space between 'Hello' and 'John' even though we did not include
a space in our text. This is the default behaviour of the print() function when it
FUN

receives more than a single value (argument).


This default behaviour may be inhibited:

10 Chapter 2. Variables, Types, and Operations


I/O O���������
I/O Operations in Python

In [3]: 1 print('Hello', 'John', sep='')

Out [3]: HelloJohn

In [4]: 1 print('Hello', 'John', sep='--')

Out [4]: Hello--John

In [5]: 1 print('Jane', 21, 'London', sep='.')

Out [5]: Jane.21.London

� R�������
In Python, values that do not require a name when passed to a function — e.g. Jane, 21, or London in the
last example, are known as positional arguments. On the other hand, values that require their name to be
specified such as sep=... are referred to as keyword arguments. Values that do not require to be specified
when calling a function and have a default value — e.g. sep=' ', are called default arguments.

F������� ����

Input arguments

Function Positional arguments Keyword Arguments


name ( args) ( kwargs)

print('something to display', 'something else', sep=' ', end='\n')

Keyword Default value


(preset but changable)
Default arguments

The input and the outputs of a function are referred to as the function signature.

D� I� Y������� 2.1.1 �
Write a script that displays the following output:

Protein Kinase C (Alpha subunit)

Chapter 2. Variables, Types, and Operations 11


I/O O���������
I/O Operations in Python

2.1.1.2 Receiving an input

� Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Input

Inputs are I/O operations that involve receiving some data from the outside world. This
might include reading the contents of a file, downloading something the Internet, or asking
the user to enter a value.

� N��� The simplest way to acquire an input in Python is to ask the user to enter a value in the
terminal. To do so, we use a dedicated built-in function called input() . The function

FUN
®
In a Unix system (Mac OS X or Linux), a tilde
is an alias for a user’s home directory.
takes a single argument called prompt. Prompt is the text displayed in the terminal to ask
the user for an input. Figure 2.1.1, illustrates a screen shot of my personal computer’s
prompt, where it displays my user name (i.e. pouria) followed by a tilde (˜ ). A terminal
prompt may be different in each computer and operating system.
Here is how we implement the input() function:

FUN
In [6]: 1 input('Please enter your name: ')

which is exactly the same as:


In [7]: 1 input(prompt='Please enter your name: ')

If we save one of the above in a file and call it script_b.py; and execute the file using the
Python 3 interpreter as described previously, we shall see the following:
python3 script_b.py

Please enter your name: _

Note Boxes of code that do not have a number (i.e. there is not out [X]:) rep-
resent the intermediary output of the preceding line in terminal where the
programme requires an input by the user before it can continue to produce
an output.

F����� 2.1.1 Terminal window on a Linux computer (le�) and a Mac (right).

12 Chapter 2. Variables, Types, and Operations


I/O O���������
I/O Operations in Python

The terminal cursor, displayed as an underscore in our example, will be in front of the
prompt (i.e. Please enter your name: ) waiting for a response. Once it receives a re-
sponse, it will proceed to run the rest of the code (if any), or terminate the execution.

� R�������
Python is an interpreted language; that is, the code we write is executed by the Python interpreter one
line at a time. The input() function performs a blocking process. This means that the execution of
FUN

the code by the Python interpreter is halted upon encountering an input() function until the user

FUN
enters a value. Once a value is entered, the interpreter then proceeds to execute the next line.

We may store the user’s response in a variable. Variables are the topic of the next section,
where we shall also review more examples on input() and how we can use it to
FUN
produce results based on the responses we receive from the user.

D� I� Y������� 2.1.2 �
Write a script that asks the user to enter the name of a protein in the terminal.

Chapter 2. Variables, Types, and Operations 13


I/O O���������
I/O Operations in Python

� E������ 1: N��������� �� ��� �������� �����������

Terminal is a � command-line interface emulator. It provides an interface to a computer programme in which a user
can issue commands in successive lines to perform specific operations.
How to navigate the terminal in Mac OS X and Linux?
The Unix terminal — i.e. the one used by Mac OS X® and Linux; uses a command language known as � Bash, also
referred to as the Unix Shell. This is not the topic of this book, so we won’t discuss its properties and features in
much detail.
Microso�® Windows® , on the other hand, relies on DOS (Microso�® Disk Operating System® ) commands to nav-
igate the terminal. The terminal environment in Microso�® Windows® is sometimes referred to as the command
prompt or CMD.
The enviroment in which we can use Bash or � DOS commands is known as the terminal. To use a terminal, we
need a terminal emulator. Every operating system has a default terminal emulator, however, you can always use
an alternative emulator if you so wish. Some IDEs (e.g. Visual Studio Code, Sublime Text, PyCharm® ) also o�er a
terminal emulator that is embedded within the IDE.

C������ ������� ���������

To navigate the terminal environment, we should first find out where we are. Here is how we do that:

Unix Microso�® Windows®

pwd pwd

Remember that the path to an enviroment is displayed (and supplied di�erently in Unix and Microso�® Windows® ).
To that end, the response (output) to these commands may slightly vary:

Unix Microso�® Windows®

/home/UserName/Documents C:\Documents

Notice that in Microso�® Windows® , we separate directories using a backslash ( \ ), whilst in Unix, we use a foreslash
( / ) for that purpose.

C������� ��� ������� ���������

To navigate to another directory, we do as follows:

Unix Microso�® Windows®

Without spaces in the name cd /home/UserName/Documents/Python_Scripts cd C:\Documents\Python_Scripts


With spaces in the name cd "/home/UserName/Documents/Python Scripts" cd "C:\Documents\Python Scripts"

Note Paths that contain one or more space characters must be encapsulated by double quotation marks
("...").

If we are already in the Documents directory, and want to navigate onto Python_Scripts, we can use the following
shorthand versions of the above method:

14 Chapter 2. Variables, Types, and Operations


I/O O���������
I/O Operations in Python

Unix Microso�® Windows®

Without spaces in the name cd Python_Scripts cd Python_Scripts


With spaces in the name cd "Python Scripts" cd "Python Scripts"

P����� ���������

To navigate to the parent directory — i.e. from Python_Scripts to Documents in this example; we do:

Unix Microso�® Windows®

cd .. cd ..

H��� ���������

We can also navigate all the way back to the home directory in one go:

Unix Microso�® Windows®

cd cd \

C������� � ��� ���������

We can create a new directory as follows:

Unix Microso�® Windows®

mkdir new_dir_name md new_dir_name

C������� �� � ���������

To see the contents of a directory, we use the following commands:

Unix Microso�® Windows®

ls dir

Chapter 2. Variables, Types, and Operations 15


V�������� A�� T����
Variable names

2.2 V�������� A�� T����

We use variables to store data in the memory. Each variable has 3 characteristics: scope,
name, and type. Scope and name must be mutually unique. Starting with name, we will
discuss each of these characteristics in more details throughout this chapter.

2.2.1 V������� �����

� Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � PEP–8 Naming Conventions

Name of a variable is in fact an alias for a location in the memory. You can think of it as a
postbox, which is used as a substitute for an address. Comparably, we use variable names so
we wouldn’t have to use the actual address to the location we want in the memory because
it would look something like 0x106fb8348.
There are some relatively simple rules to follow when defining variable names, which ulti-
mately boil down to:

V������� E������ N���

Invalid 5mins = 5 SEC_IN_MIN Variables cannot start with numbers — raises a SyntaxError.
Invalid if = 2 A�empts to overwrite an internal syntax — raises a SyntaxError.
Worst j = 2 Overwrites the internal identifier for complex numbers.
Worst int = 2 Overwrites the internal definition for integer numbers.
Bad a = 4 Meaningless name + hard to identify in code.
Okay var1 = 0 Meaningless name + hard to distinguish.
Be�er var_1 = 0 Meaningless name.
Good current_state = 0 Good name + easy to distinguish.
Okay five_mins_in_sec = 5 60 Good name, vague value.
Good SEC_IN_MIN = 60 Good name + good value + distinguished as a constant (conventionally, names made
exclusively of capital le�ers signify constants).
Good five_mins2sec = 5 SEC_IN_MIN Good name + good value.
Good five_mins2sec = 5 60 # 5 min 60 sec Good name + vague value clarified by a comment.
Good current_protein = 'DNA Polymerase III' Good name + good value.

� R�������
We should never overwrite an existing, built-in definitions or identifier (e.g. int or j). We will be learning
many such definitions and identifiers as we make progress. Nonetheless, a good IDE would highlight
syntaxes and built-in identifier in a di�erent colour. That is why internal identifiers are displayed in a
di�erent colour in table 2.2.1. The exact colourig scheme depends on the IDE and the theme.

Once a variable is defined, its value may be altered or reset:


In [1]: 1 total_items = 2
2

3 print(total_items)

16 Chapter 2. Variables, Types, and Operations


V�������� A�� T����
Variable names

Out [1]: 2

In [2]: 1 total_items = 3
2

3 print(total_items)

Out [2]: 3

Variables containing integer numbers are known as int, and those containing decimal num-
bers are known as float in Python.
In [3]: 1 total_items = 2
2

3 print(total_items)

Out [3]: 2

In [4]: 1 total_values = 3.2


2

3 print(total_values)

Out [4]: 3.2

In [5]: 1 temperature = 16.


2

3 print(temperature)

Out [5]: 16.0

Variables can contain characters as well; but to prevent Python from confusing them with
meaningful commands, we use quotation marks. So long as we remain consistent, it doesn’t
matter whether we use single or double quotations. These variables are known as string or
str:
In [6]: 1 forename = 'John'
2 surname = "Doe"
3

4 print('Hi,', forename, surname)

Out [6]: Hi, John Doe

D� I� Y������� 2.2.1 �
Oxidised low-density lipoprotein (LDL) receptor 1 mediates the recognition, internalisation and degradation of oxida-
tively modified low density lipoprotein by vascular endothelial cells. Using the � Universal Protein Resource (UniProt)
website, find this protein for humans, and identify:
• UniProt entry number.
• Length of the protein for isoform 1 (under Sequences).
• Gene name (under Names & Taxonomy).

Store the information you retrieved, including the protein name, in 4 separate variables.
Display the values of these 4 variables in one line, and separate the items with 3 spaces, as follows:

Name EntryNo GeneName Length

Chapter 2. Variables, Types, and Operations 17


V�������� A�� T����
Variable names

F����� 2.2.1

2.2.1.1 Using variables with input

Now that we know how to create variables to store values, we can also use them to retain
the value (response) entered by the user when using the input() function:

FUN
In [7]: 1 name = input('Please enter your name: ')
2

3 print('Hi,', name)

If we save this in a file called script_c.py and run it, the output will appear in the terminal
as displayed in figure 2.2.1.

� R�������
The input() function displays a prompt and waits for the user to enter a value. The value entered
FUN

by the user is then returned by the function as a str type. The value returned by a function may also be
referred to as the output of that function. The output of a function may be stored in a variable.

D� I� Y������� 2.2.2 �

1. Write a script that upon execution, asks the user to enter the name of an enzyme and then retains the response in
an appropriately named variable.
2. Use the variable to display an output similar to the following:

ENZYME_NAME is an enzyme.

where ENZYME_NAME is the name of the enzyme entered in the prompt.


3. Now alter your script to ask the user to enter the number of amino acids in that enzyme. Retain the value in another
appropriately named variable.
4. Alter the output of your script to display a report in the following format:

ENZYME_NAME is an enzyme containing a total number of AMINO_ACIDS amino acids.

where AMINO_ACIDS is the number of amino acids.

18 Chapter 2. Variables, Types, and Operations


V�������� A�� T����
Variable Types

2.2.2 V������� T����

� Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Built-in Types


� A������� �����
In computer programming, type systems are
syntactic methods to enforce and / or iden-
When it comes to types, programming languages may be divided into two distinct cate- tify levels of abstraction. An entire field
in computer science has been dedicated to
gories: the study of programming languages from a
type–theoretic approach. This is primarily
Statically typed Languages that require the programmer to define the type for due to the implication of types and their un-
derlying principles in such areas in so�ware
every variable (statically typed). engineering as optimisation and security.
To learn more about the study of type sys-
Dynamically typed Languages that define and maintain the types on the fly. tems, refer to: Pierce B. Types and program-
ming languages. Cambridge, Mass.: MIT
Press; 2002.
Python is a dynamically typed language. This means that, unlike statically typed languages,
we rarely need to worry about the type definitions because in the majority of cases, Python
takes care of them for us.

� R�������
In a dynamically typed language, it is the value of a variable that determines the type. This is because
the types are determined on the fly by the Python interpreter as and when it encouters di�erent variables
and values.

� N���
Why learn about types in a dynamically typed programming language? In a dynamically typed language, the val-
ues determine the type of a variable. This
Now that we know how to define variables in Python, you may have noticed that depending is in contrast with statically typed languages
where a variable must be initialised using a
on the value, there are different forms of variables such as int, float, and str. These are specific type before a value — whose type is
built-in definitions known as variable types. In essence, types tell the computer how much consistent with the initialised variable, can
be assigned to it.
space in the memory should be reserved for the value of a specific variable, and clarify the
operations that may be applied to it.
Python enjoys a powerful type system out of the box. Table 2.2.2 provides a comprehensive
reference for the built-in types in Python. Built-in types are the types that exist in the
language and do not require any third party libraries to implement or use.
Sometimes we might need want to know what is the type of a variable. To do so, we use
the build-in function type() as follows:
FUN

In [1]: 1 total_items = 2
2

3 print(type(total_items))

Out [1]: <class 'int'>

In [2]: 1 total_values = 3.2


2

3 print(type(total_values))

Out [2]: <class 'float'>

In [3]: 1 temperature = 16.


2

3 print(type(temperature))

Chapter 2. Variables, Types, and Operations 19


V�������� A�� T����
Variable Types

T��� T��� N��� S������������ N������ S������� I������� M������ C��������

bool Boolean True or False �


int Integer An integer number: x ∈ Z �
float Floating point A rational number: x ∈ Q �
complex Complex A complex number: x ∈ C �
bytes Bytes Single byte

bytearray Byte Array Array of bytes; i.e. binary � � �


str String Array of characters; i.e. text. � �
list List Array of any combination � � �
tuple Tuple Array of any combination � �
set Set Collection of unique members � � �
frozenset Frozen Set Collection of unique members � �
dict Dictionary Mapping (associative array) $
� � � �

F����� 2.2.2 A comprehensive (but non-exhaustive) reference of built-in (native) types in Python 3.
Not discussed in this course — included for reference only.
$
dict is not an iterable by default, however, it is possible to iterate through its keys.
Mutability is an important � concept in programming. For the purpose of this tutorial, a mutable object is an object whose value(s) may be altered.
This will become clearer once we study list and tuple. Find out more about mutability in Python from the � documentations.

Complex numbers refer to a � set of numbers that have a real part, and an imaginary part; where the imaginary part is defined as −1. These
numbers are very useful in the study of oscillatory behaviours and flow (e.g. heat, fluid, electricity). To learn more about complex numbers, watch
this � Khan Academy video tutorial.

Out [3]: <class 'float'>

In [4]: 1 phase = 12.5+1.5j


2

3 print(type(phase))

Out [4]: <class 'complex'>

In [5]: 1 full_name = 'John Doe'


2

3 print(type(full_name))

Out [5]: <class 'str'>

� R�������
In Python, a variable / value of a certain type may be referred to as an instance of that type. For instance,
an integer value whose type in Python is defined as int is said to be an instance of type int.

D� I� Y������� 2.2.3 �
Determine and display the type for each of these values:
• 32
• 24.3454
• 2.5 + 1.5
• "RNA Polymerase III"
• 0
• .5 - 1

20 Chapter 2. Variables, Types, and Operations


V�������� A�� T����
Variable Types

• 1.3e-5
• 3e5
The result for each value should be represented in the following format:

Value X is an instance of <class 'Y'>

Chapter 2. Variables, Types, and Operations 21


V�������� A�� T����
Variable Types

2.2.2.1 Conversion of types

Why convert types?


It is sometimes necessary to have the values returned by the input() function —

FUN
i.e. the user’s response, in other types. Imagine the following scenario:

We ask our user to enter the total volume of their purified protein, so that we can work out the
amount of assay they need to conduct a specific experiment. To calculate this assay volume
using the volume of the purified protein, we need to perform mathematical calculations based
on the response we receive from our user. It is not possible to perform mathematical operations
on non-numeric values. Therefore, we ought to somehow convert the type from str to a numeric
type.

The possibility of converting from one type to another depends entirely on the value, the
source type, and the target type. For instance; we can convert an instance of type str
(source type) to one of type int (target type) if and only if the source value consists entirely
of numbers and there are no other characters.

� R�������
To convert a variable from one type to another, we use the Type Name of the target type (as described in
table 2.2.2) and treat it as a function.
For instance, to convert a variable to integer, we:
• Look up the Type Name for integer from table 2.2.2
• Then use the type function, whose name is the same as the Type Name) — e.g. int() for

FUN
integers
• Use the function to convert our variable: new_var = int(old_var)

Here is an example of how we convert types in Python:


In [6]: 1 value_a = '12'
2

3 print(value_a, type(value_a))

Out [6]: 12 <class 'str'>

In [7]: 1 value_b = int(value_a)


2

3 print(value_b, type(value_b))

Out [7]: 12 <class 'int'>

If we attempt to convert a variable that contains non-numeric values, a ValueError is


raised:
In [8]: 1 value_a = '12y'
2

3 print(value_a, type(value_a))

Out [8]: 12y <class 'str'>

In [9]: 1 value_b = int(value_a)

22 Chapter 2. Variables, Types, and Operations


V�������� A�� T����
Variable Types

Out [9]: Traceback (most recent call last):


File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12y'

� R�������
In programming, we routinely face errors — also referred to as exceptions, resulting from di�erent mis-
takes. The process of finding and correcting such mistakes in the code is referred to as debugging.

D� I� Y������� 2.2.4 �
We have been given the following snippet wri�en in Python 3:

In [1]: 1 value_a = 3
2 value_b = '2'
3

4 result = value_a + value_b


5

6 print(value_a, '+', value_b, '=', result)

but when the code is executed, we encounter an error message as follows:

Out [1]: Traceback (most recent call last):


File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Debug the snippet so that the correct result is displayed:

3 + 2 = 5

Chapter 2. Variables, Types, and Operations 23


V�������� A�� T����
Variable Types

F����� 2.2.3 Type evaluation against four di�erent responses to the input() function. An input() function always stores the response as a
str value, no ma�er what the user enters.

2.2.2.2 Handling input variables

When we use input() to obtain a value from the user, the results are by default an
FUN

instance of type str. So for the code we explored in relation to figure 2.2.1, we could
assess the input type as follows:
In [10]: 1 name = input('Please enter your name: ')
2

3 print(name, type(name))

If we save this script in a file called script_d.py and run it, the output will be as displayed
in figure 2.2.3.

� R�������
The input() function always returns a value of type str regardless of the user’s response. In other
FUN

words, if a user’s response to an input() request is numeric, Python will not automatically recognise
FUN

it as a numeric type.

We may use type conversion in conjunction with the values returned by the input()
FUN

function:
In [11]: 1 response = input('Please enter a numeric value: ')
2

3 response_numeric = float(response)
4

5 print('response:', response)
6 print('response type:', type(response))
7 print('response_numeric:', response_numeric)
8 print('response_numeric type:', type(response_numeric))

If we save this as script_e.py as run it, we will be directed to enter numeric values. The
first two attempts displayed in figure 2.2.4 demonstrate the results when we enter numeric
values as directed. If, however, we supply a non-numeric response as demonstrated in the
third attempt, a ValueError will be raised.

24 Chapter 2. Variables, Types, and Operations


V�������� A�� T����
Variable scopes

F����� 2.2.4

D� I� Y������� 2.2.5 �
We know that each amino acid in a protein is encoded by a triplet of mRNA nucleotides.
With that in mind, alter the script you wrote for DIY 2.2.2 and use the number of amino acids entered by the user to
calculate the number of mRNA nucleotides.
Display the results in the following format:

ENZYME_NAME is an enzyme with AMINO_ACIDS amino acids and NUCLEOTIDES nucleotides.

where NUCLEOTIDES is the total number of mRNA nucleotides that you calculated.
Note: Multiplication is represented using the asterisk ( ) sign.

Chapter 2. Variables, Types, and Operations 25


V�������� A�� T����
Variable scopes

2.2.3 V������� ������

� Documentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Resolution of names


� Google® . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Variable scopes in Python
� YouTube® . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . � Python scopes for beginners

When defining a variable, we should always consider where in our programme we intent
to use it. The more localised our variables, the better. This is because local variables are
easier to distinguish, and thus reduce the chance of making mistakes — e.g. unintentionally
redefine or alter the value of an existing variable.
To that end, the scope of a variable defines the ability to reference a variable from different
locations in our programmes. The concept of local variables becomes clearer once we
explore functions in programming.

As displayed in figure 2.2.5, the location at or from which a variable can be referenced
depends on the location where the variable is defined.

In essence, there are three general rules to remember in relation variable


scopes in Python:
� P����� ������ ����
I. A variable that is defined in the outer scope, can be accessed or
� Script (part 1) [global] called in the inner scopes, but it cannot be altered implicitly.
a = 2
b = 3 Note Such variables may still be altered using special tech-
� FuncA [local] niques (not discussed).
f1 = a b
II. A variable that is defined in the innermost scopes (local), can only
Reference a, b, f1
Call FuncA() be accessed, called, or altered within the boundaries of the scope in
Alter f1 which it is defined.
� FuncB [local]
III. The inner scopes from which a variable is referenced must them-
x = 5
selves have be contained within the defining scope — e.g. in
Reference a, b, x
Call FuncA(), FuncB() FuncB of figure 2.2.5, we can reference a, b, and x; but not f1.
Alter x This is because the scope of f1 is Script � FuncA, so it can only
� Script (part 2)
be referenced from Script � FuncA � ..., but not Script � ... or
result = a + b Script � FuncB � ....
Reference a, b, result Python is an interpreted language. This means that the Python interpreter
Call FuncA(), FuncB()
Alter a, b, result, FuncA, FuncB goes through the codes that we write line by line, interpreting it to machine
language. It is only then that the commands are processed and executed by
� FuncC [local]
the computer. On that account, a variable (or a function) can be referenced
y = 10
only after its initial definition. That is why, for instance, in Script (part
Reference a, b, result
Call FuncA(), FuncB(), FuncC() 2) of figure 2.2.5, we can reference every variable and function except for
Alter y FuncC, which is declared further down the code hierarchy.
To conform to hierarchal rules and restrictions, it is common amongst pro-
F����� 2.2.5 Variable scopes in Python with respect
to scripts and functions. grammers and is considered a good practice to implement constants, im-
port directives, and functions at the beginning of the code before proceeding to write the
script which uses the aforementioned materials.

Although scope and hierarchy appear at first glance as theoretical concepts in programming,
their implications are entirely practical. The definition of these principles tend to vary from

26 Chapter 2. Variables, Types, and Operations


V�������� A�� T����
Variable scopes

one programming language to another. As such, it is essential to understand these principles


and their implications in relation to any programming language that we are trying to learn.

Chapter 2. Variables, Types, and Operations 27


O���������
Variable scopes

2.3 O���������

Through our experimentations with variable types, we already know that variables may be
subject to different operations.
When assessing type conversions, we also established that the operations we can apply to
each variable depend on the type of that variable. To that end, we learned that although
it is sometimes possible to mix variables from different types to perform an operation —
e.g. multiplying a floating point number with an integer, there are some logical restrictions
in place.
Throughout this section, we will take a closer look into different types of operations in
Python. This will allow us to gain a deeper insight into the concept and familiarise our-
selves with the underlying logic.
To recapitulate on what we have done so far, we start off by reviewing additions — the
most basic of all operations.
Give the variable total_items:
In [1]: 1 total_items = 2
2

3 print(total_items)

Out [1]: 2

We can increment the value of an existing variable by 1 as follows:


In [2]: 1 total_items = total_items + 1
2

3 print(total_items)

Out [2]: 3

Given 2 different variables, each containing a different value; we can perform an opera-
tion on these values and store the result in another variable without altering the original
variables in any way:
In [3]: 1 old_items = 4
2 new_items = 3
3

4 total_items = old_items + new_items


5

6 print(total_items)

Out [3]: 7

We can change the value of an existing variable using the value stored in another variable:
In [4]: 1 new_items = 5
2 total_items = total_items + new_items
3

4 print(total_items)

Out [4]: 12

There is also a shorthand method for applying the operation on an existing variable:

28 Chapter 2. Variables, Types, and Operations


O���������
Mathematical Operations

In [5]: 1 total_items = 2
2 print(total_items)

Out [5]: 2

In [6]: 1 total_items += 1
2

3 print(total_items)

Out [6]: 3

In [7]: 1 new_items = 5
2 total_items += new_items
3

4 print(total_items)

Out [7]: 8

As highlighted in the introduction, different operations may be applied to any variable or


value. Throughout the rest of this section, we will explore the most fundamental operations
in programming, and learn about their implementation in Python.

� R�������
There are 2 general categories of operations in programming, which are used frequently in all program-
ming languages:
• Mathematical operations
• Logical operations

Naturally, we use mathematical operations to perform calculations, and logical operations to perform
tests.

2.3.1 M����������� O���������

Suppose a and b are 2 variables representing integer numbers as follows:


In [1]: 1 a = 17
2 b = 5

Using a and b we can itemise built-in mathematical operations in Python as follows:

� R�������
As far as mathematical operations are concerned, variables a and b may be an instance of any numeric
type. See table 2.2.2 to find out more about numeric types in Python.
Values of type int have been chosen in our examples to facilitate the understanding of the results.

Chapter 2. Variables, Types, and Operations 29


O���������
Mathematical Operations

O�������� N������� R�����

Addition a + b 22
Subtraction a - b 12
Multiplication a b 85
Division a / b 3.4
Floor quotient a // b 3
Remainder a % b 2
�otient and remainder divmod(a, b) (3, 2)
Power a b 1419857
Absolute value abs(b - a) 12

T���� 2.1 Routine mathematical operations in Python.

D� I� Y������� 2.3.1 �

1. Calculate the following and store the results in appropriately named variables:
a. 5.8 × 3.3
180
b. 6
c. 35 − 3.0
d. 35 − 3
e. 21000
Display the result of each calculation – including the type, in the following format:

Result: X is an instance of <class 'Y'>

2. Now using the results you obtained:


I. Can you explain why is the result of 35 − 3.0 is an instance of type float, whilst that of 35 − 3 is of type int?
II. Unlike the numeric types, string values have a length. To obtain the length of a string value, we use len() .

FUN
Convert the result for 21000 from int to str, then use the aforementioned function to work out the length of
the number — i.e. how many digits is it made of?
If you feel adventurous, you can try this for 210000 or higher; but beware that you might overwhelm your computer and need
a restart it if you go too far (i.e. above 21000000 ). Just make sure you save everything beforehand, so you don’t accidentally
step on your own foot.
Hint: We discuss len() in subsection 4.1.6.2. However, at this point, you should be able to use the o�icial
FUN

documentations and StackOverflow to work out how it works.

2.3.1.1 Shorthands

� I���������� ���� When it comes to mathematical operations in Python, there is a frequently used shorthand
As of Python 3.6, you can use an underscores method that every Python programmer should be familiar with.
( _ ) within large numbers as a separator to
make them easier to read in your code. For Suppose we have a variable defined as total_residues = 52 and want to perform a math-
instance, instead of x = 1000000, you can
write x = 1_000_000.
ematical operation on it. However, we would like to store the result of that operation in
total_residues instead of a new variable. In such cases, we can do as follows:
In [2]: 1 total_residues = 52

In [3]: 1 # Addition:
2 total_residues += 8
3

30 Chapter 2. Variables, Types, and Operations


O���������
Mathematical Operations

4 print(total_residues)

Out [3]: 60

In [4]: 1 # Subtraction:
2 total_residues -= 10
3

4 print(total_residues)

Out [4]: 50

In [5]: 1 # Multiplication:
2 total_residues = 2
3

4 print(total_residues)

Out [5]: 100

In [6]: 1 # Division:
2 total_residues /= 4
3

4 print(total_residues)

Out [6]: 25

In [7]: 1 # Floor quotient:


2 total_residues //= 2
3

4 print(total_residues)

Out [7]: 12

In [8]: 1 # Remainder:
2 total_residues %= 5
3

4 print(total_residues)

Out [8]: 2

In [9]: 1 # Power:
2 total_residues = 3
3

4 print(total_residues)

Out [9]: 8

We can also perform such operations using multiple variables:


In [10]: 1 total_residues = 52
2 new_residues = 8
3 number_of_proteins = 3
4

5 total_residues += new_residues
6

7 print(total_residues)

Chapter 2. Variables, Types, and Operations 31


O���������
Mathematical Operations

Out [10]: 60

In [11]: 1 total_residues += (number_of_proteins new_residues)


2

3 print(total_residues)

Out [11]: 84

D� I� Y������� 2.3.2 �

1. Given:
• Circumference: C = 18.84956
• Radius: R = 3
and considering that the properties of a circle are defined as follows:

R
D
O

calculate p using the following equation and store it in a variable named pi:

C
p=
D
Then round the results to 5 decimal places and display the result in the following format:

The value of pi calculated to 5 decimal places: X.XXXXX

Note To round floating point numbers in Python, we use round() . This is a built-in function that
FUN

takes 2 input arguments: the first is the variable/value to be rounded, and the second is the number
decimal places. Read more about round() in the � o�icial documentations.
FUN

2. Now without creating a new variable, perform the following operation:

pi
pi =
(3 mod 2) − 1
where the expression “3 mod 2” represents the remainder for the division of 3 by 2.

Explain the output.

� R�������
Always leave a single space between the operators (e.g. +, /, ) and the operands (e.g. 3, 4.5, 2.1j, or
any variable). It is a good practice to do so, and makes your code more legible.

32 Chapter 2. Variables, Types, and Operations


O���������
Mathematical Operations

2.3.1.2 Precedence

In mathematics and computer programming, there is a collection of conventional rules on


the precedence of procedures to evaluate a mathematical expression. This collection of
rules is referred to as the order of operation or operator precedence. This is an important
topic in programming, and one that is the root many headaches for programmers who get
them wrong.
Suppose we have a mathematical expression as follows:

x = 2+3×9

Such an expression can only be evaluated correctly if we do the multiplication first and then
perform the addition. This means that the evaluation is done as follows:

given ∶ 3 × 9 = 27
⇒ x = 2 + 27
= 29

� R�������
Operator precedence in mathematical operations may be described as follows:
1. Exponents and roots
2. Multiplication and division
3. Addition and subtraction

If there are any parenthesis () in the expression, the expression is evaluated from the innermost paren-
thesis outwards.

For instance, in an expression such as:

x = 2 × �3 + (5 − 1)2 �

the evaluation workflow may be described as follows:

x = 2 × (3 + 42 )
= 2 × (3 + 16)
= 2 × 19
= 38

The same principle applies in Python. This means that if we use Python to evaluate the
above expression, the result would be identical:
In [12]: 1 result = 2 (3 + (5 - 1) 2)
2

3 print(result)

Out [12]: 38

Chapter 2. Variables, Types, and Operations 33


O���������
Mathematical Operations

D� I� Y������� 2.3.3 �
Display the result of each item in the following format:

EXPRESSION = RESULT

For example:

2 + 3 = 5

1. Calculate each expression without using parentheses:


a. 3 × 24
b. 5 + 3 × 24
c. 3 × 24 + 5
d. 2
4
×3
2. Calculate these expressions using parentheses:
a. 5 + 24 × 3
b. 5 + 2×3
4

c. 5 + 4×3
2

3. Given
1 a = 2
2 b = 5

use a and b to calculate the following expressions:


a. (a + b)2
b. a2 + 2ab + b2

� R�������
Where the precedence of operations in an expression are identical, the expression is evaluated from le�
to right.
For instance, consider the following statement:

In [13]: 1 result = 3 / 0.5 (1 + 2)

The highest precedence in the above statement belongs to the part wri�en in parenthesis. Therefore, the
expression is initially simplified to result = 3 / 0.5 3.
At this point, we are le� with a division and a multiplication. We know that both of these operations enjoy
the same level of precedence. So at this point, the result of the expression is calculated by moving from
the le�most operation to the rightmost one — i.e. result = (3 / 0.5) 3.

In [14]: 1 print(result)

Out [14]: 18.0

Note In mathematics and programming, it is considered a bad practice to write expressions


such as the one in this example: they are ambiguous, di�icult to read, and prone to mis-
takes. They should be avioded as much as possible. To that end, we ought to right such
expressions such as follows:

34 Chapter 2. Variables, Types, and Operations


O���������
Mathematical Operations

In [15]: 1 result = (3 / 0.5) (1 + 2)


2

3 print(result)

Out [15]: 18.0

Ultimately, the subject of precedence in programming may be summarised as follows:

1. Expressions inside parenthesis.

2. Exponents and roots

3. Multiplication and division

4. Addition and subtraction

5. Where the precedence of operations in an expression are identical, the expression is


evaluated from left to right.

These principles may be also be visualised as follows:

= 2057
Precedence
6th
= 2058

5th
=6
4th = 343

3rd
=7
2nd
=6
1st
result = 3 / 0.5 (1 + 2 3) 3 - 1

In [16]: 1 result = 3 / 0.5 (1 + 2 3) 3 - 1


2

3 print(result)

Out [16]: 2057.0

2.3.1.3 Non-numeric values

It sometimes makes sense to apply some mathematical operations to non-numeric variables


too.
We can multiply strings to repeat them. There is no specific advantage to the use of mul-
tiplication instead of manually repeating characters or words, but it makes our code look
cleaner, and that’s always a good thing!
We can also add string values to each other. This is called string concatenation. It is a
useful method for concatenating a few strings and / or string variables.
In [17]: 1 SEPARATOR = '-' 20
2 NEW_LINE = '\n'
3 SPACE = ' '

Chapter 2. Variables, Types, and Operations 35


O���������
Mathematical Operations

5 forename = 'Jane'
6 surname = 'Doe'
7 birthday = '01/01/1990'
8

9 full_name = forename + SPACE + surname


10

11 data = full_name + NEW_LINE + SEPARATOR + NEW_LINE + 'DoB: ' + birthday


12

13 print(data)

Out [17]: Jane Doe


--------------------
DoB: 01/01/1990

� R�������
New line character or '\n' is a universal directive to induce a line-break in Unix based operating systems
(Mac OS X® and Linux). In Microso�® Windows® , we usually use '\r' or '\r\n' instead. These are
known as Escape Sequences, which we explore in more depath under String Operations in chapter 7.

D� I� Y������� 2.3.4 �
The risk of Huntington’s disease appears to increase proportional to the continuous repetition of CAG nucleotides
(glutamine codon) once they exceed 35 near the beginning of the Huntingtin (IT15) gene. The CAG repeats are also
referred to as a polyglutamine or polyQ tract.
Given:

1 glutamine_codon = 'CAG'

1. Create a polynucleotide chain representing 36 glutamine codons. Store the result in a variable called polyq_codons.
Display the result as:

Polyglutamine codons with 36 repeats: XXXXXXXXX...

2. Use len() to work out the length of polyq_codons, and store the result in a variable called polyq_codons_length.
FUN

Display the result in the following format:

Number of nucleotides in a polyglutamine with 36 repeats: XXX

3. Use len() to work out the length of glutamin_codon, and store the result in variable amino_acids_per_codon.
FUN

4. Divide polyq_codons_length by amino_acids_per_codon to prove that the chain contains the codon for exactly
36 amino acids. Store the result in variable polyq_peptide_length.
Display the result in the following format:

Number of amino acids in a polyglutamine with 36 repeats: XXX

5. Determine the types for the following variable:


• amino_acids_per_codon
• polyq_codons_length
• polyq_peptide_length
and display the result for each item in the following format:

Value: XXX - Type: <class 'XXXX'>

6. Are all the variables in task #5 of the same type? why?


7. Repeat from task #4, but this time use an alternative method of division as outlined in table 2.1.

36 Chapter 2. Variables, Types, and Operations


O���������
Logical Operations

2.3.2 L������ O���������

An operation may involve a comparison. The result of such operations is either True or
False. This is known as the Boolean or bool data type. In reality, however, computers � I���������� ����
record True and False as 1 and 0 respectively. The Boolean data type is named a�er the
English mathematician and logician George
Operations with Boolean results are referred to as logical operations. Testing the results of Boole (1815–1864).
such operations is known as truth value testing.
Given the two variables a and b as follows:
In [1]: 1 a = 17
2 b = 5

Boolean operations may be defined as demonstrated in table 2.2.

T���� 2.2 Routine logical operations in Python.

L���� S�������� R�����

Equivalence a == b False
Non-equivalence a != b True
Greater a > b True
Either greater or equal a >= b True
Smaller a < b False
Either smaller or equal a <= b False
Between 5 < a < 20 True

D� I� Y������� 2.3.5 �
We know that in algebra, the first identity (square of a binomial) is:

(a + b)2 = a2 + 2ab + b2

now given:

1 a = 15
2 b = 4

1. Calculate
• y1 = (a + b)2
• y2 = a2 + 2ab + b2
Display the results in the following format:

y1 = XX
y2 = XX

2. Determine whether or not y_1 is indeed equal to y_2. Store the result of your test in another variable called
equivalence. Display the results in the following format:

Where a = XX and b = XX:


y1 is equal to y2: [True/False]

Chapter 2. Variables, Types, and Operations 37


O���������
Logical Operations

2.3.2.1 Negation

We can also use negation in logical operations. Negation means that the reverse of the
statement is to be considered as True. So given:
In [2]: 1 value = 2
2

3 result = value == 2
4

5 print(result)

Out [2]: True

The negated version of value == 2 would behave as follows:


In [3]: 1 negated_result = not value == 2
2

3 print(negated_result)

Out [3]: False

As you can see from the code, negation in Python is implemented using not :

SYN
L���� S�������� R�����

Not equal not a == b True


Not greater not a > b False
Neither greater nor equal not a >= b False
Smaller not a < b True
Neither smaller nor equal not a <= b True

D� I� Y������� 2.3.6 �
Using the information from DIY 2.3.5:
1. Without using not , determine whether or not y_1 is not equal to y_2. Display the result of your test and store
SYN

it in another variable called inequivalent.


2. Negate inequivalent and display the result.

2.3.2.2 Disjunctions and Conjunctions

Logical operations may be combined using conjunction with and and disjunction with
SYN

or to create more complex logics:


SYN

D� I� Y������� 2.3.7 �
Given

In [1]: 1 a = True
2 b = False
3 c = True

38 Chapter 2. Variables, Types, and Operations


O���������
Logical Operations

L���� S�������� R�����

Either smaller or equal a < b or a == b False


Either greater or smaller a < b or a > b True
Either greater or equal a > b or a == b True
Greater but not greater than a > b and not a > 18 True
Either equal or not between a == b or not b < a < 20 False
Equal and between a == b and 5 <= a < 20 False

Evaluate the following statements:

1. a == b
2. a == c
3. a or b
4. a and b
5. a or b and c
6. (a or b) and c
7. not a or (b and c)
8. not a or not(b and c)
9. not a and not(b and c)
10. not a and not(b or c)

Display the results in the following format:

1. [True/False]
2. [True/False]
...

Chapter 2. Variables, Types, and Operations 39


O���������
Logical Operations

2.3.2.3 Complex logical operations

It may help to break down more complex operations, or use parenthesis to make them easier
to both read and write:

L���� S�������� R�����

Complex logic a == b or (5 <= a or b < 20 and a < b) True


Complex logic a == b or (5 <= a or b < 20) and a < b False
Complex logic a == b or 5 <= a or (b < 20 and a < b) True

Notice that in the last example, all notations are essentially the same and only vary in terms
of their collective results as defined using parenthesis. Always remember that in a logical
statement:
• The statement in parenthesis does not have precedence over the rest of the state (un-
like mathematical statements). It merely defines an independent part of the operation
whose response is evaluated separately.

• The precedence is established on a first come, first serve basis (from left to right).

• Always use parenthesis in longer statements for clarification.

• In disjunctive statements — i.e. a > 5 or b > 5, if the first part is True, the second
part is not checked. In other words, if a is greater than 5, the computer does not
proceed to check whether or not b is greater than 5.

• In conjunctive statements — i.e. a > 5 and b > 5, the statement proceeds to the
seconds part if and only if the first part is True. In other words, the result of a
conjunctive statement is only True if and only if both a and b are greater than 5. If a
is False, the entire statement will inevitably be False.

• The longer the statement, the more difficult it would be to understand it properly, and
by extension, the more likely it would be to cause problems.
In [4]: 1 a, b, c = 17, 5, 2 # Alternative method to define variables.

In [5]: 1 # Disjunction: false OR true.


2 a < b or b > c

Out [5]: True

In [6]: 1 # Disjunction: true OR true.


2 a > b or b > c

Out [6]: True

In [7]: 1 # Conjunction: true AND true.


2 a > b and b > c

Out [7]: True

In [8]: 1 # Conjunction: false and true.


2 a < b and b > c

Out [8]: False

40 Chapter 2. Variables, Types, and Operations


O���������
Logical Operations

In [9]: 1 # Disjunction and conjunction: true OR false AND true


2 a > b or b < c and b < a

Out [9]: True

In [10]: 1 # Disjunction and conjunction: false OR true AND false


2 a < b or b > c and b > a

Out [10]: False

In [11]: 1 # Disjunctions and conjunction: false OR true AND true


2 a < b or b > c and b < a

Out [11]: True

In [12]: 1 # Disjunction and negated conjunction and conjunction:


2 # true AND NOT false AND false
3 a < b or not b < c and b > a

Out [12]: False

In [13]: 1 # Disjunction and negated conjunction - similar to the


2 # previous example: true AND NOT (false AND false)
3 a < b or not (b < c and b > a)

Out [13]: True

These are only a few examples. There are endless possibilities, try them yourself and see
how they work.

� R�������
Some logical operations may be wri�en in di�erent ways. However, we should always use the notation
that is most coherent in the context of our code. If in doubt, use the simplest / shortest notation.

To that end, you may want to use variables to split complex statements down to smaller
portions:
In [14]: 1 age_a, age_b = 15, 35
2

3 are_positive = age_a > 0 and age_b > 0


4

5 a_is_older = are_positive and (age_a > age_b)


6 b_is_older = are_positive and (age_a < age_b)
7

8 a_is_teenager = are_positive and 12 < age_a < 20


9 b_is_teenager = are_positive and 12 < age_b < 20
10

11 a_is_teenager and b_is_older

Out [14]: True

In [15]: 1 a_is_teenager and a_is_older

Chapter 2. Variables, Types, and Operations 41


O���������
Logical Operations

Out [15]: False

In [16]: 1 a_is_teenager and (b_is_teenager or b_is_older)

Out [16]: True

D� I� Y������� 2.3.8 �
Given

In [1]: 1 a = 3
2 b = 13

Test the following statements and display the results:

• a2 < b
• 3 − a3 < b
• �25 − a2 � > b
• 25 mod a2 > b
• 25 mod a2 > b or 25 mod b < a
• 25 mod a2 < b and 25 mod b > a
• 12
a
and a × 4 < b

where “|. . . |” represents the absolute value, and “n mod m” represents the remainder for the division of n by m.

Display the results in the following format:

1. [True/False]
2. [True/False]
...

42 Chapter 2. Variables, Types, and Operations


E�� �� �������
E��������
1. Write and execute a Python script to display your own name as an output in the terminal.
2. Write and execute a Python script that:
• Displays the text Please press enter to continue..., and waits for the user to press
enter.
• Once the user pressed enter, the program should display Welcome to my programme!
before it terminates.
3. We have an enzyme whose reaction velocity is v = 50 mol ⋅L−1 ⋅s−1 at the substrate concentration
of [S] = Km = 2.5 mol ⋅ L−1 . Work out the maximum reaction velocity or Vmax for this enzyme
using the Michaelis-Menten equation:

Vmax [S]
v=
Km + [S]

Chapter 2. Variables, Types, and Operations 43

You might also like