7. Working with external libraries Flashcards
How do you import the math module?
import math
How do you see all the names in the math module?
print(dir(math))
How do you print pi to 4 significant digits?
print("{:.4}".format(math.pi))
How do you find out the log of 32, base 2?
math.log(32,2)
How do you assign the math module the name “mt”
import math as mt
How can you refer to pi directly, without saying math.pi?
from math import * print(pi)
Why is it NOT a good idea to import *
? Give an example.
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 would you just import log and pi from math module?
from math import log, pi
Give an exmaple of a sub-module
rolls = numpy.random.randint(low=1, high=6, size=10)