
Overview
A module is a code library, within an external file, containing a set of functions that can be included in a Python application.
Creating a Module
To create a module, specified code is saved in a file with the file extension .py.
The following can be saved into a file called myModule.py.
def greeting(name):
print("Hello, " + name)
Using a Module
The import statement, using the following syntax, is used to call the myModule.py file.
Syntax:
module_name.function_name
Importing and Using:
import myModule
myModule.greeting("Johnny")
Output:
Hello, Johnny
Using Variables with Modules
A module can contain variables of all types (arrays, dictionaries, objects, etc.).
The following imports the module “myModule” and accesses the person dictionary.
import myModule
x = myModule.person["age"]
print(x)
Output:
12
Renaming a Module
An alias can be created when you import a module by using the as keyword.
import myModule as mm
x = mm.person["age"]
print(x)
Output:
12
Using Built-In Modules
There are numerous built-in modules in Python, that can be imported and used. For example, the platform module is a built-in module that contains the dir() function which lists all the function or variable names contained in ANY module, whether built-in or created.
import platform
x = dir(platform)
print(x)
Output:
['DEV_NULL', '_UNIXCONFDIR', 'WIN32_CLIENT_RELEASES', 'WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package __', '__spec__', '__version__', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_perse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_aliases', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']
Using the from Keyword
To import only parts from a module, use the from keyword.
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "Johnny",
"age": 12
}
person2 = {
"name": "Marty",
"age": 57
}
from myModule import person1
print(person["age"])
Output:
12
Python Notes:
- The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
- Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
- Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
- Python string methods return new values, and DO NOT change the original string
- Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
- Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
- As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)
- Python does not have built-in support for arrays, but Python lists can be used as pseudo “arrays”; therefore, all Python list methods will work with these pseudo “arrays”
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.