What does the `as` command do in Python 3.x?
Question: I've seen it many times but never understood what the `as` command does in
Python 3.x. Can you explain it in plain English?


Answer: It's not a command per se, it's a keyword used as part of the [`with`
statement](http://docs.python.org/2/reference/compound_stmts.html#the-with-
statement):

    
    
    with open("myfile.txt") as f:
        text = f.read()
    

The object after `as` gets assigned the result of the expression handled by
the `with` context manager.

Another use is to rename an imported module:

    
    
    import numpy as np
    

so you can use the name `np` instead of `numpy` from now on.

The third use is to give you access to an `Exception` object:

    
    
    try:
        f = open("foo")
    except IOError as exc:
        # Now you can access the Exception for more detailed analysis
    

