Skip to content

Commit 973ca9e

Browse files
committed
Merge branch 'master' of https://github.com/pycam/python-intro
2 parents f3d0532 + 03560bb commit 973ca9e

4 files changed

Lines changed: 105 additions & 26 deletions

File tree

Introduction_to_python_session_2.ipynb

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"cell_type": "code",
9696
"collapsed": false,
9797
"input": [
98-
"x = 3\n",
98+
"x = -3\n",
9999
"\n",
100100
"if x > 0:\n",
101101
" print \"Value is positive\"\n",
@@ -157,7 +157,7 @@
157157
"collapsed": false,
158158
"input": [
159159
"gene = \"BRCA2\"\n",
160-
"geneExpression = 1.2\n",
160+
"geneExpression = -1.2\n",
161161
"\n",
162162
"if geneExpression < 0:\n",
163163
" print gene, \"is downregulated\"\n",
@@ -166,7 +166,7 @@
166166
" print gene, \"is upregulated\"\n",
167167
" \n",
168168
"else:\n",
169-
" pass # Placeholder - do nothing"
169+
" pass\n"
170170
],
171171
"language": "python",
172172
"metadata": {},
@@ -184,7 +184,7 @@
184184
"cell_type": "code",
185185
"collapsed": false,
186186
"input": [
187-
"x = 5\n",
187+
"x = 11\n",
188188
"s = \"Yes\" if x < 10 else \"No\"\n",
189189
"print s"
190190
],
@@ -226,19 +226,19 @@
226226
"cell_type": "markdown",
227227
"metadata": {},
228228
"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."
230230
]
231231
},
232232
{
233233
"cell_type": "code",
234234
"collapsed": false,
235235
"input": [
236-
"x = 20\n",
236+
"x = -5\n",
237237
"\n",
238-
"if 0 < x < 10:\n",
238+
"if x > 0 and x < 10:\n",
239239
" print \"In range A\"\n",
240240
" \n",
241-
"elif -10 < x <= 20:\n",
241+
"elif x < 0 or x > 10:\n",
242242
" print \"In range B\""
243243
],
244244
"language": "python",
@@ -259,9 +259,12 @@
259259
"collapsed": false,
260260
"input": [
261261
"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"
265268
],
266269
"language": "python",
267270
"metadata": {},
@@ -304,8 +307,8 @@
304307
"cell_type": "code",
305308
"collapsed": false,
306309
"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",
309312
"\n",
310313
"if x:\n",
311314
" print \"x is true\"\n",
@@ -316,6 +319,15 @@
316319
"metadata": {},
317320
"outputs": []
318321
},
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+
},
319331
{
320332
"cell_type": "heading",
321333
"level": 2,
@@ -353,8 +365,8 @@
353365
"input": [
354366
"codeList = ['NA06984', 'NA06985', 'NA06986', 'NA06989', 'NA06991']\n",
355367
"\n",
356-
"for code in codeList:\n",
357-
" print code"
368+
"for abc in codeList:\n",
369+
" print abc"
358370
],
359371
"language": "python",
360372
"metadata": {},
@@ -393,8 +405,8 @@
393405
"input": [
394406
"rnaMassDict = {\"G\":345.21, \"C\":305.18, \"A\":329.21, \"U\":302.16}\n",
395407
"\n",
396-
"for key in rnaMassDict:\n",
397-
" print key, rnaMassDict[key]"
408+
"for x in rnaMassDict:\n",
409+
" print x, rnaMassDict[x]"
398410
],
399411
"language": "python",
400412
"metadata": {},
@@ -416,6 +428,7 @@
416428
"\n",
417429
"for v in values:\n",
418430
" total = total + v\n",
431+
" print total\n",
419432
"\n",
420433
"print total"
421434
],
@@ -479,7 +492,8 @@
479492
"input": [
480493
"value = 0.25\n",
481494
"while value < 32.0:\n",
482-
" value *= 2.0\n",
495+
" \n",
496+
" print value\n",
483497
"\n",
484498
"print value"
485499
],
@@ -570,15 +584,24 @@
570584
"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."
571585
]
572586
},
587+
{
588+
"cell_type": "markdown",
589+
"metadata": {},
590+
"source": [
591+
"3 is skipped"
592+
]
593+
},
573594
{
574595
"cell_type": "code",
575596
"collapsed": false,
576597
"input": [
577-
"values = [1, 2, 3, 4, 5]\n",
598+
"values = [1, 2, 2, 4, 5]\n",
578599
"for v in values:\n",
579600
" if v == 2:\n",
580601
" values.remove(v)\n",
581-
" print v\n"
602+
" print v\n",
603+
"\n",
604+
"print values"
582605
],
583606
"language": "python",
584607
"metadata": {},
@@ -591,10 +614,9 @@
591614
"valuesCopy = values[:]\n",
592615
"for v in values:\n",
593616
" 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 "
598620
],
599621
"language": "python",
600622
"metadata": {},

Introduction_to_python_session_4.ipynb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,28 @@
654654
"metadata": {},
655655
"outputs": []
656656
},
657+
{
658+
"cell_type": "code",
659+
"collapsed": false,
660+
"input": [
661+
"%%bash \n",
662+
"cat scripts/search_gzip_file.py\n"
663+
],
664+
"language": "python",
665+
"metadata": {},
666+
"outputs": []
667+
},
668+
{
669+
"cell_type": "code",
670+
"collapsed": false,
671+
"input": [
672+
"%%bash\n",
673+
"python scripts/search_gzip_file.py SRS006837"
674+
],
675+
"language": "python",
676+
"metadata": {},
677+
"outputs": []
678+
},
657679
{
658680
"cell_type": "heading",
659681
"level": 2,
@@ -666,8 +688,7 @@
666688
"cell_type": "markdown",
667689
"metadata": {},
668690
"source": [
669-
"1. Write a program that finds whether an individual sample is available in the *1000 genomes* project. A snapshot of the *1000 genomes* data is available as ``1000genomes_samples.csv.gz``. The program should take an identifier on the command line, and print \"Found\" or \"Not found\" to the terminal. **Hint**: use ``GzipFile`` from the ``gzip`` package to open a compressed file directly.\n",
670-
"2. Write a program that takes a file containing sample accession numbers and writes out a csv-file containing the accession number and a boolean value whether it is in the *1000genomes* data or not. **Hint**: preprocess the *1000genomes* data into a data structure that allows quick membership tests."
691+
"1. Write a program that extends the search_gzip_file.py script to use a file containing sample accession numbers and writes out a csv-file containing the accession number and a boolean value whether it is in the *1000genomes* data or not. **Hint**: preprocess the *1000genomes* data into a data structure that allows quick membership tests."
671692
]
672693
},
673694
{

scripts/search_gzip_file.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import gzip
2+
import sys
3+
import csv
4+
5+
if len( sys.argv ) != 2:
6+
print "Usage: %s ACCESSION" % sys.argv[0]
7+
sys.exit(1)
8+
9+
with gzip.GzipFile( 'data/1000genomes_samples.csv.gz' ) as fobj:
10+
reader = csv.reader( fobj )
11+
12+
for record in reader:
13+
if record[1] == sys.argv[1]:
14+
print "Found"
15+
sys.exit(0)
16+
17+
print "Not found"

solutions/2_0.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name_1 = "James Morris"
2+
age_1 = 31
3+
4+
name_2 = "Mark Cavendish"
5+
age_2 = 29
6+
7+
# name_2 = "Bradley Wiggins"
8+
# age_2 = 33
9+
10+
# name_2 = "Fabian Cancellara"
11+
# age_2 = 31
12+
13+
14+
if age_1 > age_2:
15+
print name_1, 'is older than', name_2
16+
elif age_1 < age_2:
17+
print name_2, 'is older than', name_1
18+
else:
19+
print name_1, 'and', name_2, 'are the same age'

0 commit comments

Comments
 (0)