Four Import Forms

import math
from math import sqrt
from math import sqrt as root
import math as m

What Happens on Import

  1. Python locates the module file on sys.path.
  2. Executes it top-to-bottom once.
  3. Caches the compiled .pyc in __pycache__/.
  4. 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

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 →