Programming Bootcamp 2016

Lesson 1 Exercises


How to earn points

If you would like to get points for your work, enter your name below and send this notebook with your completed problems to me (sarahmid@mail.med.upenn.edu) with "Bootcamp Lesson 1" as the subject line before 9:00 am on 9/9. You do not need to complete all the problems to get points. Points will be assigned based on correctness or effort, depending on the problem. At the end of the course, everyone who gets at least 90% of the total points will get a prize (bootcamp mug!).

Name (double click here to edit):


Notes on using this notebook

  • Cells with In [] next to them are code cells. Run code using Ctrl+Enter (or Shift+Enter)
  • In places where it says "Your guess" or "Your answer", double click to edit the cell with your answer.
  • I will often provide you with expected output for a given problem. Use this to check that your code is correct.
  • If the directions for a question don't specify a particular way of doing things, this means you can do it however you want (within reason). For example, if I don't specifically say "print using one line of code", then you can print using multiple print statements if you want.

1. Guess the output: print statement practice (1pt)

For the following blocks of code, first try to guess what the output will be, and then run the code yourself. These examples may introduce some ideas and common pitfalls that were not explicitly covered in the lecture, so be sure to complete this section. Points will be given for filling in the guesses; guessing wrong won't be penalized.


In [ ]:
print "what's", "up"

Your guess:


In [ ]:
print "what's" + "up"

Your guess:


In [ ]:
print "I have", 5, "cats"

Your guess:


In [ ]:
print "I have" + 5 + "cats"

Your guess:


In [ ]:
print 9 - 6 * 2

Your guess:


In [ ]:
print (9 - 6) * 2

Your guess:


In [ ]:
print 24 % 6

Your guess:


In [ ]:
print 24 % 7

Your guess:


In [ ]:
print -3 ** 2

Your guess:


In [ ]:
print 9 / 2

Your guess:


In [ ]:
print 9.0 / 2

Your guess:


In [ ]:
print 9 / float(2)

Your guess:


2. Guess the output: variables practice (1pt)

For the following blocks of code, first try to guess what the output will be, and then run the code yourself.


In [ ]:
x = 5
print x * 3

Your guess:


In [ ]:
x = "5"
print x * 3

Your guess:


In [ ]:
x = "5"
print int(x) * 3

Your guess:


In [ ]:
x = "cat"
y = x
print y

Your guess:


In [ ]:
x = "cat"
y = "dog"
x = y
y = x
print y

Your guess:


In [ ]:
x = "cat"
y = "dog"
print x + y

Your guess:


In [ ]:
x = 5
x = 1
print x

Your guess:


In [ ]:
x = 5
x + 1
print x

Your guess:


In [ ]:
x = 2
y = 4
print (x * y) ** x

Your guess:


In [ ]:
x = 16
print x ** 0.5

Your guess:


3. On your own: printing (1pt)

Write a single line of code that prints your name:


In [ ]:

Add a single line of code to what's already below to print the variables color and number:


In [1]:
color = "magenta"
number = 2715

4. Fix the code (1pt)

The following code blocks have bugs. Fix each one so that it runs without error. There may be multiple errors!


In [ ]:
print "I bought" + 10 + "pizzas"

In [ ]:
1stPerson = "Joe"
print 1stPerson

In [ ]:
fruit = "Banana"
print "I bought a", friut

In [ ]:
age = "25"
name = Wilfred
introduction = "Hello, my name is " + name + " and I am " + age " years old."
prnt introduction

5. Math practice I (1pt)

The equation for a line is

y = mx + b

Write code to solve for y for a given m, x, and b. Print the value of y at the end. Use the code below as a starting point, and fill in the rest of the needed code.

[ Check your answer ] For the given values of m, x, and b below (0.5, 10, 3), you should get the answer 8.0.


In [ ]:
m = 0.5
x = 10
b = 3

6. Math practice II (2pt)

The quadratic formula is defined as:

$$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$

Write code to calculate the quadratic formula for a given a, b, and c. Your code should print both possible values for x (that is, the values produced by doing + or - for the $\pm$).

I haven't told you yet how to find the square root in Python because I'd like you to look this up for yourself! There are actually several different ways -- try googling it.

[ Check your answer ] For the given values of a, b, and c (-2, 2, 1), you should get the answers -0.366 and 1.366.


In [ ]:
a = -2
b = 2
c = 1

After you verify that your code works, try a few different values of these variables. Note, you will get an error message if $b^2 - 4ac$ is negative, since you can't take the square root of a negative number. This is fine for now -- later we'll go over ways to prevent errors like that from occurring.


7. Reading user input (1pt)

We'll go over this next time, but here's a head start. Run the following code:


In [ ]:
print "Enter your first name"
firstName = raw_input()
print "Enter your last name"
lastName = raw_input()
print "Welcome,", firstName, lastName

What happens? What does raw_input() do?

Your answer:

Google it to see if you're right.

Here's another way you can use the raw_input() function:


In [ ]:
firstName = raw_input("Enter your first name:")
lastName = raw_input("Enter your last name:")
print "Welcome,", firstName, lastName

Run this. What is different about the output?

Your answer:

Oftentimes we can change the behavior of a function by putting different things within the () braces. The values we put in these braces are called arguments. They're sort of like options that you send along with the function that make it do slightly different things. In this case, raw_input() only takes one argument, which is a string that it uses as a "prompt". Different function take different types/numbers of arguments. We're going to see this a lot in the future, so just keep it in the back of your mind.


8. Interactive quadratic formula (2pt)

Edit your quadratic formula code from problem 6 so that it takes the values of a, b, and c interactively using raw_input().

Hint: you may have noticed when you looked up raw_input() that it reads in everything as a string. So if you input 3.5, what actually gets read is "3.5" (a string). This will cause an error when you try to do math using these values. To use them as decimal numbers, we must convert them from strings to floats using the float() function.

Here's an example of how to do this:

age = raw_input("Your age:")
age = float(age)

Or:

age = float(raw_input("Your age:"))

Now, apply this to your quadratic formula code to read in the values of a, b, and c. Check that you still get the correct answer!


In [ ]: