Earning points (optional)
.ipynb
file to me (sarahmid@mail.med.upenn.edu) before 9:00 am on 9/13. Name:
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.
NOTE: The first cell below holds the variables that will be used in the problems. Since variables are shared across cells in Jupyter notebooks, you just need to run this cell once and then those variables can be used in any other code cell.
(However, remember that closing the notebook or restarting the kernel clears all the variables, so you will need to re-run the cell to re-populate those variables after doing that.)
In [ ]:
# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
a = True
b = False
x = 2
y = -2
cat = "Mittens"
(^^ you might find it helpful to copy these variables somewhere you can easily see them when doing the problems. e.g. a piece of paper or a text file.)
In [ ]:
print a
Your guess:
In [ ]:
print (not a)
Your guess:
In [ ]:
print (a == b)
Your guess:
In [ ]:
print (a != b)
Your guess:
In [ ]:
print (x == y)
Your guess:
In [ ]:
print (x > y)
Your guess:
In [ ]:
print (x = 2)
Your guess:
In [ ]:
print (a and b)
Your guess:
In [ ]:
print (a and not b)
Your guess:
In [ ]:
print (a or b)
Your guess:
In [ ]:
print (not b or a)
Your guess:
In [ ]:
print not (b or a)
Your guess:
In [ ]:
print (not b and a)
Your guess:
In [ ]:
print not (b and a)
Your guess:
In [ ]:
print (x == abs(y))
Your guess:
In [ ]:
print len(cat)
Your guess:
In [ ]:
print cat + x
Your guess:
In [ ]:
print cat + str(x)
Your guess:
In [ ]:
print float(x)
Your guess:
In [ ]:
print ("i" in cat)
Your guess:
In [ ]:
print ("g" in cat)
Your guess:
In [ ]:
print ("Mit" in cat)
Your guess:
What is "
in
"? This is just another Python operator that you can use in your conditionals. As you may have guessed,(x in y)
evaluates toTrue
ifx
is found iny
. This can be used to check if a string is contained in another string (e.g."Mit" in "Mittens"
).
In [ ]:
# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
x = 2
y = -2
cat = "Mittens"
In [ ]:
if (x % 2) == 0:
print "x is even"
else:
print "x is odd"
Your guess:
In [ ]:
if (y - 4*x) < 0:
print "Invalid!"
else:
print "Banana"
Your guess:
In [ ]:
if "Mit" in cat:
print "Hey Mits!"
else:
print "Where's Mits?"
Your guess:
In [ ]:
x = "C"
if x == "A" or "B":
print "yes"
else:
print "no"
Your guess:
In [ ]:
x = "C"
if (x == "A") or (x == "B"):
print "yes"
else:
print "no"
Your guess:
Surprised by the last two? It's important to note that when you want compare a variable against multiple things, you can only compare it to one thing at a time. Although it makes sense in English to say "is x equal to A or B?", in Python you must write: ((x == "A") or (x == "B")) to accomplish this. The same goes for e.g. ((x > 5) and (x < 10)) and anything along those lines.
So why does the first version give the answer "yes"? Basically, anything that isn't
False
or the literal number 0 is considered to beTrue
in Python. When you say 'x == "A" or "B"
', Python reads it as '(x == "A") or (B)
', which evaluates to '(False) or (True)
', which isTrue
!
In [ ]:
# RUN THIS BLOCK FIRST!
num1 = 3.14159
num2 = 6
num3 = 100
num4 = 6 * -5 - 4 * 2 + -7 * -8 + 3
motif = "GTAAGTCGGTAACGTAAGTCGGTAAC"
(A) Check if num3
is greater than num4
. Print "yes" if it is, and "no" if it isn't.
In [ ]:
(B) Print a random integer between num2
and num3
.
In [ ]:
(C) Check if the length of the string stored in motif
is less than or equal to 25. Print "yes" if it is, and "no" if it isn't.
In [ ]:
(D) Round num1
to the nearest hundredth.
In [ ]:
(E) Check if num4 is positive or negative.
In [ ]:
Recall that when calculating the quadratic formula, you will get an error if $b^2 - 4ac$ is negative, since you can't take the square root of a negative number.
Edit the code below so that it checks for this potential error before it occurs:
In [ ]:
a = float(raw_input("Enter value for a: "))
b = float(raw_input("Enter value for b: "))
c = float(raw_input("Enter value for c: "))
underRoot = (b**2 - 4*a*c)
x1 = ( (-b) + underRoot ** 0.5 ) / float(2*a)
x2 = ( (-b) - underRoot ** 0.5 ) / float(2*a)
print "x =", x1, "or", x2
[ Check your answer ]
x = -0.267949192431 or -3.73205080757
non-real answer
(A) Find the length of the motif and length of the DNA sequence and check if the motif is longer than the sequence. If it is, print "Motif is too long".
In [ ]:
(B) Adding to your code from part (A): If and only if the motif is shorter than the sequence, go on to check if the motif can be found somewhere within the sequence (hint: use "in
"). If it is found, print "Motif found". If it is not found, print "Motif not found".
In [ ]:
[ Check your answer ] Try running your code above using the following input and make sure your results match the expected output.
Sequence:
AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG
Motif:ATG
Expected output:Motif not found
Sequence:
AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG
Motif:ATCGA
Expected output:Motif found
Sequence:
CTAGCC
Motif:ATGGCTAGCTA
Expected output:Motif is too long
Write code that prompts the user to guess a password and checks whether the password is correct.
password
and set it equal to whatever you want the password to be (any string). (A) Do this giving the user only one chance to guess.
In [ ]:
(B) Do this giving the user 3 chances to guess. Only prompt for another guess if the previous guess was incorrect. When they guess correctly, print a confirmation message and end the program. If they don't guess correctly in 3 tries, print "Access denied!" and end the program.
In [ ]:
In [ ]:
import random
randNum = random.randint(0,1)
print randNum
You should see that you always get either 0 or 1. We will pretend that each time we run random.randint(0,1)
we are flipping a coin, where 1 represents heads and 0 represents tails.
Write code that "flips a coin" 10 times and counts how many times it comes up heads. At the end, print how many of the flips were heads.
In [ ]:
(A) Following from the coin flip problem above:
Change your code so that the coin is now "unfair" (i.e. the chance of heads is not equal to the chance of tails). See if you can make it so the probability of heads is 75%.
Hint: There are several ways to do this, but one simple way involves using a larger range of random ints and assigning heads/tails differently.
In [ ]:
(B) Using raw_input()
, prompt the user for a number. Check if that number is between 50 and 100, and print different messages depending on the outcome.
In [ ]:
(C) Using raw_input()
, prompt the user for an integer. Check if the integer is a multiple of 7, and print different messages depending on the outcome.
In [ ]: