🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

as Keyword

AI & DATA SCIENCE // as-keyword

The as keyword renames an imported module or name (or an exception, or a context manager's result) to a different local name.

Syntax

import numpy as np
from collections import OrderedDict as OD
except ValueError as e:
with open(f) as file:

Deep Dive Course

as shows up in several unrelated Python constructs, always meaning 'bind this to a different, local name': import module as alias lets you refer to a module by a shorter or conflict-free name, like the widespread convention of aliasing numpy to np; from module import name as alias renames a specific imported name; except ExceptionType as e binds the caught exception object to e; and with expr as name binds whatever a context manager's __enter__ returns. In every case, as is just introducing a local binding for something on its left.

1Understanding as Keyword

as shows up in several unrelated Python constructs, always meaning 'bind this to a different, local name': import module as alias lets you refer to a module by a shorter or conflict-free name, like the widespread convention of aliasing numpy to np; from module import name as alias renames a specific imported name; except ExceptionType as e binds the caught exception object to e; and with expr as name binds whatever a context manager's __enter__ returns. In every case, as is just introducing a local binding for something on its left.

💡

Follow established community aliasing conventions, like aliasing numpy to np or pandas to pd, rather than inventing your own — consistent aliases make code instantly recognizable to anyone familiar with those libraries.

editor.html
import datetime as dt
now = dt.datetime(2026, 1, 1)
print(now.year)
localhost:3000

2Practical Example

Here is a real-world application of as Keyword showing how it is used in production Python code.

editor.html
try:
    import numpy as np
except ImportError as e:
    print(f"numpy not available: {e}")
    np = None
localhost:3000

3Best Practices

Follow these guidelines when working with as Keyword:

1. Use as to shorten long or commonly-typed module names, following widely recognized conventions where they exist

2. Use as to avoid a naming collision when two modules would otherwise export the same name

3. Give the exception variable in an except clause a short, conventional name like e, since it's typically used briefly right where it's caught

⚠️

Tip: Follow established community aliasing conventions, like aliasing numpy to np or pandas to pd, rather than inventing your own — consistent aliases make code instantly recognizable to anyone familiar with those libraries.

editor.html
import datetime as dt
now = dt.datetime(2026, 1, 1)
print(now.year)
localhost:3000

Examples

Example 01Basic Usage
import datetime as dt
now = dt.datetime(2026, 1, 1)
print(now.year)
Example 02Advanced Example
try:
    import numpy as np
except ImportError as e:
    print(f"numpy not available: {e}")
    np = None

Best Practices

  • Use as to shorten long or commonly-typed module names, following widely recognized conventions where they exist
  • Use as to avoid a naming collision when two modules would otherwise export the same name
  • Give the exception variable in an except clause a short, conventional name like e, since it's typically used briefly right where it's caught

Interview Question

What do the `as` used in imports, in except clauses, and in with statements all have in common, despite being three unrelated Python features?

Hint: Think about what `as` actually does grammatically in each case.

In all three, as introduces a local name bound to something that already exists on its left: the imported module or name, the exception object currently being handled, or whatever a context manager's __enter__ method returned. It's not a special feature of imports, exceptions, or context managers individually — it's a general piece of Python syntax for 'bind the thing on the left to this name', reused consistently across several different statements.

Exercises

MediumPractice using as Keyword in a real scenario.
View Solution
import datetime as dt
now = dt.datetime(2026, 1, 1)
print(now.year)

Frequently Asked Questions

What do the `as` used in imports, in except clauses, and in with statements all have in common, despite being three unrelated Python features?

In all three, as introduces a local name bound to something that already exists on its left: the imported module or name, the exception object currently being handled, or whatever a context manager's __enter__ method returned. It's not a special feature of imports, exceptions, or context managers individually — it's a general piece of Python syntax for 'bind the thing on the left to this name', reused consistently across several different statements.

Related Functions

import-statementfrom-importexcept-block