|
95 | 95 | "cell_type": "code", |
96 | 96 | "collapsed": false, |
97 | 97 | "input": [ |
98 | | - "x = 3\n", |
| 98 | + "x = -3\n", |
99 | 99 | "\n", |
100 | 100 | "if x > 0:\n", |
101 | 101 | " print \"Value is positive\"\n", |
|
157 | 157 | "collapsed": false, |
158 | 158 | "input": [ |
159 | 159 | "gene = \"BRCA2\"\n", |
160 | | - "geneExpression = 1.2\n", |
| 160 | + "geneExpression = -1.2\n", |
161 | 161 | "\n", |
162 | 162 | "if geneExpression < 0:\n", |
163 | 163 | " print gene, \"is downregulated\"\n", |
|
166 | 166 | " print gene, \"is upregulated\"\n", |
167 | 167 | " \n", |
168 | 168 | "else:\n", |
169 | | - " pass # Placeholder - do nothing" |
| 169 | + " pass\n" |
170 | 170 | ], |
171 | 171 | "language": "python", |
172 | 172 | "metadata": {}, |
|
184 | 184 | "cell_type": "code", |
185 | 185 | "collapsed": false, |
186 | 186 | "input": [ |
187 | | - "x = 5\n", |
| 187 | + "x = 11\n", |
188 | 188 | "s = \"Yes\" if x < 10 else \"No\"\n", |
189 | 189 | "print s" |
190 | 190 | ], |
|
226 | 226 | "cell_type": "markdown", |
227 | 227 | "metadata": {}, |
228 | 228 | "source": [ |
229 | | - "It is notable that comparison operations can be combined, for example to check if a valuye is within a range." |
| 229 | + "It is notable that comparison operations can be combined, for example to check if a value is within a range." |
230 | 230 | ] |
231 | 231 | }, |
232 | 232 | { |
233 | 233 | "cell_type": "code", |
234 | 234 | "collapsed": false, |
235 | 235 | "input": [ |
236 | | - "x = 20\n", |
| 236 | + "x = -5\n", |
237 | 237 | "\n", |
238 | | - "if 0 < x < 10:\n", |
| 238 | + "if x > 0 and x < 10:\n", |
239 | 239 | " print \"In range A\"\n", |
240 | 240 | " \n", |
241 | | - "elif -10 < x <= 20:\n", |
| 241 | + "elif x < 0 or x > 10:\n", |
242 | 242 | " print \"In range B\"" |
243 | 243 | ], |
244 | 244 | "language": "python", |
|
259 | 259 | "collapsed": false, |
260 | 260 | "input": [ |
261 | 261 | "x = [123, 54, 92, 87, 33]\n", |
262 | | - "y = x[:] # y is a copy of x\n", |
263 | | - "y == x # True\n", |
264 | | - "y is x # False" |
| 262 | + "y = x[:] # y is a copy of x\n", |
| 263 | + "z = x\n", |
| 264 | + "y == x \n", |
| 265 | + "print y is x \n", |
| 266 | + "print y is not x\n", |
| 267 | + "print z is x" |
265 | 268 | ], |
266 | 269 | "language": "python", |
267 | 270 | "metadata": {}, |
|
304 | 307 | "cell_type": "code", |
305 | 308 | "collapsed": false, |
306 | 309 | "input": [ |
307 | | - "x = [] # An empty list\n", |
308 | | - "y = [0] # A list with one item\n", |
| 310 | + "x = '' # An empty list\n", |
| 311 | + "y = 'a' # A list with one item\n", |
309 | 312 | "\n", |
310 | 313 | "if x:\n", |
311 | 314 | " print \"x is true\"\n", |
|
316 | 319 | "metadata": {}, |
317 | 320 | "outputs": [] |
318 | 321 | }, |
| 322 | + { |
| 323 | + "cell_type": "markdown", |
| 324 | + "metadata": {}, |
| 325 | + "source": [ |
| 326 | + "#### Exercises\n", |
| 327 | + "\n", |
| 328 | + "1. Create a if..elif..else block that will compare a variable containing your age to another variable containing another persons age and print a statement which says if you are younger, older or the same age as that person." |
| 329 | + ] |
| 330 | + }, |
319 | 331 | { |
320 | 332 | "cell_type": "heading", |
321 | 333 | "level": 2, |
|
353 | 365 | "input": [ |
354 | 366 | "codeList = ['NA06984', 'NA06985', 'NA06986', 'NA06989', 'NA06991']\n", |
355 | 367 | "\n", |
356 | | - "for code in codeList:\n", |
357 | | - " print code" |
| 368 | + "for abc in codeList:\n", |
| 369 | + " print abc" |
358 | 370 | ], |
359 | 371 | "language": "python", |
360 | 372 | "metadata": {}, |
|
393 | 405 | "input": [ |
394 | 406 | "rnaMassDict = {\"G\":345.21, \"C\":305.18, \"A\":329.21, \"U\":302.16}\n", |
395 | 407 | "\n", |
396 | | - "for key in rnaMassDict:\n", |
397 | | - " print key, rnaMassDict[key]" |
| 408 | + "for x in rnaMassDict:\n", |
| 409 | + " print x, rnaMassDict[x]" |
398 | 410 | ], |
399 | 411 | "language": "python", |
400 | 412 | "metadata": {}, |
|
416 | 428 | "\n", |
417 | 429 | "for v in values:\n", |
418 | 430 | " total = total + v\n", |
| 431 | + " print total\n", |
419 | 432 | "\n", |
420 | 433 | "print total" |
421 | 434 | ], |
|
479 | 492 | "input": [ |
480 | 493 | "value = 0.25\n", |
481 | 494 | "while value < 32.0:\n", |
482 | | - " value *= 2.0\n", |
| 495 | + " \n", |
| 496 | + " print value\n", |
483 | 497 | "\n", |
484 | 498 | "print value" |
485 | 499 | ], |
|
570 | 584 | "An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if you delete the current an item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if you insert an item in a sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence." |
571 | 585 | ] |
572 | 586 | }, |
| 587 | + { |
| 588 | + "cell_type": "markdown", |
| 589 | + "metadata": {}, |
| 590 | + "source": [ |
| 591 | + "3 is skipped" |
| 592 | + ] |
| 593 | + }, |
573 | 594 | { |
574 | 595 | "cell_type": "code", |
575 | 596 | "collapsed": false, |
576 | 597 | "input": [ |
577 | | - "values = [1, 2, 3, 4, 5]\n", |
| 598 | + "values = [1, 2, 2, 4, 5]\n", |
578 | 599 | "for v in values:\n", |
579 | 600 | " if v == 2:\n", |
580 | 601 | " values.remove(v)\n", |
581 | | - " print v\n" |
| 602 | + " print v\n", |
| 603 | + "\n", |
| 604 | + "print values" |
582 | 605 | ], |
583 | 606 | "language": "python", |
584 | 607 | "metadata": {}, |
|
591 | 614 | "valuesCopy = values[:]\n", |
592 | 615 | "for v in values:\n", |
593 | 616 | " if v == 2:\n", |
594 | | - " values.remove(v)\n", |
595 | | - " \n", |
596 | | - "values = valuesCopy\n", |
597 | | - "print values" |
| 617 | + " valuesCopy.remove(v)\n", |
| 618 | + " \n", |
| 619 | + "print valuesCopy " |
598 | 620 | ], |
599 | 621 | "language": "python", |
600 | 622 | "metadata": {}, |
|
0 commit comments