Homework 3 Key

CHE 116: Numerical Methods and Statistics

2/2/2019


1. String Formatting

  1. [1 point] Using the math module, print euler's number to 5 digits precision in scientific notation format
  2. [1 point] Print the first 5 powers of 7 with exactly 5 spaces starting from $7^1$
  3. [1 point] Create a variable v whose value is None and print it out
  4. [4 points] Use the same variable, v, and print it with 3 digits of precision in scientific notation. You should receive an error. Using the definition of a sentinel value, why would we want to not have an error if printed without formatting (problem 1.3) but receieve an error when we format it?

1.1


In [2]:
import math

print('{:.5e}'.format(math.e))


2.71828e+00

1.2


In [6]:
#It's ok if didnt' use for loop
for i in range(1, 6):
    print('{:5d}'.format(7**i))


    7
   49
  343
 2401
16807

1.3


In [10]:
v = None
print('{}'.format(v))


None

1.4


In [11]:
print('{:.5}'.format(v))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-55d8b2ebaf57> in <module>()
----> 1 print('{:.5}'.format(v))

TypeError: unsupported format string passed to NoneType.__format__

Print the value without formatting allows it to adopt any data type, like string or number or None. If we try to set digits of precision, that means we are treating v as if it's a number. Therefore if the value is a sentinel, meaning it's not ready, then we should receive an error"

2. Representing Numbers

  1. [1 point] What is the Manitissa for the number $0.4335 \times 10^{-3}$?
  2. [1 point] How many numbers can be represented with 4 bytes?
  3. [1 point] Youtube previously represented view counts on videos with 4 bytes. What is the largest number of views that could be represented?
  4. [2 points] One more person viewed the video from 2.3? What would the view count be? What is this called?
  5. [3 points] We learned that we shouldn't compare floating point numbers with ==. Compare 0.75 and 3 / 4 in Python using == (even though we said we should not). Explain why it worked.

2.1

0.4335 or 4335

2.2

$2^{4 \times 8} = 4,294,967,296$

2.3

$2^{32} - 1 = 4,294,967,295$

2.4

0 because of overflow

2.5


In [12]:
0.75 == 3/4


Out[12]:
True

These two numbers are exactly representable in floating point, so they can be compared. We still don't use == because that is not true of all floating point numbers.

3. Booleans

  1. [4 points] Write an if statement that prints a variable if it has a magnitude less than 40, otherwise it prints 'Error, number is too large'. Demonstrate with your variable being -12.
  2. [2 points] Write an if statement that compares two variables to see if they're equal. Demonstrate this with two strings.

3.1


In [15]:
v = -12
if abs(v) < 40:
    print(v)
else:
    print('Error, number is too large')


-12

3.2


In [17]:
a = 'hi'
b = 'b'
if a == b:
    print('equal')

4. Lists

Use this list for the examples [3, 6, 1, 12, 43, 3, 100] and use only slicing to answer the questions.

  1. [2 points] Reverse the list
  2. [2 points] Print out every 3rd element starting from 6
  3. [2 points] Print out the first 4 elements.
  4. [2 points] Print out the last element using negative indexing
  5. [2 points] Print out all elements except the last one

4.1


In [22]:
a = [3, 6, 1, 12, 43, 3, 100]
a[::-1]


Out[22]:
[100, 3, 43, 12, 1, 6, 3]

4.2


In [18]:
a[1::3]


Out[18]:
[6, 43]

4.3


In [19]:
a[:4]


Out[19]:
[3, 6, 1, 12]

4.4


In [20]:
a[-1]


Out[20]:
100

4.5


In [21]:
a[:-1]


Out[21]:
[3, 6, 1, 12, 43, 3]

5. List Functinos

[4 points] Using the len function, write a python expression that can print the second half of a list variable a. Your answer should be exactly 13 characters.


In [28]:
a[len(a)//2:]


Out[28]:
[12, 43, 3, 100]