7. Working with external libraries Flashcards

1
Q

How do you import the math module?

A

import math

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you see all the names in the math module?

A

print(dir(math))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you print pi to 4 significant digits?

A

print("{:.4}".format(math.pi))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you find out the log of 32, base 2?

A

math.log(32,2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you assign the math module the name “mt”

A

import math as mt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you refer to pi directly, without saying math.pi?

A
from math import *
print(pi)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is it NOT a good idea to import *? Give an example.

A

These kinds of “star imports” can occasionally lead to weird, difficult-to-debug situations.

E.g. the problem in this case is that the math and numpy modules both have functions called log, but they have different semantics. Because we import from numpy second, its log overwrites (or “shadows”) the log variable we imported from math.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you just import log and pi from math module?

A

from math import log, pi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an exmaple of a sub-module

A

rolls = numpy.random.randint(low=1, high=6, size=10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly