as
KeywordPython 2.0+Beginner
Creates an alias (used with import, with, except)
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
Learn by Difficulty
Quick Example
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.
Try in PlaygroundTags
languagesyntaxcorealiasimportcontext-manager