Q2

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

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)

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)

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

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.

What is the remainder? Store this quantity in the variable named remainder.


In [ ]:
n1 = 20344983945038.48374839
n2 = 984374.783894273

quotient = -1
remainder = -1

### BEGIN SOLUTION

### END SOLUTION

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

E

Is there a mathematical operator that can be used to determine if a number is odd or even? If so, which one? If not, can you think of a way how to test if a number is odd or even?