Workshop: Advanced Computation (with Python)

From Design and Build Lab
Revision as of 21:01, 30 September 2019 by JosephM (talk | contribs) (Created page with "Modules and Packages Modules and packages are a feature of Python wherein, you can import subtasks (or modules) into your code. Using modules and packages leads to cleaner cod...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Modules and Packages Modules and packages are a feature of Python wherein, you can import subtasks (or modules) into your code. Using modules and packages leads to cleaner code because the files you create are less verbose.

Modules Suppose you write an algorithm called sort.py which sorts a given dataset alphanumerically. Anytime you write a python script where you want to utilize this alphanumeric sorting function you can import the sort.py script into your existing code as a module with the following line:

import sort

Packages Some coders write large modules and publish them for public use. We call these open source modules “packages”. SciPy, NumPy and matplotlib are all examples of popular python packages. We import them into our code with the same syntax as with modules. We can create a nickname for each package, simply “import [packagename] as [nickname]”.

import numpy as np import scipy import matplotlib as plt