Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

for what as is use i do not understand in which situation i have to use

for what as is use i do not understand in which situation i have to use

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

Hey JHON mical, if you are referring to the as keyword used in the following cases:

  • importmoduleasidentifier [docs]
  • try:suiteexcepterrorTypeasidentifier: [docs]
  • withexpressionastarget: [docs]

In each of these statements, as is used to create an alias to an object:

  • for import, the identifier points to the same module.
    • example: import numpy as np allows using "np" instead of the longer name "numpy"
  • for try/except statement, the identifier points to the error object containing the raised error. This allows accessing attributes of the error.
    • example, except ValueError as err: then allows print(err)
  • for with statements, the target points to the object returned by the expression. This might be a filehandle.
    • example with open("my_data", "r") as f: allows f to be used as an alias for the filehandle returned by the open statement

Post back if you need more help. Good luck!!