Programming Bootcamp 2016

Lesson 4 Exercises - ANSWERS



1. Guess the output: list practice (1pt)

For the following blocks of code, first try to guess what the output will be, and then run the code yourself. Points will be given for filling in the guesses; guessing wrong won't be penalized.


In [45]:
ages = [65, 34, 96, 47]

print len(ages)


4

In [46]:
names = ["Wilfred", "Manfred", "Wadsworth", "Jeeves"]
ages = [65, 34, 96, 47]

print len(ages) == len(names)


True

In [47]:
ages = [65, 34, 96, 47]

for hippopotamus in ages:
    print hippopotamus


65
34
96
47

In [48]:
ages = [65, 34, 96, 47]

print ages[1:3]


[34, 96]

In [49]:
names = ["Wilfred", "Manfred", "Wadsworth", "Jeeves"]

if "Willard" not in names:
    names.append("Willard")
print names


['Wilfred', 'Manfred', 'Wadsworth', 'Jeeves', 'Willard']

In [50]:
names = ["Wilfred", "Manfred", "Wadsworth", "Jeeves"]
ages = [65, 34, 96, 47]

for i in range(len(names)):
    print names[i],"is",ages[i]


Wilfred is 65
Manfred is 34
Wadsworth is 96
Jeeves is 47

In [51]:
ages = [65, 34, 96, 47]

ages.sort()
print ages


[34, 47, 65, 96]

In [52]:
ages = [65, 34, 96, 47]

ages = ages.sort()
print ages


None

Remember that .sort() is an in-place function. Its return value is "None". (This is a special value in Python that basically means "null". It's used as a placeholder sometimes when we don't want to give something a value.)


In [53]:
ages = [65, 34, 96, 47]

print max(ages)


96

In [54]:
cat = "Mitsworth"

for i in range(len(cat)):
    print cat[i]


M
i
t
s
w
o
r
t
h

In [55]:
cat = "Mitsworth"

print cat[:4]


Mits

In [56]:
str1 = "Good morning, Mr. Mitsworth."

parts = str1.split()
print parts
print str1


['Good', 'morning,', 'Mr.', 'Mitsworth.']
Good morning, Mr. Mitsworth.

In [57]:
str1 = "Good morning, Mr. Mitsworth."

parts = str1.split(",")
print parts


['Good morning', ' Mr. Mitsworth.']

In [58]:
names = ["Wilfred", "Manfred", "Wadsworth", "Jeeves"]

print names[-1]


Jeeves

In [59]:
oldList = [2, 2, 6, 1, 2, 6]
newList = []

for item in oldList:
    if item not in newList:
        newList.append(item)
print newList


[2, 6, 1]

This is an example of how to remove duplicates.


2. On your own: Lists (3pts)

Write code to accomplish each of the following tasks using list functions. Do not copy and paste the list to make changes. You must pretend you don't know what's in the list.


In [60]:
# run this first!
geneNames = ["Ppia", "Gria2", "Mecp2", "Omd", "Zfp410", "Hsp1", "Mtap1a", "Cfl1", 
             "Slc25a40", "Dync1i1", "V1ra4", "Fmnl1", "Mtap2", "Atp5b", "Olfr259", 
             "Atf3", "Vapb", "Dhx8", "Slc22a15", "Orai3", "Ifitm7", "Kcna2", "Timm23", "Shank1"]

(A) (1pt) Replace the 12th element in geneNames with the string "Camk2a".


In [61]:
geneNames[11] = "Camk2a"

(B) (1pt) Add the string "Shank3" to the end of the geneNames list.


In [62]:
geneNames.append("Shank3")

(C) (1pt) Using a loop, print the elements in geneNames that start with "S" or "O".


In [63]:
for gene in geneNames:
    firstChar = gene[0]
    if firstChar == "S" or firstChar == "O":
        print gene


Omd
Slc25a40
Olfr259
Slc22a15
Orai3
Shank1
Shank3

3. On your own: Strings & Splitting (3pts)

Write code to accomplish each of the following tasks using string slicing and splitting.

(A) (1pt) Print characters 3-9 of the string stored in the variable magicWords using string slicing. (You should get "racadab")


In [64]:
magicWords = "abracadabra"
print magicWords[2:9]


racadab

(B) (1pt) Prompt the user to input a sentence using raw_input(). Print each word of their sentence on a separate line.


In [66]:
sentence = raw_input("Input a sentence: ")
splitSentence = sentence.split()
for word in splitSentence:
    print word


Input a sentence: No, you input a sentence!
No,
you
input
a
sentence!

(C) (1pt) Using raw_input(), prompt the user to enter some good ideas for cat names (have them enter the names on one line, separating each name with a comma). Separate the names so that each one is a separate element of a list. Then choose one randomly (using the random module) and print it out.


In [67]:
import random

catNamesStr = raw_input("Input some cat names (separate by comma): ")
catNamesList = catNamesStr.split(",")

randNum = random.randint(0, len(catNamesList)-1)
print catNamesList[randNum]


Input some cat names (separate by comma): Mittens, Tatertot, Meatball
 Tatertot

Note: if you want to strip off the extra whitespace, you can do this:

print catNamesList[randNum].strip(" ")

4. File reading and lists (3pts)

For this problem, use the file genes.txt provided on Piazza. It contains some gene IDs, one on each line.

(A) (1pt) Read the file and print to the screen only the gene IDs that contain "uc007".

[ Check your answer ] You should end up printing 14 gene IDs.


In [68]:
inFile = "genes.txt"
ins = open(inFile, 'r')
for line in ins:
    line = line.rstrip('\n')
    if "uc007" in line:
        print line
ins.close()


uc007zzs.1
uc007hnl.1
uc007hnl.1
uc007xgk.1
uc007shu.4
uc007ztg.2
uc007nkt.1
uc007piu.2
uc007ker.1
uc007vlq.1
uc007xog.1
uc007kmj.1
uc007cib.1
uc007ket.1

(B) (1 pt) Print to the screen only unique gene IDs (remove the duplicates). Do not assume repeat IDs appear consecutively in the file.

Hint: see problem 1 for an example of duplicate checking.

[ Check your answer ] You should end up printing 49 gene IDs.


In [69]:
inFile = "genes.txt"
geneList = []
ins = open(inFile, 'r')
for line in ins:
    line = line.rstrip('\n')
    if line not in geneList: #keep track of what we've already seen using a list
        geneList.append(line)
        print line
ins.close()


uc007zzs.1
uc009akk.2
uc009eyb.1
uc008vlv.1
uc008wzq.1
uc007hnl.1
uc008tvu.1
uc008vlv.3
uc007xgk.1
uc009qsh.1
uc008all.1
uc008eda.1
uc007shu.4
uc009mor.1
uc008fux.1
uc007ztg.2
uc007nkt.1
uc008qul.3
uc008ktr.2
uc008iwn.1
uc009fxp.2
uc008vsh.1
uc008gkj.2
uc007piu.2
uc008vsk.1
uc008vsv.1
uc008kjh.1
uc009dri.1
uc008vlv.2
uc009rxy.1
uc008fyq.1
uc009act.1
uc008lub.1
uc007ker.1
uc008qiz.2
uc008bak.1
uc008kcg.1
uc009cjg.1
uc007vlq.1
uc007xog.1
uc009avv.1
uc008kcg.2
uc007kmj.1
uc008oaj.1
uc007cib.1
uc007ket.1
uc009rpf.1
uc008owo.1
uc008jaq.1

(C) (1 pt) Print to the screen only the gene IDs that are still unique after removing the ".X" suffix (where X is a number).

[ Check your answer ] You should end up printing 46 gene IDs.


In [70]:
inFile = "genes.txt"
geneList = []
ins = open(inFile, 'r')
for line in ins:
    line = line.rstrip('\n')
    splitLine = line.split(".") #could also do this using string slicing: line[:-2]
    geneID = splitLine[0] #the splitting returns a list, and the geneID should be the first part
    if geneID not in geneList: 
        geneList.append(geneID)
        print geneID
ins.close()


uc007zzs
uc009akk
uc009eyb
uc008vlv
uc008wzq
uc007hnl
uc008tvu
uc007xgk
uc009qsh
uc008all
uc008eda
uc007shu
uc009mor
uc008fux
uc007ztg
uc007nkt
uc008qul
uc008ktr
uc008iwn
uc009fxp
uc008vsh
uc008gkj
uc007piu
uc008vsk
uc008vsv
uc008kjh
uc009dri
uc009rxy
uc008fyq
uc009act
uc008lub
uc007ker
uc008qiz
uc008bak
uc008kcg
uc009cjg
uc007vlq
uc007xog
uc009avv
uc007kmj
uc008oaj
uc007cib
uc007ket
uc009rpf
uc008owo
uc008jaq

5. Practice with .split() (4pts)

Use init_sites.txt to complete the following. This file contains a subset of translation initiation sites in mouse, identified by Ingolia et al. (Cell, 2011). Note that this file has a header, which you will want to skip over.

(A) (2 pt) Write a script that reads this file and computes the average CDS length (i.e. average the values in the 7th column).

[ Check your answer ] You should get an average of 236.36.


In [71]:
fileName = "init_sites.txt"
totalLen = 0
numLines = 0
ins = open(fileName, 'r')
ins.readline()
for line in ins:
    line = line.rstrip('\n')
    lineParts = line.split()
    totalLen = totalLen + int(lineParts[6]) #all file input is read as string; must convert to int
    numLines = numLines + 1
print float(totalLen)/numLines
ins.close()


236.36

(B) (2 pt) Write a script that reads this file and prints the "Init Context" from each line (i.e. the 6th column) if and only if the "Codon" column (column 12) is "aug" for that line.

[ Check your answer ] You should print 38 init contexts.


In [72]:
fileName = "init_sites.txt"
ins = open(fileName, 'r')
for line in ins:
    line = line.rstrip('\n')
    lineParts = line.split()
    if lineParts[11] == "aug":
        print lineParts[5]
ins.close()


CAGATGC
AAGATGG
GACATGG
ACCATGA
GACATGG
GCTATGG
AAGATGG
ACCATGG
GCTATGT
GCCATGA
ataatgg
GAAATGG
GCCATGG
AACATGG
GAAATGT
ACTATGG
AGAATGA
GTCATGT
GCCATGA
AGAATGA
AGAATGA
AAGATGG
AGGATGG
GCCATGA
GCCATGG
ACCATGG
AATATGT
AAAATGG
AAGATGG
GTCATGC
ACGATGC
AGCATGG
GCCATGG
GCCATGT
GCAATGG
AGTATGT
GCCATGA
GACATGC

(C) (0 pt) For fun (?), copy and paste your output from (b) into http://weblogo.berkeley.edu/logo.cgi to create a motif logo of the sequence around these initiation sites. What positions/nt seem to be most common?

ATG at positions 4,5,6 (as expected, since this is the canonical initiation codon) and a preference for A/G at position 1


6. Cross-referencing (4pts)

Here you will extract and print the data from init_sites.txt that corresponds to genes with high expression. There isn't gene expression data in init_sites.txt, so we'll have to integrate information from another file.

  • First, use gene_expr.txt to create a list of genes with high expression. We'll say high expression is anything >= 50.
  • Then read through init_sites.txt and print the GeneName (2nd column) and PeakScore (11th column) from any line that matches an ID in your high-expression list.
  • Finally, separately compute the average PeakScore for high expression genes and non-high expression genes. Print both averages to the screen.

[ Check your answer ] There should be 10 lines corresponding to high-expression genes that you print info about. Your average peak scores should be 4.371 and 4.39325 for high and non-high expression genes, respectively.


In [73]:
initFile = "init_sites.txt"
exprFile = "gene_expr.txt"

# create a list of "high expression" genes
highExpr = []
ins = open(exprFile)
ins.readline() #skip header
for line in ins:
    line = line.rstrip('\n')
    lineParts = line.split()
    if float(lineParts[1]) >= 50:
        highExpr.append(lineParts[0])
ins.close()

# average the peak scores for high vs low genes
highExprTotal = 0
highExprCount = 0
lowExprTotal = 0
lowExprCount = 0

ins = open(initFile, 'r')
ins.readline() #skip header
for line in ins:
    line = line.rstrip('\n')
    lineParts = line.split()
    if lineParts[0] in highExpr:
        print lineParts[1], "\t", lineParts[10]
        highExprTotal = highExprTotal + float(lineParts[10])
        highExprCount = highExprCount + 1
    else:
        lowExprTotal = lowExprTotal + float(lineParts[10])
        lowExprCount = lowExprCount + 1
ins.close()

print ""
print "Avg PeakScore high expression genes:", float(highExprTotal) / highExprCount
print "Avg PeakScore low expression genes:", float(lowExprTotal) / lowExprCount


Pa2g4 	4.54
Pa2g4 	4.54
Rexo4 	4.39
Rexo4 	4.39
Psmc4 	4.39
Phb2 	4.33
Prune 	4.30
Tmsb10 	4.29
Rhebl1 	4.28
Tsn 	4.26

Avg PeakScore high expression genes: 4.371
Avg PeakScore low expression genes: 4.39325

7. All-against-all comparisons (4pts)

A common situation that arises in data analysis is that we have a list of data points and we would like to compare each data point to each other data point. Here, we will write a script that computes the "distance" between each pair of strings in a file and outputs a distance matrix. We will define "distance" between two strings as the number of mismatches between two strings when they are lined up, divided by their length.

First we'll use a toy dataset. We'll create a list as follows:

things = [1, 2, 5, 10, 25, 50]

We'll start off by doing a very simple type of pairwise comparison: taking the numerical difference between two numbers. To systematically do this for all possible pairs of numbers in our list, we can make a nested for loop:


In [74]:
things = [1, 2, 5, 10, 25, 50]

for i in range(len(things)):
    for j in range(len(things)):
        print abs(things[i] - things[j]) #absolute value of the difference


0
1
4
9
24
49
1
0
3
8
23
48
4
3
0
5
20
45
9
8
5
0
15
40
24
23
20
15
0
25
49
48
45
40
25
0

Try running this code yourself and observe the output. Everything prints out on its own line, which isn't what we want -- we'd usually prefer a matrix-type format. Try this slightly modified code:


In [75]:
things = [1, 2, 5, 10, 25, 50]

for i in range(len(things)):
    for j in range(len(things)):
        print abs(things[i] - things[j]), "\t",
    print ""


0 	1 	4 	9 	24 	49 	
1 	0 	3 	8 	23 	48 	
4 	3 	0 	5 	20 	45 	
9 	8 	5 	0 	15 	40 	
24 	23 	20 	15 	0 	25 	
49 	48 	45 	40 	25 	0 	

This gives us the matrix format we want. Make sure you understand how this code works. FYI, the "\t" is a tab character, and much like "\n", it is invisible once it's printed (it becomes a tab). The comma at the very end of the print statement suppresses the \n that print usually adds on to the end.

So now we know how to do an all-against-all comparison. But how do we compute the number of mismatches between strings? As long as the strings are the same length, we can do something simple like the following:


In [76]:
str1 = "Wilfred"
str2 = "Manfred"
diffs = 0

for k in range(len(str1)):
    if str1[k] != str2[k]: #compare the two strings at the same index
        diffs = diffs + 1
        
print "dist =", round(float(diffs) / len(str1), 2)


dist = 0.43

So this outputs the distance between the two strings, where the distance is defined as the fraction of the sequence length that is mismatched.

Using these two pieces of code as starting points, complete the following:

(A) (2 pt) Create a list of a few short strings of the same length. For example:

things = ["bear", "pear", "boar", "tops", "bops"]

Write code that prints a distance matrix for this list. As in the last example, use the fraction of mismatches between a given pair of words as the measure of their "distance" from each other. Round the distances to 2 decimals.


In [77]:
things = ["bear", "pear", "boar", "tops", "bops"]
for i in range(len(things)):
    for j in range(len(things)):
        str1 = things[i]
        str2 = things[j]
        diffs = 0
        for k in range(len(str1)):
            if str1[k] != str2[k]:
                diffs += 1
        print round(float(diffs)/len(str1),2), "\t",
    print ""


0.0 	0.25 	0.25 	1.0 	0.75 	
0.25 	0.0 	0.5 	1.0 	1.0 	
0.25 	0.5 	0.0 	0.75 	0.5 	
1.0 	1.0 	0.75 	0.0 	0.25 	
0.75 	1.0 	0.5 	0.25 	0.0 	

(B) (2 pt) Now, instead of using a hard coded list like you did in (A), create a list of DNA sequences by reading in the file sequences2.txt. Compute the distance matrix between these sequences and print the distance matrix. Looking at this matrix, do you see a pair of sequences that are much less "distant" from each other than all the rest?


In [78]:
# create list of sequences
things = []
ins = open("sequences2.txt", 'r')
for line in ins:
    line = line.rstrip('\n')
    things.append(line)
ins.close()

# create distance matrix
for i in range(len(things)):
    for j in range(len(things)):
        str1 = things[i]
        str2 = things[j]
        diffs = 0
        for k in range(len(str1)):
            if str1[k] != str2[k]:
                diffs += 1
        print round(float(diffs)/len(str1),2), "\t",
    print ""


0.0 	0.62 	0.78 	0.78 	0.7 	0.7 	0.92 	0.8 	0.76 	0.6 	
0.62 	0.0 	0.78 	0.76 	0.68 	0.72 	0.78 	0.76 	0.76 	0.8 	
0.78 	0.78 	0.0 	0.8 	0.78 	0.72 	0.72 	0.82 	0.12 	0.76 	
0.78 	0.76 	0.8 	0.0 	0.72 	0.84 	0.64 	0.84 	0.8 	0.86 	
0.7 	0.68 	0.78 	0.72 	0.0 	0.7 	0.82 	0.64 	0.76 	0.86 	
0.7 	0.72 	0.72 	0.84 	0.7 	0.0 	0.78 	0.78 	0.72 	0.8 	
0.92 	0.78 	0.72 	0.64 	0.82 	0.78 	0.0 	0.78 	0.74 	0.7 	
0.8 	0.76 	0.82 	0.84 	0.64 	0.78 	0.78 	0.0 	0.74 	0.74 	
0.76 	0.76 	0.12 	0.8 	0.76 	0.72 	0.74 	0.74 	0.0 	0.72 	
0.6 	0.8 	0.76 	0.86 	0.86 	0.8 	0.7 	0.74 	0.72 	0.0 	


Extra questions (0pts)

These questions are for people who would like extra practice. They will not be counted for points.

(A) Following from problem 7 above: As you can see, the distance matrix you made in (B) is symmetrical around the diagonal. This means dist(i,j) is the same as dist(j,i), so we're doing some redundant calculations.

Change the code so that we don't do any unnecessary calculations (including comparing a sequence to itself, which always is 0). For any calculations you skip, you can print "-" or some other place-holder to keep the printed matrix looking neat.

Hint: There's a really simple way to do this! Think about the range of the second loop...


In [79]:
### METHOD #1

print ""
print "METHOD 1: lower triangle"
print ""

things = []
ins = open("sequences2.txt", 'r')
for line in ins:
    line = line.rstrip('\n')
    things.append(line)
ins.close()

for i in range(len(things)):
    for j in range(0,i): #<-- this is the only change we need to make
        str1 = things[i]
        str2 = things[j]
        diffs = 0
        for k in range(len(str1)):
            if str1[k] != str2[k]:
                diffs += 1
        print round(float(diffs)/len(str1),2), "\t",
    print "" 




### METHOD #2
# note that the way it prints will look weird! see below for another example that fixes this

print ""
print "METHOD 2: upper triangle, messed up format"
print ""

things = []
ins = open("sequences2.txt", 'r')
for line in ins:
    line = line.rstrip('\n')
    things.append(line)
ins.close()

for i in range(len(things)):
    for j in range(i+1,len(things)): #<-- 
        str1 = things[i]
        str2 = things[j]
        diffs = 0
        for k in range(len(str1)):
            if str1[k] != str2[k]:
                diffs += 1
        print round(float(diffs)/len(str1),2), "\t",
    print "" 



### METHOD #3
# similar to above, but fixed output formatting to be more easily interpreted

print ""
print "METHOD 3: upper triangle with formatting"
print ""

things = []
ins = open("sequences2.txt", 'r')
for line in ins:
    line = line.rstrip('\n')
    things.append(line)
ins.close()

for i in range(len(things)):
    for j in range(len(things)):
        if j > i:
            str1 = things[i]
            str2 = things[j]
            diffs = 0
            for k in range(len(str1)):
                if str1[k] != str2[k]:
                    diffs += 1
            print round(float(diffs)/len(str1),2), "\t",
        else:
            print " - \t",
    print ""


METHOD 1: lower triangle


0.62 	
0.78 	0.78 	
0.78 	0.76 	0.8 	
0.7 	0.68 	0.78 	0.72 	
0.7 	0.72 	0.72 	0.84 	0.7 	
0.92 	0.78 	0.72 	0.64 	0.82 	0.78 	
0.8 	0.76 	0.82 	0.84 	0.64 	0.78 	0.78 	
0.76 	0.76 	0.12 	0.8 	0.76 	0.72 	0.74 	0.74 	
0.6 	0.8 	0.76 	0.86 	0.86 	0.8 	0.7 	0.74 	0.72 	

METHOD 2: upper triangle, messed up format

0.62 	0.78 	0.78 	0.7 	0.7 	0.92 	0.8 	0.76 	0.6 	
0.78 	0.76 	0.68 	0.72 	0.78 	0.76 	0.76 	0.8 	
0.8 	0.78 	0.72 	0.72 	0.82 	0.12 	0.76 	
0.72 	0.84 	0.64 	0.84 	0.8 	0.86 	
0.7 	0.82 	0.64 	0.76 	0.86 	
0.78 	0.78 	0.72 	0.8 	
0.78 	0.74 	0.7 	
0.74 	0.74 	
0.72 	


METHOD 3: upper triangle with formatting

 - 	0.62 	0.78 	0.78 	0.7 	0.7 	0.92 	0.8 	0.76 	0.6 	
 - 	 - 	0.78 	0.76 	0.68 	0.72 	0.78 	0.76 	0.76 	0.8 	
 - 	 - 	 - 	0.8 	0.78 	0.72 	0.72 	0.82 	0.12 	0.76 	
 - 	 - 	 - 	 - 	0.72 	0.84 	0.64 	0.84 	0.8 	0.86 	
 - 	 - 	 - 	 - 	 - 	0.7 	0.82 	0.64 	0.76 	0.86 	
 - 	 - 	 - 	 - 	 - 	 - 	0.78 	0.78 	0.72 	0.8 	
 - 	 - 	 - 	 - 	 - 	 - 	 - 	0.78 	0.74 	0.7 	
 - 	 - 	 - 	 - 	 - 	 - 	 - 	 - 	0.74 	0.74 	
 - 	 - 	 - 	 - 	 - 	 - 	 - 	 - 	 - 	0.72 	
 - 	 - 	 - 	 - 	 - 	 - 	 - 	 - 	 - 	 - 	

(B) Below is a loop that creates a list. Do the same thing but with a list comprehension instead.


In [80]:
# Loop version:
import random
randomNums = []

for i in range(100):
    randomNums.append(random.randint(0,10))
    
print randomNums


[7, 6, 9, 3, 3, 5, 6, 0, 10, 4, 8, 1, 1, 0, 3, 8, 5, 6, 3, 0, 7, 4, 3, 10, 0, 8, 2, 8, 1, 0, 9, 10, 2, 1, 0, 3, 5, 8, 6, 7, 4, 4, 4, 9, 3, 2, 5, 4, 2, 4, 9, 2, 7, 5, 6, 3, 4, 2, 10, 9, 1, 10, 8, 0, 10, 0, 8, 9, 2, 2, 0, 4, 5, 1, 10, 2, 8, 5, 9, 3, 10, 0, 3, 1, 5, 3, 2, 8, 0, 4, 9, 7, 9, 3, 4, 2, 6, 8, 7, 2]

In [81]:
# Your list comprehension version here:
randomNums = [random.randint(0,10) for i in range(100)]
print randomNums


[5, 10, 4, 5, 1, 2, 3, 7, 8, 4, 9, 6, 8, 0, 9, 3, 6, 0, 7, 0, 6, 4, 8, 0, 4, 0, 6, 3, 1, 10, 8, 4, 4, 1, 0, 5, 0, 2, 3, 10, 4, 5, 6, 1, 4, 1, 7, 2, 3, 1, 3, 2, 2, 0, 10, 1, 10, 7, 8, 7, 0, 3, 6, 5, 1, 4, 3, 4, 9, 6, 6, 0, 4, 10, 5, 8, 0, 5, 4, 7, 8, 2, 10, 5, 8, 4, 5, 1, 3, 7, 3, 6, 2, 0, 4, 10, 5, 7, 9, 3]

(C) Below is a loop that creates a list. Do the same thing but with a list comprehension instead.


In [82]:
# Loop version:
import random
randomNums = []

for i in range(100):
    if (i % 2) == 0:
        randNum = random.randint(0,10)
        randNumStr = str(randNum)
        randomNums.append(randNumStr)
    
print randomNums


['5', '6', '1', '4', '8', '6', '7', '7', '9', '2', '5', '0', '8', '7', '3', '7', '7', '5', '3', '2', '7', '3', '6', '8', '0', '5', '9', '5', '0', '7', '1', '9', '0', '9', '4', '3', '5', '5', '6', '6', '10', '0', '8', '3', '2', '3', '2', '7', '4', '1']

Note: I changed this slightly from how it was originally.


In [83]:
# Your list comprehension version here:
randomNums = [str(random.randint(0,10)) for i in range(100) if i % 2 == 0]
print randomNums


['6', '1', '10', '5', '10', '7', '10', '5', '10', '9', '7', '10', '7', '9', '0', '1', '9', '9', '4', '10', '0', '6', '3', '8', '4', '3', '0', '0', '1', '2', '6', '7', '3', '6', '4', '6', '6', '9', '8', '8', '2', '3', '3', '2', '0', '5', '7', '9', '0', '2']