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 trialJHON mical
Python Development Techdegree Student 51 Pointsfor 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
Treehouse Moderator 68,454 PointsHey JHON mical, if you are referring to the as
keyword used in the following cases:
-
import
moduleas
identifier [docs] -
try:
suiteexcept
errorTypeas
identifier:
[docs] -
with
expressionas
target:
[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"
- example:
- for
try/except
statement, theidentifier
points to the error object containing the raised error. This allows accessing attributes of the error.- example,
except ValueError as err:
then allowsprint(err)
- example,
- 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:
allowsf
to be used as an alias for the filehandle returned by theopen
statement
- example
Post back if you need more help. Good luck!!
Megan Amendola
Treehouse TeacherMegan Amendola
Treehouse TeacherHi JHON mical ! Can you tell us more about this issue you're having? Is this about a course or code challenge?