182 lines
16 KiB
HTML
182 lines
16 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="generator" content="pandoc" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
||
<title>2.4 Importing Modules</title>
|
||
<style>
|
||
code{white-space: pre-wrap;}
|
||
span.smallcaps{font-variant: small-caps;}
|
||
span.underline{text-decoration: underline;}
|
||
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
||
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
||
ul.task-list{list-style: none;}
|
||
pre > code.sourceCode { white-space: pre; position: relative; }
|
||
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
|
||
pre > code.sourceCode > span:empty { height: 1.2em; }
|
||
code.sourceCode > span { color: inherit; text-decoration: inherit; }
|
||
div.sourceCode { margin: 1em 0; }
|
||
pre.sourceCode { margin: 0; }
|
||
@media screen {
|
||
div.sourceCode { overflow: auto; }
|
||
}
|
||
@media print {
|
||
pre > code.sourceCode { white-space: pre-wrap; }
|
||
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
|
||
}
|
||
pre.numberSource code
|
||
{ counter-reset: source-line 0; }
|
||
pre.numberSource code > span
|
||
{ position: relative; left: -4em; counter-increment: source-line; }
|
||
pre.numberSource code > span > a:first-child::before
|
||
{ content: counter(source-line);
|
||
position: relative; left: -1em; text-align: right; vertical-align: baseline;
|
||
border: none; display: inline-block;
|
||
-webkit-touch-callout: none; -webkit-user-select: none;
|
||
-khtml-user-select: none; -moz-user-select: none;
|
||
-ms-user-select: none; user-select: none;
|
||
padding: 0 4px; width: 4em;
|
||
color: #aaaaaa;
|
||
}
|
||
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
|
||
div.sourceCode
|
||
{ }
|
||
@media screen {
|
||
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
|
||
}
|
||
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
|
||
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
|
||
code span.at { color: #7d9029; } /* Attribute */
|
||
code span.bn { color: #40a070; } /* BaseN */
|
||
code span.bu { } /* BuiltIn */
|
||
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
|
||
code span.ch { color: #4070a0; } /* Char */
|
||
code span.cn { color: #880000; } /* Constant */
|
||
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
|
||
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
|
||
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
|
||
code span.dt { color: #902000; } /* DataType */
|
||
code span.dv { color: #40a070; } /* DecVal */
|
||
code span.er { color: #ff0000; font-weight: bold; } /* Error */
|
||
code span.ex { } /* Extension */
|
||
code span.fl { color: #40a070; } /* Float */
|
||
code span.fu { color: #06287e; } /* Function */
|
||
code span.im { } /* Import */
|
||
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
|
||
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
|
||
code span.op { color: #666666; } /* Operator */
|
||
code span.ot { color: #007020; } /* Other */
|
||
code span.pp { color: #bc7a00; } /* Preprocessor */
|
||
code span.sc { color: #4070a0; } /* SpecialChar */
|
||
code span.ss { color: #bb6688; } /* SpecialString */
|
||
code span.st { color: #4070a0; } /* String */
|
||
code span.va { color: #19177c; } /* Variable */
|
||
code span.vs { color: #4070a0; } /* VerbatimString */
|
||
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
|
||
</style>
|
||
<link rel="stylesheet" href="../tufte.css" />
|
||
<!--[if lt IE 9]>
|
||
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
|
||
<![endif]-->
|
||
</head>
|
||
<body>
|
||
<div style="display:none">
|
||
\(
|
||
\newcommand{\NOT}{\neg}
|
||
\newcommand{\AND}{\wedge}
|
||
\newcommand{\OR}{\vee}
|
||
\newcommand{\XOR}{\oplus}
|
||
\newcommand{\IMP}{\Rightarrow}
|
||
\newcommand{\IFF}{\Leftrightarrow}
|
||
\newcommand{\TRUE}{\text{True}\xspace}
|
||
\newcommand{\FALSE}{\text{False}\xspace}
|
||
\newcommand{\IN}{\,{\in}\,}
|
||
\newcommand{\NOTIN}{\,{\notin}\,}
|
||
\newcommand{\TO}{\rightarrow}
|
||
\newcommand{\DIV}{\mid}
|
||
\newcommand{\NDIV}{\nmid}
|
||
\newcommand{\MOD}[1]{\pmod{#1}}
|
||
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
|
||
\newcommand{\N}{\mathbb N}
|
||
\newcommand{\Z}{\mathbb Z}
|
||
\newcommand{\Q}{\mathbb Q}
|
||
\newcommand{\R}{\mathbb R}
|
||
\newcommand{\C}{\mathbb C}
|
||
\newcommand{\cA}{\mathcal A}
|
||
\newcommand{\cB}{\mathcal B}
|
||
\newcommand{\cC}{\mathcal C}
|
||
\newcommand{\cD}{\mathcal D}
|
||
\newcommand{\cE}{\mathcal E}
|
||
\newcommand{\cF}{\mathcal F}
|
||
\newcommand{\cG}{\mathcal G}
|
||
\newcommand{\cH}{\mathcal H}
|
||
\newcommand{\cI}{\mathcal I}
|
||
\newcommand{\cJ}{\mathcal J}
|
||
\newcommand{\cL}{\mathcal L}
|
||
\newcommand{\cK}{\mathcal K}
|
||
\newcommand{\cN}{\mathcal N}
|
||
\newcommand{\cO}{\mathcal O}
|
||
\newcommand{\cP}{\mathcal P}
|
||
\newcommand{\cQ}{\mathcal Q}
|
||
\newcommand{\cS}{\mathcal S}
|
||
\newcommand{\cT}{\mathcal T}
|
||
\newcommand{\cV}{\mathcal V}
|
||
\newcommand{\cW}{\mathcal W}
|
||
\newcommand{\cZ}{\mathcal Z}
|
||
\newcommand{\emp}{\emptyset}
|
||
\newcommand{\bs}{\backslash}
|
||
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
|
||
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
|
||
\newcommand{\abs}[1]{\left | #1 \right |}
|
||
\newcommand{\xspace}{}
|
||
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
|
||
\)
|
||
</div>
|
||
<header id="title-block-header">
|
||
<h1 class="title">2.4 Importing Modules</h1>
|
||
</header>
|
||
<section>
|
||
<p>So far we have learned about Python’s built-in functions and various data type methods. But these form a small fraction of all the functions that the Python programming language comes with. Python’s other functions (and even other data types) are separated into various <strong>modules</strong>, which is another name we give to Python code files. Unlike the functions and data types we’ve seen so far, these modules are not automatically loaded when run the Python interpreter, as they contain more specialized functions and data types. So in this section, we’re going to learn how to load one of these modules and use their definitions.</p>
|
||
<h2 id="the-import-statement">The <code>import</code> statement</h2>
|
||
<p>To load a Python module, we use a piece of code called an <strong>import statement</strong>, which has the following syntax:</p>
|
||
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="im">import</span> <span class="op"><</span>module_name<span class="op">></span></span></code></pre></div>
|
||
<p>For example, here is how we could load the <code>math</code> module in the Python console:</p>
|
||
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1"></a><span class="op">>>></span> <span class="im">import</span> math</span></code></pre></div>
|
||
<p>Like the other statements we’ve seen so far, import statements do not produce a value, but they do have an important effect. An <code>import</code> statement introduces a new variable (the name of the module being imported) that can be used to refer to all definitions from that module.</p>
|
||
<p>For example, the <code>math</code> module defines a function <code>log2</code> which computes the base-2 logarithm of a number. To access this function, we use dot notation:<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote"> This notation is the same as accessing data type methods, but <code>log2</code> is <em>not</em> a method. It’s a top-level function, just one that happens to be defined in the <code>math</code> module.</span></p>
|
||
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1"></a><span class="op">>>></span> math.log2(<span class="dv">1024</span>)</span>
|
||
<span id="cb3-2"><a href="#cb3-2"></a><span class="fl">10.0</span></span></code></pre></div>
|
||
<p>What other functions are contained in the <code>math</code> module? We’ll make use of a few other later in this course, but if you’re curious you can call the special built-in function <code>dir</code> on the the module (or any other module) to see a list of functions and other variables defined in the module:</p>
|
||
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1"></a><span class="op">>>></span> <span class="bu">dir</span>(math)</span>
|
||
<span id="cb4-2"><a href="#cb4-2"></a>[<span class="st">'__doc__'</span>, <span class="st">'__loader__'</span>, <span class="st">'__name__'</span>, <span class="st">'__package__'</span>, <span class="st">'__spec__'</span>, <span class="st">'acos'</span>, <span class="st">'acosh'</span>, <span class="st">'asin'</span>, <span class="st">'asinh'</span>, <span class="st">'atan'</span>, <span class="st">'atan2'</span>, <span class="st">'atanh'</span>, <span class="st">'ceil'</span>, <span class="st">'comb'</span>, <span class="st">'copysign'</span>, <span class="st">'cos'</span>, <span class="st">'cosh'</span>, <span class="st">'degrees'</span>, <span class="st">'dist'</span>, <span class="st">'e'</span>, <span class="st">'erf'</span>, <span class="st">'erfc'</span>, <span class="st">'exp'</span>, <span class="st">'expm1'</span>, <span class="st">'fabs'</span>, <span class="st">'factorial'</span>, <span class="st">'floor'</span>, <span class="st">'fmod'</span>, <span class="st">'frexp'</span>, <span class="st">'fsum'</span>, <span class="st">'gamma'</span>, <span class="st">'gcd'</span>, <span class="st">'hypot'</span>, <span class="st">'inf'</span>, <span class="st">'isclose'</span>, <span class="st">'isfinite'</span>, <span class="st">'isinf'</span>, <span class="st">'isnan'</span>, <span class="st">'isqrt'</span>, <span class="st">'ldexp'</span>, <span class="st">'lgamma'</span>, <span class="st">'log'</span>, <span class="st">'log10'</span>, <span class="st">'log1p'</span>, <span class="st">'log2'</span>, <span class="st">'modf'</span>, <span class="st">'nan'</span>, <span class="st">'perm'</span>, <span class="st">'pi'</span>, <span class="st">'pow'</span>, <span class="st">'prod'</span>, <span class="st">'radians'</span>, <span class="st">'remainder'</span>, <span class="st">'sin'</span>, <span class="st">'sinh'</span>, <span class="st">'sqrt'</span>, <span class="st">'tan'</span>, <span class="st">'tanh'</span>, <span class="st">'tau'</span>, <span class="st">'trunc'</span>]</span></code></pre></div>
|
||
<p>Ignoring the first few with the double underscore, we see some familiar looking names, like <code>ceil</code>, <code>floor</code>, <code>pi</code>, and <code>sin</code>. We’ve linked to the documentation for the <code>math</code> module in the <a href="#references">References</a> section below.</p>
|
||
<h2 id="the-datetime-module">The <code>datetime</code> module</h2>
|
||
<p>Python comes with far more modules than we’ll have time to learn about in this course. However, just to illustrate the breadth of these modules, we’ll briefly introduce one more that will be useful occasionally throughout the course.</p>
|
||
<p>The <code>datetime</code> module provides not just functions but new <em>data types</em> for representing time-based data. The first data type we’ll study here is <code>date</code>, which is a data type that represents a specific date.</p>
|
||
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1"></a><span class="op">>>></span> <span class="im">import</span> datetime</span>
|
||
<span id="cb5-2"><a href="#cb5-2"></a><span class="op">>>></span> canada_day <span class="op">=</span> datetime.date(<span class="dv">1867</span>, <span class="dv">7</span>, <span class="dv">1</span>) <span class="co"># Create a new date</span></span>
|
||
<span id="cb5-3"><a href="#cb5-3"></a><span class="op">>>></span> <span class="bu">type</span>(canada_day)</span>
|
||
<span id="cb5-4"><a href="#cb5-4"></a><span class="op"><</span><span class="kw">class</span> <span class="st">'datetime.date'</span><span class="op">></span></span>
|
||
<span id="cb5-5"><a href="#cb5-5"></a><span class="op">>>></span> term_start <span class="op">=</span> datetime.date(<span class="dv">2020</span>, <span class="dv">9</span>, <span class="dv">10</span>)</span>
|
||
<span id="cb5-6"><a href="#cb5-6"></a><span class="op">>>></span> datetime.date.weekday(term_start) <span class="co"># Return the day of the week of the date</span></span>
|
||
<span id="cb5-7"><a href="#cb5-7"></a><span class="dv">3</span> <span class="co"># 0 = Monday, 1 = Tuesday, etc.</span></span></code></pre></div>
|
||
<p>Note the double use of dot notation in that last expression. <code>datetime.date</code> is the data type being accessed, and <code>.weekday</code> accesses a method of that data type.</p>
|
||
<p>We can compare dates for equality using <code>==</code> and chronological order (e.g., <code><</code> for comparing one date comes before another). We can also subtract dates, which is pretty cool:</p>
|
||
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1"></a><span class="op">>>></span> term_start <span class="op">-</span> canada_day</span>
|
||
<span id="cb6-2"><a href="#cb6-2"></a>datetime.timedelta(days<span class="op">=</span><span class="dv">55954</span>)</span></code></pre></div>
|
||
<p>The difference between two dates is an instance of the <code>datetime.timedelta</code> data type, which is used to represent an interval of time. What the above expression tells us is that 55,954 days have passed between the first day of the fall semester and the day of Canada’s confederation.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> Fun fact: Canada’s confederation first consisted of only four provinces: Ontario, Quebec, Nova Scotia, and New Brunswick.</span></p>
|
||
<h2 id="theres-a-lot-of-python-out-theredont-worry">There’s a lot of Python out there—don’t worry!</h2>
|
||
<p>Up to this point, we’ve covered several different data types, functions, methods, and now modules in Python. It might be starting to feel a bit daunting, and we wanted to take a moment to pause and look at the bigger picture. Our goal in showing you these elements of Python is not to overwhelm you, but instead to give you a taste of the language’s powerful computational capabilities. But this course is not about memorizing different functions, data types, and modules in Python! All throughout this course, you’ll have access to references and documentation that describe the functionality of these different elements, and will have lots of opportunities to practice using them. For now, all we want you to know is simply that these capabilities exist, how to experiment with them in the Python console, and how to look up information about them.</p>
|
||
<h2 id="references">References</h2>
|
||
<ul>
|
||
<li><a href="https://docs.python.org/3/library/datetime.html"><code>datetime</code> module documentation</a></li>
|
||
<li><a href="https://docs.python.org/3/library/math.html"><code>math</code> module documentation</a></li>
|
||
</ul>
|
||
</section>
|
||
<footer>
|
||
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
|
||
</footer>
|
||
</body>
|
||
</html>
|