Q2

In this question, we'll dive a bit deeper into some arithmetic operations.

Part A

Five numbers are given below. Keeping them in that same order, compute a single result with Python addition, multiplication, subtraction, and division, respectively.

Put another way--add the first and second numbers, multiply the second and third numbers, subtract the third and fourth numbers, and divide the fourth and fifth numbers. Don't use any parentheses; let order of operations take care of things. Do this in a single statement, and store the result in the result variable below.


In [ ]:
n1 = 0.4589
n2 = 13
n3 = -59
n4 = 8.43333333
n5 = 2.1

result = -1

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
import numpy as np
np.testing.assert_allclose(result, -770.5569730142857)

Part B

Using the same five numbers as before, this time use parentheses around your arithmetic operation to enforce that the addition and subtraction operations should happen before the others. Again, store your result in the variable result.


In [ ]:
n1 = 0.4589
n2 = 13
n3 = -59
n4 = 8.43333333
n5 = 2.1

result = -1

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
import numpy as np
np.testing.assert_allclose(result, -432.1802333119699)

Part C

Repeat the steps you took in Part B, this time casting each variable to an integer data type before performing any computation. Store your answer in the variable result.


In [ ]:
n1 = 0.4589
n2 = 13
n3 = -59
n4 = 8.43333333
n5 = 2.1

result = -1

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
assert result == -435.5

Part D

Two numbers are given below. When you perform the operation n1 / n2, what is the quotient? Store this quantity in the variable named quotient.


In [ ]:
n1 = 20344983945038.48374839
n2 = 984374.783894273

quotient = -1

### BEGIN SOLUTION

### END SOLUTION

In [ ]:
import numpy as np
np.testing.assert_allclose(quotient, 20667924.735487375)

Part E

You've seen in lecture how, when two numbers in Python are divided, their result is a float. What if you really wanted the output of division to be an integer? See if you can make that happen by modifying the following single line of code.

(You are not allowed to add any numbers or additional lines of code!)


In [ ]:
integer_quotient = 32 / 16

In [ ]:
assert type(integer_quotient) == int