Watch Me Code 1: Modules and Help


In [1]:
# Let's import the math module from the Python standard library
import math

In [2]:
# what can we do with math? use the dir() function to investigate
dir(math)


Out[2]:
['__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'acos',
 'acosh',
 'asin',
 'asinh',
 'atan',
 'atan2',
 'atanh',
 'ceil',
 'copysign',
 'cos',
 'cosh',
 'degrees',
 'e',
 'erf',
 'erfc',
 'exp',
 'expm1',
 'fabs',
 'factorial',
 'floor',
 'fmod',
 'frexp',
 'fsum',
 'gamma',
 'gcd',
 'hypot',
 'inf',
 'isclose',
 'isfinite',
 'isinf',
 'isnan',
 'ldexp',
 'lgamma',
 'log',
 'log10',
 'log1p',
 'log2',
 'modf',
 'nan',
 'pi',
 'pow',
 'radians',
 'sin',
 'sinh',
 'sqrt',
 'tan',
 'tanh',
 'trunc']

In [3]:
# let's supose I want to use factorial... how do I get help?
help(math.factorial)


Help on built-in function factorial in module math:

factorial(...)
    factorial(x) -> Integral
    
    Find x!. Raise a ValueError if x is negative or non-integral.


In [4]:
# okay let's try it:
math.factorial(5)


Out[4]:
120

In [5]:
# how about another one?
help(math.gcd)


Help on built-in function gcd in module math:

gcd(...)
    gcd(x, y) -> int
    greatest common divisor of x and y


In [6]:
# trying it out
result = math.gcd(24,32)
print (result)


8

In [ ]:


In [ ]: