Lesson 1 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. Write code to print the following string:

Insert some funny string here

In [ ]:


2. The following code blocks have errors. Fix each one so that it runs without error.


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

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

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

3. The equation for a line is

y = mx + b

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


In [ ]:
m = 
x = 
b =

Homework exercise (10 Points)

Write code to calculate the quadratic formula: Your code should output both possible values for x. The program should start by defining the values of a, b, and c. For example, the first three lines of your script might look like this:

a = -2
b = 2
c = 1

If you use these values, the answers should be -0.366025403784 and 1.36602540378 (check and make sure you get this). Try a few different values of these variables. Note, you will get an error message if b2 - 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 bugs like that from occurring. Also note that we don't require you to round the output, but if you want to, you can google the 'round' function in Python (as an aside, a lot of the day-to-day work of coding is knowing how to use google to solve your problems!).

Hint: There are a few different ways to find the square root in Python. Try googling it.


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