Importing external modules¶
Try me¶
You can use external modules o libraries using the keyword import. These libraries contain functions and variables that you can reuse, no need to write them from scratch. For instance, let us use the math library that contains mathematical variables and functions.
Mathematical library math¶
[1]:
import math
print(math.pi) # The mathematical constant π = 3.141592…
print(math.e) # The mathematical constant e = 2.718281…
print(math.factorial(9)) # math.factorial(x) returns the factorial mathematical of x (x!)
print(math.exp(5)) # math.exp(x) returns e to the power of x
print(math.perm(4,3)) # math.perm(n,k) Returns the number of ways to choose k items from n items without
# repetion
print(math.log(5)) # math.log(x) returns the natural logarithm of x
print(math.cos(math.pi)) # math.cos(rad) Returns the cosine of rad (the angle is passed in radians)
# Other trigonometric functions are sin, tan, atan, acos, asin...
print(math.asinh(math.pi)) # math.asinh(rad) Returns the hyperbolic sin of rad (the angle is passed in radians)
# Other hyperbolic functions are cosh, tanh, acosh, asinh,...
3.141592653589793
2.718281828459045
362880
148.4131591025766
24
1.6094379124341003
-1.0
1.8622957433108482
Note that we need to prepend the name of the library to the name of the variable or function that we want to use.
Random Library random¶
The library random can be used to generate pseudo-random numbers. The term pseudo-random number is preferred since at the end programs in this library use deterministic methods to calculate numbers that appear to be random.
[2]:
import random
print(random.randint(1, 6)) # randint(a,b) returns a random integer number a<=N<=b
print(random.choice([1, 2, 3])) # choice(sequence) returns an element in sequence selected at random
x = [1, 2, 3]
random.shuffle(x)
print(x) # shuffle(x) shuffles the elements of x in place
print(random.uniform(0.0, 1.0)) # uniform(a,b) returns a floating number drawn from a uniform distribution in
# the interval [a,b]
random.gauss(1.0, 2.0) # gauss(mu=0.0, sigma=1.0) returns a floating number drawn from a normal
# distributions with parameters mu and sigma
6
2
[1, 3, 2]
0.9425762742917996
[2]:
3.32206754941009
Modules¶
Complex libraries are organised into modules, so that we do not need import the entire library if we are only interested in some parts of it. The keyword from is used to import a module from a library. For instance the datetime library includes a module called datetime which we can import and use as:
[ ]:
from datetime import datetime
print(datetime.now())
Note that the name of the library and the module is the same, but we have only imported the module datetime and therefore cannot use any other constant, function, or module included in the datetime library.
Aliases¶
We can define aliases to make our code easier to read. An alias is just an alternative name we give to the library or module name to shorten it and make it more contact. Aliases are defined with the keyworkd as:
[ ]:
from datetime import datetime as dt
print(dt.now())
Installing libraries¶
The libraries and modules above are built-in the standard Python distribution and there is no need to install them. If you use Google Colabs, it packs some additional modules to make your work more efficient, but you may find that they are missing in other distributions and need to be installed. Libraries can be installed using the package installer PiP. Fortunately, you can install libraries in the runtime environment of your Notebook with pip adding an exclamation mark before the pip command:
[ ]:
!pip install numpy
The line above will install Numpy and all its related dependencies in your runtime if it is not already available (if you run it in colabs, you will be happy to know it is already preinstalled)