Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook 06_Python-III_assignment/Lesson3_exercises.ipynb

Views: 65
Kernel: Python 2 (SageMath)

Lesson 3: In-class exercises


Instructions: For each problem, write code in the provided code block. Don't forget to run your code to make sure it works.


1. Simple loop practice

Write code to accomplish each of the following tasks using a for loop or a while loop. Choose whichever type of loop you want for each problem (you can try both, if you want extra practice). Note: you may want to refer to the Lesson 3 "extra material" for some hints on how to use range() to make these problems easier.

(A) Print the integers between 3 and 35, inclusive.

for i in range(3,36): print i
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

(B) Print the positive integers less than 100 that are multiples of 7.

for i in range(1, 100,7): print i
1 8 15 22 29 36 43 50 57 64 71 78 85 92 99

(C) Starting with x = 1, double x until it's greater than 1000. Print each value of x as you go along.

x = 1 while 2*x < 1000: print x x = x+1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

(D) Print each character of the string "supercalifragilisticexpialidocious" on a separate line.

for i in "supercalifragilisticexpialidocious": print i
s u p e r c a l i f r a g i l i s t i c e x p i a l i d o c i o u s

2. File reading practice

For these problems, use the file sequences.txt provided with this document. This file contains several DNA sequences of different lengths. You can assume each sequence is on a separate line.

(A) Using a loop, read in each sequence from the file and print it. Make sure to remove any newline characters (\n) while reading in the data.

Seq = "sequences.txt" inSeq = open(Seq, 'r') for line in inSeq: line = line.rstrip('\r\n') print line inSeq.close()
CTGTGCCTGATCTTGAGGTGCCAATGAGACTCAGCGA TAAATCACCGCCCAAGAAGTATAATGCTTGGGGGTGATAGGTTTTACATATTTTTAAGTTCGCTAGCTAAAAATTATCCGTATCATAGGCTGAA CAGTCCTGCCAATAAAAGAAATATCCCAAGACAGATTAAGCTTTAATCTTTGCTCAACCACGTCGTGGTTATGAATTCGCTAAATTAGTTGATCTCGTTGG TGAGGGCGAATTACCCAAGCACCGACTCACTTGTCACGGAAAAATACCGGACAATTTGTATAACTCAACAAAGTTTCGGA TAGCTATGTCAGCCGGAGACCAGAAAGACTCTCTTGTATTTAAGGTCAGGGCTATGGCTATCGAGT TACCGTTATTGCGTGAAACGGTGTAGCTATAGGGCTGAGTGTGTCTTTGTTTCTTCACTCCTATTGGGCTGACTACGATTGCCCTTAGGTTTTCATTTAGTTGTTAATAATCGCTACT TTCTTAAGACCGCCGAGCTTCGTCTTTTATGGCC GAACGACCAACATGCACGGTTAGGGGTTGGAATGCTATCGATTACGTCGGACCGAAAAGTCAGGAAAAAG ATGTGTTGGGGGTCTGGGACCGCGTCGACACCTAGCGCCTTCCACGTAGCATACAGCCTGGCTCACGCGGTTCTGCGGACCCTACATAGT GATCCGATTTGTTTCTACCGGAAGCTCCACGCAGGAGGGAGCAACGCAA CAATAATTAGCCTCTCCCCAGGGCTCACATGCCCCCATGGTTAAATAGCACAAAGCAGATCGGTGACTGGAACCCCCTTCGTTGATGTCCGCTAATCGATGAG CTAATTACCGCTGACTGCAGGGTGTTTCTGGTGTACACTATTCCTATATCGCAATCAATT ACAAGTGATCATCCCGGTCATGCTAAAACGGTGATTAAGGGTACTATGCGAAGTGTAGATATGCCCTGAGCCTCTGGCCGGGCCCATCTTGCA CCGGAACGTGGGAGCTGTTTAAAGGCCGAACATATAACGGATAAGTCTGTGTTAGCGACTAGGCCTGCAGATCAGTTTGAGCTAATAAATTCCA AAAGTGGACTTGAGTAGAGGTGTCGAACAATGATGAGGCCTCTATTTGAATATAAACTGAACGCCAGTAGGTCCAGG AGTCTTCAAAGAGCTGGGAAGGATCTCAGAGTGCGCCACCGACCAGCGTCCGTCCTTAGGTTGATTCTAACGCGAGGGTCTGTACATAACTTCTGTTTGACCTAAATGTATCACA TATTGAATATCAGGCTGAGCGTCCTGACCGGTAAAAAAAACATAAAT CAGAATAGGGGTCTTTCTCTCCCTGTTCATGTATTGTGCACACCTGGCAATGGTACTA CGTAACCTCATGGAAGTTGCCATTAATGTAGAGTCAGACTTGCCCAGCTTCTCGATCACCCAAAATG TTCGTAAGCCCTGACGTGTCTAGCTAAGTTTGTCCGCACGGAGCTA GCGCTGCCGCCATCGGTTGTGCGTCATCGCAATTAGTACCAGGACGGGCGTAGCTAA ACTCGGCCCAACGCTGGCGATATGGGGAAAAACACGGGTACAGGACGACCCTGCGAGCCTCGGAGACAGGCGATAGCGCCCGATCCTGAACT GACGATAATAGCGGCTTTTAAACCCATAGATGGGAAACGCAATGGGTGCGCACGGTGCAGGTAAAGAGTAACAACACGGTGAACGGA ACCCCAACCCTTCAACCCATCTTGGCCCACACTGATCAGTCCAGGTATGAACTGAGGAAGGATAAGGGCAGTGCTGTGTCATACGGGCACCCCACATAACGCCGAT TTACACTAGCCCCGCTATGTTAACACTCGCCCCCCGTGGGCTTTTGCTCCACTGATGTTCGATCTTGTCAGGTCGCGTCTAGGTGAGTGAGTGAAGAT

(B) Now, instead of printing the sequences, output the length of each sequence to the terminal screen. At the end, print the average length of the sequences. (You should get 77.56 as the average.)

Hint: use the concept of an "accumulator" variable to help with computing the average.

lengthline = 0 #initialize length of the line numlines = 0 #initialize the number of lines seen Seq = "sequences.txt" inSeq = open(Seq, 'r') #open Seq into read mode for line in inSeq: line = line.rstrip('\r\n') lengthline = lengthline + len(line) #keep running tab instead of overwriting the previous one numlines = numlines + 1 print float(lengthline)/numlines
77.56

3. File writing practice

(A) Write a script that prints "Hello, world" to a file called hello.txt

fileHello = "hello.txt" outFile = open(fileHello, 'w') outFile.write("Hello, world") outFile.close()

(B) Write a script that prints the following pieces of data to a file called meow.txt. Each piece of data must be printed to a separate line.

# data to be printed: name = "Mitsworth" age = 11 birthday = "9/1/04" coloring = "Tabby" livesRemaining = 8 # write your code here: fileName = "meow.txt" outFile = open(fileName, 'w') outFile.write("name = Mitsworth\nage = 11\nbirthday = 9/1/04\ncoloring = Tabby\nlivesRemaining = 8") outFile.close()

Homework exercise (10 Points)


String manipulation 101

These problems follow from problem 2 above. Continue using the file sequences.txt.

(A) Instead of printing lengths as before, print the GC content of each sequence (GC content is the number of G's and C's in a DNA sequence divided by the total sequence length). Make sure not to do integer division! (You should get ~0.4877 as the average.) (5 Points)

GCcount = 0 GCcontent = 0 TotalGCcount = 0 TotalBaseLength = 0 Seq = "sequences.txt" inSeq = open(Seq, 'r') for line in inSeq: line = line.strip('\r\n') GCcount = 0 GCcontent = 0 for i in line: if i=="G" or i=="C": #for each character in line GCcount = GCcount + 1 GCcontent = float(GCcount)/len(line) print "The GC content for this sequence is" print GCcontent inSeq.close() Seq = "sequences.txt" inSeq = open(Seq, 'r') GCcount = 0 for line in inSeq: line = line.strip('\r\n') TotalBaseLength = TotalBaseLength + float(len(line)) for i in line: if i=="G" or i=="C": #for each character in line GCcount = GCcount + 1 AvgGCcontent = GCcount/float(TotalBaseLength) print "The average GC is", AvgGCcontent
The GC content for this sequence is 0.540540540541 The GC content for this sequence is 0.36170212766 The GC content for this sequence is 0.386138613861 The GC content for this sequence is 0.4375 The GC content for this sequence is 0.469696969697 The GC content for this sequence is 0.415254237288 The GC content for this sequence is 0.5 The GC content for this sequence is 0.485714285714 The GC content for this sequence is 0.611111111111 The GC content for this sequence is 0.551020408163 The GC content for this sequence is 0.514563106796 The GC content for this sequence is 0.416666666667 The GC content for this sequence is 0.516129032258 The GC content for this sequence is 0.457446808511 The GC content for this sequence is 0.441558441558 The GC content for this sequence is 0.486956521739 The GC content for this sequence is 0.36170212766 The GC content for this sequence is 0.465517241379 The GC content for this sequence is 0.462686567164 The GC content for this sequence is 0.521739130435 The GC content for this sequence is 0.59649122807 The GC content for this sequence is 0.619565217391 The GC content for this sequence is 0.494252873563 The GC content for this sequence is 0.547169811321 The GC content for this sequence is 0.530612244898 The average GC is 0.486333161423 0.486333161423

(B) Convert each sequence to its reverse complement. This means changing each nucleotide to its complement (A->T, T->A, G->C, C->G) and reversing the entire sequence. (5 Points)

Hint: we've already touched on everything you need to know to do this. See the practice problems from Lesson 3 for some hints on reversing..

Seq = "sequences.txt" inSeq = open(Seq, 'r') for line in inSeq: complement = "" for i in line: c = i.replace("A", "t").replace("T", "a").replace("G", "c").replace("C", "g") complement = c + complement reversecomp = "" for i in complement: d = i.replace("t", "T").replace("a", "A").replace("c", "C").replace("g", "G") reversecomp = d + reversecomp print "The reverse complement for each seq is" print reversecomp
The reverse complement for each seq is GACACGGACTAGAACTCCACGGTTACTCTGAGTCGCT The reverse complement for each seq is ATTTAGTGGCGGGTTCTTCATATTACGAACCCCCACTATCCAAAATGTATAAAAATTCAAGCGATCGATTTTTAATAGGCATAGTATCCGACTT The reverse complement for each seq is GTCAGGACGGTTATTTTCTTTATAGGGTTCTGTCTAATTCGAAATTAGAAACGAGTTGGTGCAGCACCAATACTTAAGCGATTTAATCAACTAGAGCAACC The reverse complement for each seq is ACTCCCGCTTAATGGGTTCGTGGCTGAGTGAACAGTGCCTTTTTATGGCCTGTTAAACATATTGAGTTGTTTCAAAGCCT The reverse complement for each seq is ATCGATACAGTCGGCCTCTGGTCTTTCTGAGAGAACATAAATTCCAGTCCCGATACCGATAGCTCA The reverse complement for each seq is ATGGCAATAACGCACTTTGCCACATCGATATCCCGACTCACACAGAAACAAAGAAGTGAGGATAACCCGACTGATGCTAACGGGAATCCAAAAGTAAATCAACAATTATTAGCGATGA The reverse complement for each seq is AAGAATTCTGGCGGCTCGAAGCAGAAAATACCGG The reverse complement for each seq is CTTGCTGGTTGTACGTGCCAATCCCCAACCTTACGATAGCTAATGCAGCCTGGCTTTTCAGTCCTTTTTC The reverse complement for each seq is TACACAACCCCCAGACCCTGGCGCAGCTGTGGATCGCGGAAGGTGCATCGTATGTCGGACCGAGTGCGCCAAGACGCCTGGGATGTATCA The reverse complement for each seq is CTAGGCTAAACAAAGATGGCCTTCGAGGTGCGTCCTCCCTCGTTGCGTT The reverse complement for each seq is GTTATTAATCGGAGAGGGGTCCCGAGTGTACGGGGGTACCAATTTATCGTGTTTCGTCTAGCCACTGACCTTGGGGGAAGCAACTACAGGCGATTAGCTACTC The reverse complement for each seq is GATTAATGGCGACTGACGTCCCACAAAGACCACATGTGATAAGGATATAGCGTTAGTTAA The reverse complement for each seq is TGTTCACTAGTAGGGCCAGTACGATTTTGCCACTAATTCCCATGATACGCTTCACATCTATACGGGACTCGGAGACCGGCCCGGGTAGAACGT The reverse complement for each seq is GGCCTTGCACCCTCGACAAATTTCCGGCTTGTATATTGCCTATTCAGACACAATCGCTGATCCGGACGTCTAGTCAAACTCGATTATTTAAGGT The reverse complement for each seq is TTTCACCTGAACTCATCTCCACAGCTTGTTACTACTCCGGAGATAAACTTATATTTGACTTGCGGTCATCCAGGTCC The reverse complement for each seq is TCAGAAGTTTCTCGACCCTTCCTAGAGTCTCACGCGGTGGCTGGTCGCAGGCAGGAATCCAACTAAGATTGCGCTCCCAGACATGTATTGAAGACAAACTGGATTTACATAGTGT The reverse complement for each seq is ATAACTTATAGTCCGACTCGCAGGACTGGCCATTTTTTTTGTATTTA The reverse complement for each seq is GTCTTATCCCCAGAAAGAGAGGGACAAGTACATAACACGTGTGGACCGTTACCATGAT The reverse complement for each seq is GCATTGGAGTACCTTCAACGGTAATTACATCTCAGTCTGAACGGGTCGAAGAGCTAGTGGGTTTTAC The reverse complement for each seq is AAGCATTCGGGACTGCACAGATCGATTCAAACAGGCGTGCCTCGAT The reverse complement for each seq is CGCGACGGCGGTAGCCAACACGCAGTAGCGTTAATCATGGTCCTGCCCGCATCGATT The reverse complement for each seq is TGAGCCGGGTTGCGACCGCTATACCCCTTTTTGTGCCCATGTCCTGCTGGGACGCTCGGAGCCTCTGTCCGCTATCGCGGGCTAGGACTTGA The reverse complement for each seq is CTGCTATTATCGCCGAAAATTTGGGTATCTACCCTTTGCGTTACCCACGCGTGCCACGTCCATTTCTCATTGTTGTGCCACTTGCCT The reverse complement for each seq is TGGGGTTGGGAAGTTGGGTAGAACCGGGTGTGACTAGTCAGGTCCATACTTGACTCCTTCCTATTCCCGTCACGACACAGTATGCCCGTGGGGTGTATTGCGGCTA The reverse complement for each seq is AATGTGATCGGGGCGATACAATTGTGAGCGGGGGGCACCCGAAAACGAGGTGACTACAAGCTAGAACAGTCCAGCGCAGATCCACTCACTCACTTCTA