Four Import Forms
import math from math import sqrt from math import sqrt as root import math as m
What Happens on Import
- Python locates the module file on
sys.path. - Executes it top-to-bottom once.
- Caches the compiled
.pycin__pycache__/. - Binds the module object to a name in your namespace.
Your Own Module
Any .py file is a module. Put shared code in utils.py and import it:
# utils.py
def greet(name):
return f"Hi, {name}"
# main.py
from utils import greet
print(greet("Ming"))
Guarding Script Entry Points
def main():
...
if __name__ == "__main__":
main()
This lets the file be imported or run directly without re-executing main().
Common HKDSE Modules
math— sqrt, ceil, floor, pirandom— randint, choice, shuffledatetime— today, time deltas
Practise this on PyForm — free
PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.
Open PyForm →