Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook 04_Python-I_assignment/Lesson1_exercises.ipynb

Views: 69
Kernel: Python 2 (SageMath)

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
print "I love Python"
I love Python

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

print "I bought" , 10 , "pizzas"
I bought 10 pizzas
firstPerson = "Joe" print firstPerson
Joe
fruit = "Banana" print "I bought a", fruit
I bought a Banana

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.

m = x = b = y=m*x+b print y
File "<ipython-input-7-dec0d3387558>", line 1 m = ^ SyntaxError: invalid syntax

Homework exercise (10 Points)

Write code to calculate the quadratic formula: 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.366 and 1.366 (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.

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

a = -2 b = 2 c = 1 delta = (b**2-4*a*c)**0.5 print "x=",(delta-b)/(2*a),"or",(-delta-b)/(2*a)
x= -0.366025403784 or 1.36602540378