as — Easy Examples
Creates an alias (used with import, with, except)
Aliasing with as
Renaming imports and exception variables.
python
# Import aliasing import math as m print(m.pi) print(m.sqrt(25)) from datetime import datetime as dt print(dt.now().year) # Exception aliasing try: result = 10 / 0 except ZeroDivisionError as e: print(f"Error: {e}") # Context manager aliasing class Resource: def __enter__(self): return {"status": "open"} def __exit__(self, *args): pass with Resource() as r: print(f"Resource: {r}")
'as' creates an alias — a new name for an imported module, caught exception, or context manager result. It makes code cleaner without extra assignment lines.
Want to try these examples interactively?
Open Easy Playground