|
| 1 | +--- |
| 2 | +title: Python Basics Recap |
| 3 | +teaching: 0 |
| 4 | +exercises: 0 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Understanding key concepts in Python |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 14 | + |
| 15 | +- Python Refresher |
| 16 | + |
| 17 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 18 | + |
| 19 | +This course follows on from the python introduction course. To ensure that we are starting from similar positions, there follows a short multiple choice quiz on key python concepts that we will be building on through this course. |
| 20 | + |
| 21 | +## Variables and Lists |
| 22 | + |
| 23 | +::::::::::::::::::::::::::::::::::::: challenge |
| 24 | + |
| 25 | +### 1\. Assigning a value to a variable |
| 26 | + |
| 27 | +We wish to store the string `Cat` as a value in the variable `animal`, which of these lines of code will do this for us? |
| 28 | + |
| 29 | +1. `animal = 'Cat'` |
| 30 | +2. `animal = Cat` |
| 31 | +3. `Cat = animal` |
| 32 | +4. `animal(Cat)` |
| 33 | + |
| 34 | +::::::::::::::: solution |
| 35 | + |
| 36 | +Answer 1 is correct |
| 37 | + |
| 38 | +::::::::::::::::::::::::: |
| 39 | + |
| 40 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 41 | + |
| 42 | +::::::::::::::::::::::::::::::::::::: challenge |
| 43 | + |
| 44 | +### 2\. Assigning values to a list |
| 45 | + |
| 46 | +We wish to create a list of values, which of these lines of code is valid to do this? |
| 47 | + |
| 48 | +1. `varlist = [34, 57, '2d']` |
| 49 | +2. `varlist = (12, 'vr', 95)` |
| 50 | +3. `varlist = 'xcf', 12, 97` |
| 51 | + |
| 52 | +::::::::::::::: solution |
| 53 | + |
| 54 | +Answer 1 is correct |
| 55 | + |
| 56 | +::::::::::::::::::::::::: |
| 57 | + |
| 58 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 59 | + |
| 60 | +::::::::::::::::::::::::::::::::::::: challenge |
| 61 | + |
| 62 | +### 3a. Indexing characters in a string |
| 63 | + |
| 64 | +Lists and strings both contain multiple indexed values (in the case of strings these are specifically individual characters rather than other values). If we have a variable `animal` which contains the string `penguin`, which of these options will print the first character (`p`) for us? |
| 65 | + |
| 66 | +1. `print(animal[0])` |
| 67 | +2. `print(animal[1])` |
| 68 | +3. `print(animal['p'])` |
| 69 | + |
| 70 | +::::::::::::::: solution |
| 71 | + |
| 72 | +Answer 1 is correct |
| 73 | + |
| 74 | +::::::::::::::::::::::::: |
| 75 | + |
| 76 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 77 | + |
| 78 | +::::::::::::::::::::::::::::::::::::: challenge |
| 79 | + |
| 80 | +### 3b. Indexing characters in a string (slicing) |
| 81 | + |
| 82 | +We can also select whole sections of lists and strings, not just single elements. Using the same variable `animal`, containing the string `penguin`, as above, which of these options will print the last 2 characters for us? (Note that there is more an one correct answer) |
| 83 | + |
| 84 | +1. `print(animal[5:])` |
| 85 | +2. `print(animal[6:])` |
| 86 | +3. `print(animal[6:7])` |
| 87 | +4. `print(animal[5:7])` |
| 88 | +5. `print(animal[-2:])` |
| 89 | +6. `print(animal[:-2])` |
| 90 | + |
| 91 | +::::::::::::::: solution |
| 92 | + |
| 93 | +Answers 1, 4, and 5 are correct |
| 94 | + |
| 95 | +::::::::::::::::::::::::: |
| 96 | + |
| 97 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 98 | + |
| 99 | +## Loops |
| 100 | + |
| 101 | +::::::::::::::::::::::::::::::::::::: challenge |
| 102 | + |
| 103 | +### 4\. Constructing a `for` loop |
| 104 | + |
| 105 | +Please write a simple `for` loop which will print out each of the characters in the `animal` variable one at a time. |
| 106 | + |
| 107 | +::::::::::::::: solution |
| 108 | + |
| 109 | +```python |
| 110 | +for char in animal: |
| 111 | + print(char) |
| 112 | +``` |
| 113 | + |
| 114 | +::::::::::::::::::::::::: |
| 115 | + |
| 116 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 117 | + |
| 118 | +## Software Modules |
| 119 | + |
| 120 | +::::::::::::::::::::::::::::::::::::: challenge |
| 121 | + |
| 122 | +### 5\. Loading new functions |
| 123 | + |
| 124 | +We want to use the functions in the `numpy` library in our code. How do we open this library in our code? |
| 125 | + |
| 126 | +1. `import numpy` |
| 127 | +2. `load numpy` |
| 128 | +3. `open numpy` |
| 129 | + |
| 130 | +::::::::::::::: solution |
| 131 | + |
| 132 | +Answer 1 is correct |
| 133 | + |
| 134 | +::::::::::::::::::::::::: |
| 135 | + |
| 136 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 137 | + |
| 138 | +## If statements and conditionals |
| 139 | + |
| 140 | +::::::::::::::::::::::::::::::::::::: challenge |
| 141 | + |
| 142 | +### 6\. Conditionals |
| 143 | + |
| 144 | +Which of these conditional tests returns a `True` result? |
| 145 | + |
| 146 | +1. `4 > 3` |
| 147 | +2. `'a' != 'b'` |
| 148 | +3. `6 >= 6.0` |
| 149 | +4. `'3' == 3` |
| 150 | +5. `3 > 'c'` |
| 151 | + |
| 152 | +::::::::::::::: solution |
| 153 | + |
| 154 | +Answers 1, 2, and 3 return `True` results. 4 returns `False`. 5 raises an Error. |
| 155 | + |
| 156 | +::::::::::::::::::::::::: |
| 157 | + |
| 158 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 159 | + |
| 160 | +::::::::::::::::::::::::::::::::::::: challenge |
| 161 | + |
| 162 | +### 7\. If statements |
| 163 | + |
| 164 | +What is printed when this `if` statement is used? |
| 165 | + |
| 166 | +```python |
| 167 | +if 4 > 5: |
| 168 | + print('A') |
| 169 | +elif 4 <= 5: |
| 170 | + print('B') |
| 171 | +elif 4 < 5: |
| 172 | + print('C') |
| 173 | +else: |
| 174 | + print('D') |
| 175 | +``` |
| 176 | + |
| 177 | +1. `A` |
| 178 | +2. `B` |
| 179 | +3. `C` |
| 180 | +4. `B` and `C` |
| 181 | +5. `D` |
| 182 | +6. `A` and `D` |
| 183 | + |
| 184 | +::::::::::::::: solution |
| 185 | + |
| 186 | +`B`. Both `4 <= 5` and `4 < 5` would return a `True` result, but the `if` statement is exited as soon as the `True` result is returned. |
| 187 | + |
| 188 | +::::::::::::::::::::::::: |
| 189 | + |
| 190 | +::::::::::::::::::::::::::::::::::::::::::::::: |
| 191 | + |
| 192 | +## Generative AI |
| 193 | + |
| 194 | +We would like to reiterate the [Carpentries](https://carpentries.org/) stance on [Generative AI in coding](https://swcarpentry.github.io/python-novice-inflammation/01-intro.html#generative-ai) which you may have seen when preparing for this course. |
| 195 | + |
| 196 | +To summarise the position, the recommendation is that you **avoid** getting help from generative AI during the workshop for several reasons: |
| 197 | + |
| 198 | + - For most problems you will encounter at this stage, help and answers can be found among the first results returned by searching the internet or looking on e.g. [StackOverflow](https://stackoverflow.com/questions). |
| 199 | + - The foundational knowledge and skills you will learn in this lesson by writing and fixing your own programs are essential to be able to evaluate the correctness and safety of any code you receive from online help or a generative AI chatbot. If you choose to use these tools in the future, the expertise you gain from learning and practising these fundamentals on your own will help you use them more effectively. |
| 200 | + - As you continue programming, the mistakes you make will be the kinds that have also been made – and overcome! – by everybody else who learned to program before you. Since these mistakes and the questions you are likely to have at this stage are common, they are also better represented than other, more specialised problems and tasks in the data that was used to train generative AI tools. This means that a generative AI chatbot is more likely to produce accurate responses to questions that novices ask, which could give you a false impression of how reliable they will be when you are ready to do things that are more advanced. |
| 201 | + |
| 202 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 203 | + |
| 204 | +- variables |
| 205 | +- lists |
| 206 | +- indexing |
| 207 | +- loops |
| 208 | +- conditionals |
| 209 | + |
| 210 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 211 | + |
| 212 | + |
0 commit comments