math
module, print euler's number to 5 digits precision in scientific notation formatv
whose value is None and print it outv
, 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?
In [2]:
import math
print('{:.5e}'.format(math.e))
In [6]:
#It's ok if didnt' use for loop
for i in range(1, 6):
print('{:5d}'.format(7**i))
In [10]:
v = None
print('{}'.format(v))
In [11]:
print('{:.5}'.format(v))
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"
==
. Compare 0.75
and 3 / 4
in Python using ==
(even though we said we should not). Explain why it worked.
In [12]:
0.75 == 3/4
Out[12]:
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.
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
.if
statement that compares two variables to see if they're equal. Demonstrate this with two strings.
In [15]:
v = -12
if abs(v) < 40:
print(v)
else:
print('Error, number is too large')
In [17]:
a = 'hi'
b = 'b'
if a == b:
print('equal')
Use this list for the examples [3, 6, 1, 12, 43, 3, 100]
and use only slicing to answer the questions.
6
In [22]:
a = [3, 6, 1, 12, 43, 3, 100]
a[::-1]
Out[22]:
In [18]:
a[1::3]
Out[18]:
In [19]:
a[:4]
Out[19]:
In [20]:
a[-1]
Out[20]:
In [21]:
a[:-1]
Out[21]:
In [28]:
a[len(a)//2:]
Out[28]: