|
2 | 2 | title: Reference |
3 | 3 | --- |
4 | 4 |
|
5 | | -## Glossary |
| 5 | +## Python syntax primer |
6 | 6 |
|
7 | | -This is a placeholder file. Please add content here. |
| 7 | +As this is a course on image analysis, some prior knowledge of Python syntax is assumed. |
| 8 | +This reference provides a summary of the language constructs used in the examples and |
| 9 | +exercises of this course. |
| 10 | + |
| 11 | +### Variables |
| 12 | + |
| 13 | +This is how you can store data to retrieve later: |
| 14 | + |
| 15 | +```python |
| 16 | +# assigning the integer 1 to the variable `a`, and the decimal 0.4 to |
| 17 | +# the variable `b` |
| 18 | +a = 1 |
| 19 | +b = 0.4 |
| 20 | + |
| 21 | +# we can perform arithmetic on numbers |
| 22 | +c = a + 4 |
| 23 | + |
| 24 | +# even reassign a variable based on its previous value |
| 25 | +b = b * 2 |
| 26 | + |
| 27 | +# strings/text can be specified with single quotes or double quotes |
| 28 | +some_string = 'a b c d e' |
| 29 | + |
| 30 | +# we can still use the `+` operator on strings, except instead this |
| 31 | +# will concatenate them together |
| 32 | +some_other_string = some_string + 'f g h i' |
| 33 | + |
| 34 | +# Python also has the boolean values True and False |
| 35 | +one_is_greater_than_zero = True |
| 36 | +one_is_an_even_number = False |
| 37 | +``` |
| 38 | + |
| 39 | +### Functions |
| 40 | + |
| 41 | +These may be built into base Python, the standard library, extra installed packages, or ones that you've made |
| 42 | +yourself. A function is always run with brackets. |
| 43 | + |
| 44 | +```python |
| 45 | +# show something on the console |
| 46 | +x = 2 |
| 47 | +print(x) |
| 48 | + |
| 49 | +# you don't have to create a variable first - you can just print a value directly |
| 50 | +print('abc') |
| 51 | +print(2) |
| 52 | + |
| 53 | +# multiple arguments are separated with commas |
| 54 | +print(1, 2, 3) |
| 55 | + |
| 56 | +# some functions are written to take named arguments - check the function's documentation |
| 57 | +# to see what arguments you can give it |
| 58 | +print(1, 2, 3, sep='-') |
| 59 | + |
| 60 | +# we still need brackets to run the function, even if they're empty |
| 61 | +credits() |
| 62 | + |
| 63 | +# the number 1 and the string '1' are not the same - Python has function for converting |
| 64 | +# or 'casting' values to certain data types |
| 65 | +string_1 = str(1) # str -> cast to string |
| 66 | +integer_4 = int(4.1) # int -> cast to integer |
| 67 | +float_3_point_0 = float(3) # float -> cast to decimal |
| 68 | +``` |
| 69 | + |
| 70 | +### Importing packages |
| 71 | + |
| 72 | +Python comes with a lot of extra functionality that it not loaded by default. Some of these may be pre-installed (i.e. they are |
| 73 | +part of the Python [standard library](https://docs.python.org/3/library/index.html)), or they may need to be installed with `pip`. |
| 74 | + |
| 75 | +```python |
| 76 | +# load the entire package - access its functions with dot notation |
| 77 | +import skimage |
| 78 | +image = skimage.io.imread('some_image.tif') |
| 79 | + |
| 80 | +# some components inside packages are not functions, but regular |
| 81 | +# strings, numbers, etc. - we still access them with dot notation but |
| 82 | +# do not need `()` brackets to use them |
| 83 | +import sys |
| 84 | +print(sys.executable) |
| 85 | + |
| 86 | +# import just the bits we need |
| 87 | +from skimage.io import imread |
| 88 | +image = imread('some_image.tif') |
| 89 | + |
| 90 | +# rename the package as you're importing it |
| 91 | +import matplotlib.pyplot as plt |
| 92 | +plt.imshow(image) |
| 93 | +``` |
| 94 | + |
| 95 | +### Lists |
| 96 | + |
| 97 | +Python can use these to store many pieces of information together: |
| 98 | + |
| 99 | +```python |
| 100 | +# use square brackets to define a list, and separate the items with commas |
| 101 | +some_list = ['a', 'b', 'c', 'd', 'e'] |
| 102 | + |
| 103 | +# access individual items with square brackets - note that Python counts from 0 |
| 104 | +print(some_list[0]) |
| 105 | +print(some_list[3]) |
| 106 | + |
| 107 | +# access a subset (slice) of a list with square brackets and colons - this will |
| 108 | +# start at position 1 and go up to, but not including, position 3 |
| 109 | +print(some_list[1:3]) |
| 110 | + |
| 111 | +# negative numbers will count from the end of the list rather than the start |
| 112 | +print(some_list[2:-1]) |
| 113 | + |
| 114 | +# leaving out the first number is equivalent to starting at position 0 (i.e. the |
| 115 | +# start), and leaving out the second number is equivalent to ending at position -1 |
| 116 | +# (i.e. the end) |
| 117 | +print(some_list[:3]) |
| 118 | +print(some_list[1:]) |
| 119 | + |
| 120 | +# leaving out both numbers will just slice the whole list |
| 121 | +print(some_list[:]) |
| 122 | + |
| 123 | +# slicing and indexing also works with strings |
| 124 | +some_string = 'abcdefgh' |
| 125 | +print(some_string[2:5]) |
| 126 | + |
| 127 | +# images are designed to be indexed in multiple dimensions at once - do this |
| 128 | +# by specifying multiple indexes/slices, separated by commas. Here we load an |
| 129 | +# image and access all of dimension 1, positions 100-200 of dimension 2, and |
| 130 | +# just position 2 of dimension 3 |
| 131 | +from skimage.io import imread |
| 132 | +image = imread('some_image.tif') |
| 133 | +print(image[:, 100:200, 2]) |
| 134 | +``` |
| 135 | + |
| 136 | +### Dictionaries |
| 137 | + |
| 138 | +These store many named pieces of information together as key-value pairs: |
| 139 | + |
| 140 | +```python |
| 141 | +# use curly brackets to define a dictionary. Separate the key from the value |
| 142 | +# with a colon, and separate key-value pairs from each other with commas |
| 143 | +chromosome_counts = {'human': 46, 'wheat': 42, 'fruit fly': 8, 'zebrafish': 25} |
| 144 | + |
| 145 | +# access individual values with square brackets |
| 146 | +print(chromosome_counts['fruit fly']) |
| 147 | +``` |
| 148 | + |
| 149 | +### Loops |
| 150 | + |
| 151 | +This is how we can perform some actions on many pieces of information, one at a time: |
| 152 | + |
| 153 | +```python |
| 154 | +# first we need something to loop over |
| 155 | +colours = ['orange', 'red', 'blue', 'green', 'yellow', 'purple'] |
| 156 | + |
| 157 | +# go through each item the `colours` list, one at a time. The item |
| 158 | +# that we're currently processing will be called `colour` |
| 159 | +for colour in colours: |
| 160 | + # the code inside the loop is indented with spaces or tabs - it doesn't |
| 161 | + # matter which one or how many, as long as you're consistent |
| 162 | + print('doing something with: ' + colour) |
| 163 | + |
| 164 | +# we're no longer indented, so we're now outside the loop |
| 165 | +print('done') |
| 166 | + |
| 167 | +# this will do the same thing as the loop above, but with much more |
| 168 | +# typing and repetition |
| 169 | +print('doing something with: ' + 'orange') |
| 170 | +print('doing something with: ' + 'red') |
| 171 | +print('doing something with: ' + 'blue') |
| 172 | +print('doing something with: ' + 'green') |
| 173 | +print('doing something with: ' + 'yellow') |
| 174 | +print('doing something with: ' + 'purple') |
| 175 | +``` |
| 176 | + |
| 177 | +### Conditional statements |
| 178 | + |
| 179 | +Python can execute blocks of code only when certain conditions are met: |
| 180 | + |
| 181 | +```python |
| 182 | +# use the keyword `if`, followed by a boolean expression. Common mathematical |
| 183 | +# operators can be used in Python, including ==, !=, >, <, >=, <= |
| 184 | +if 1 < 2: |
| 185 | + # 1 is less than 2, so this indented code will run |
| 186 | + print('1 is less than 2') |
| 187 | + |
| 188 | +if 1 + 2 == 5: |
| 189 | + # 1 + 2 is not 5, so this will not run |
| 190 | + print('1 + 2 is 5') |
| 191 | + |
| 192 | +# we can use `else` to provide some code to fall back on if the condition |
| 193 | +# is not met |
| 194 | +if 1 + 2 == 5: |
| 195 | + print('1 + 2 is 5') |
| 196 | +else: |
| 197 | + print('1 + 2 is not 5') |
| 198 | + |
| 199 | +# use `elif` to specify multiple possible courses of action - only the first one to |
| 200 | +# be satisfied will be executed |
| 201 | + |
| 202 | +# let's check how large each number in a list is |
| 203 | +values = [5, 0, 2, 150, -1] |
| 204 | + |
| 205 | +for val in values: |
| 206 | + if val > 100: |
| 207 | + print(str(val) + ' is greater than 100') |
| 208 | + |
| 209 | + elif val > 4: |
| 210 | + print(str(val) + 'is greater than 4') |
| 211 | + |
| 212 | + elif val >= 0: |
| 213 | + print(str(val) + ' is greater than or equal to 0') |
| 214 | + |
| 215 | + else: |
| 216 | + print(str(val) + ' is less than 0') |
| 217 | +``` |
0 commit comments