{"id":3948,"date":"2026-11-29T12:07:34","date_gmt":"2026-11-29T12:07:34","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3948"},"modified":"2026-11-29T12:07:34","modified_gmt":"2026-11-29T12:07:34","slug":"python-as","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-as\/","title":{"rendered":"Python as Keyword: Aliases for Modules, Exceptions and with Statements"},"content":{"rendered":"<p>The <code>as<\/code> keyword in Python creates an <strong>alias<\/strong>, a second, usually shorter, name for something. You will see it in three places: when importing modules (<code>import numpy as np<\/code>), when catching exceptions (<code>except ValueError as e<\/code>), and in <code>with<\/code> statements (<code>with open(...) as f<\/code>). This page explains each use with real outputs.<\/p>\n<h2>1. Giving a Module a Short Name<\/h2>\n<pre class=\"wp-block-code\"><code>import math as m\n\nprint(m.sqrt(16))\nprint(m.pi)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>4.0\n3.141592653589793<\/code><\/pre>\n<p>After <code>import math as m<\/code>, you use <code>m<\/code> instead of <code>math<\/code>. Note that the original name <code>math<\/code> is <em>not<\/em> available in this case; only the alias is.<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/THE-AS-KEYWORD-IN-PYTHONFOR-CREATING-ALIAS-FOR-MODULE.jpg\" alt=\"Python as keyword module alias example\" \/><\/figure>\n<h3>Popular conventions<\/h3>\n<p>Some aliases are so common that almost every Python programmer uses them. Following them makes your code easier for others to read:<\/p>\n<table>\n<thead>\n<tr>\n<th>Import<\/th>\n<th>Library<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>import numpy as np<\/code><\/td>\n<td>NumPy (numbers and arrays)<\/td>\n<\/tr>\n<tr>\n<td><code>import pandas as pd<\/code><\/td>\n<td>pandas (data tables)<\/td>\n<\/tr>\n<tr>\n<td><code>import matplotlib.pyplot as plt<\/code><\/td>\n<td>Matplotlib (charts)<\/td>\n<\/tr>\n<tr>\n<td><code>import seaborn as sns<\/code><\/td>\n<td>Seaborn (statistical charts)<\/td>\n<\/tr>\n<tr>\n<td><code>import tensorflow as tf<\/code><\/td>\n<td>TensorFlow (machine learning)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>2. Renaming a Single Imported Item<\/h2>\n<pre class=\"wp-block-code\"><code>from math import factorial as fact\nprint(fact(5))\n\nimport datetime as dt\nprint(dt.date(2026, 11, 29))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>120\n2026-11-29<\/code><\/pre>\n<p>This is useful when a name is long, or when two modules have functions with the same name and you need to tell them apart.<\/p>\n<h2>3. Catching an Exception Object<\/h2>\n<pre class=\"wp-block-code\"><code>try:\n    1 \/ 0\nexcept ZeroDivisionError as err:\n    print(\"Error:\", err)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Error: division by zero<\/code><\/pre>\n<p><code>as err<\/code> stores the exception object, so you can print its message or log it.<\/p>\n<h2>4. In with Statements<\/h2>\n<pre class=\"wp-block-code\"><code>with open(\"notes.txt\", \"w\") as f:\n    f.write(\"Hello\")<\/code><\/pre>\n<p>Here <code>as f<\/code> gives a name to the opened file. The <code>with<\/code> statement closes the file automatically when the block ends, even if an error occurs.<\/p>\n<h2>5. In match-case Patterns (Python 3.10+)<\/h2>\n<pre class=\"wp-block-code\"><code>match (3, 4):\n    case (x, y) as point:\n        print(point, x + y)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>(3, 4) 7<\/code><\/pre>\n<p>See <a href=\"https:\/\/gyanipandit.com\/programming\/python-structural-pattern-matching\/\">Python structural pattern matching<\/a> for more.<\/p>\n<h2>Good Practices<\/h2>\n<ul>\n<li>Use the standard aliases (<code>np<\/code>, <code>pd<\/code>, <code>plt<\/code>) rather than inventing your own.<\/li>\n<li>Do not create aliases that make code harder to understand, such as <code>import os as x<\/code>.<\/li>\n<li>Avoid aliases that hide built-in names, like <code>import math as list<\/code>.<\/li>\n<\/ul>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What does &#8220;as&#8221; do in Python?<\/h3>\n<p>It gives a new name (alias) to a module, an imported item, an exception, a <code>with<\/code> resource or a matched pattern.<\/p>\n<h3>Can I still use the original module name after &#8220;import math as m&#8221;?<\/h3>\n<p>No. Only <code>m<\/code> is defined. Write <code>import math<\/code> separately if you also need the full name.<\/p>\n<h3>Why do people write &#8220;import numpy as np&#8221;?<\/h3>\n<p>It is a widely followed convention that keeps code short and familiar to other programmers.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/accessing-tuple-elements-python\/\">Accessing tuple elements in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The as keyword in Python creates an alias, a second, usually shorter, name for something. You will see it in three places: when importing modules (import numpy as np), when catching exceptions (except ValueError as e), and in with statements (with open(&#8230;) as f). This page explains each use with real outputs. 1. Giving a &#8230; <a title=\"Python as Keyword: Aliases for Modules, Exceptions and with Statements\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-as\/\" aria-label=\"Read more about Python as Keyword: Aliases for Modules, Exceptions and with Statements\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3948","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3948"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3948\/revisions"}],"predecessor-version":[{"id":6167,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3948\/revisions\/6167"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}