~charles/cdaniels.net

b624c5aa39962f12a9d4c6a8cf3a5b9c04d21d55 — Charles Daniels a month ago fc66997
add intro to python talk
A src/assets/talks/intro_to_python/Introduction to Python.tgz => src/assets/talks/intro_to_python/Introduction to Python.tgz +0 -0

A src/assets/talks/intro_to_python/notes.pdf => src/assets/talks/intro_to_python/notes.pdf +0 -0

A src/assets/talks/intro_to_python/slides.html => src/assets/talks/intro_to_python/slides.html +445 -0
@@ 0,0 1,445 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="generator" content="pandoc" />
  <meta name="author" content="Charles Daniels" />
  <meta name="date" content="2020-09-17" />
  <title>Introduction to Python</title>
  <style type="text/css">
      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%;}
  </style>
  <style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; 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 {
a.sourceLine::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" type="text/css" media="screen, projection, print"
    href="https://www.w3.org/Talks/Tools/Slidy2/styles/slidy.css" />
  <script src="https://www.w3.org/Talks/Tools/Slidy2/scripts/slidy.js"
    charset="utf-8" type="text/javascript"></script>
</head>
<body>
<div class="slide titlepage">
  <h1 class="title">Introduction to Python</h1>
  <p class="author">
Charles Daniels
  </p>
  <p class="date">September 17, 2020</p>
</div>
<div id="introduction" class="title-slide slide section level1"><h1>Introduction</h1></div><div id="who-am-i" class="slide section level2">
<h1>Who Am I?</h1>
<ul>
<li>Charles Daniels</li>
<li>PhD CS student</li>
<li><a href="https://cse.sc.edu/~jbakos/group/">HeRC research group</a></li>
<li>B.S.E in Computer Engineering from USC</li>
<li>TA for 313, 317, 611</li>
</ul>
</div><div id="why-learn-python" class="slide section level2">
<h1>Why Learn Python?</h1>
<ul>
<li>Easy to write
<ul>
<li>Much less boilerplate than Java</li>
<li>No need for manual memory management like C/C++</li>
</ul></li>
<li>Popular
<ul>
<li>Widely used in scientific computing and industry</li>
</ul></li>
<li>Huge, mature library ecosystem
<ul>
<li>Numpy/Scipy</li>
<li>Matplotlib</li>
<li>PIL</li>
<li>many, many more</li>
</ul></li>
</ul>
</div><div id="how-do-i-use-python" class="slide section level2">
<h1>How Do I Use Python?</h1>
<p>I use Python all the time in my work as a graduate student…</p>
<ul>
<li>Data processing scripts
<ul>
<li>Convert from one format to another</li>
<li>Summarize or gather statistics</li>
</ul></li>
<li>Create figures</li>
<li>Automate other programs</li>
<li>Create prototypes for systems to be lowered to C/hardware later</li>
</ul>
</div><div id="python-essentials" class="slide section level2">
<h1>Python Essentials</h1>
<ul>
<li>Interpreted, not compiled</li>
<li>Duck-typed</li>
<li>Object-oriented</li>
<li>Garbage-collected</li>
</ul>
</div>
<div id="the-basics" class="title-slide slide section level1"><h1>The Basics</h1></div><div id="want-to-follow-along" class="slide section level2">
<h1>Want to Follow Along?</h1>
<ul>
<li>Just open a terminal and run <code>python3</code></li>
<li>Or use <a href="https://repl.it/">repl.it</a></li>
<li>Or use <code>ipython3</code> if you feel fancy</li>
</ul>
</div><div id="syntax---variables" class="slide section level2">
<h1>Syntax - Variables</h1>
<p>Python syntax is different from C and Java…</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb1-1" title="1"><span class="co"># assign a new variable x</span></a>
<a class="sourceLine" id="cb1-2" title="2">x <span class="op">=</span> <span class="dv">7</span></a>
<a class="sourceLine" id="cb1-3" title="3">y <span class="op">=</span> <span class="dv">3</span></a>
<a class="sourceLine" id="cb1-4" title="4"><span class="co"># print x^y</span></a>
<a class="sourceLine" id="cb1-5" title="5"><span class="bu">print</span>(<span class="st">&quot;</span><span class="sc">{}</span><span class="st"> to the power of </span><span class="sc">{}</span><span class="st">=</span><span class="sc">{}</span><span class="st">&quot;</span>.<span class="bu">format</span>(x, y, x<span class="op">**</span>y))</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>7 to the power of 3=343</p>
</blockquote>
</blockquote>
</div><div id="syntax-loops" class="slide section level2">
<h1>Syntax – Loops</h1>
<div class="sourceCode" id="cb2"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" title="1">x <span class="op">=</span> <span class="dv">2</span></a>
<a class="sourceLine" id="cb2-2" title="2"><span class="cf">while</span> x <span class="op">&gt;</span> <span class="dv">0</span>:</a>
<a class="sourceLine" id="cb2-3" title="3">  <span class="bu">print</span>(<span class="st">&quot;x is &quot;</span>, x)</a>
<a class="sourceLine" id="cb2-4" title="4">  x <span class="op">-=</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb2-5" title="5"></a>
<a class="sourceLine" id="cb2-6" title="6"><span class="cf">for</span> y <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">0</span>, <span class="dv">3</span>):</a>
<a class="sourceLine" id="cb2-7" title="7">  <span class="bu">print</span>(<span class="st">&quot;y is &quot;</span>, y)</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>x is 2</p>
<p>x is 1</p>
<p>y is 0</p>
<p>y is 1</p>
<p>y is 2</p>
</blockquote>
</blockquote>
</div><div id="syntax-functions" class="slide section level2">
<h1>Syntax – Functions</h1>
<p>Defining a function…</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" title="1"><span class="kw">def</span> doubleit(x):</a>
<a class="sourceLine" id="cb3-2" title="2">  <span class="cf">return</span> x <span class="op">*</span> <span class="dv">2</span></a>
<a class="sourceLine" id="cb3-3" title="3"></a>
<a class="sourceLine" id="cb3-4" title="4"><span class="co"># here, message has a default value</span></a>
<a class="sourceLine" id="cb3-5" title="5"><span class="kw">def</span> sayit(x, message<span class="op">=</span><span class="st">&quot;value is: &quot;</span>):</a>
<a class="sourceLine" id="cb3-6" title="6">  <span class="bu">print</span>(message, x)</a>
<a class="sourceLine" id="cb3-7" title="7"></a>
<a class="sourceLine" id="cb3-8" title="8"><span class="bu">print</span>(<span class="st">&quot;doubleit(3)=&quot;</span>, doubleit(<span class="dv">3</span>))</a>
<a class="sourceLine" id="cb3-9" title="9">sayit(<span class="dv">5</span>)</a>
<a class="sourceLine" id="cb3-10" title="10">sayit(<span class="dv">5</span>, <span class="st">&quot;different message!&quot;</span>)</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>doubleit(3)= 6</p>
<p>value is: 5</p>
<p>different message! 5</p>
</blockquote>
</blockquote>
</div><div id="syntax-classes-1" class="slide section level2">
<h1>Syntax – Classes 1</h1>
<p>Defining a class…</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" title="1"><span class="kw">class</span> Dog:</a>
<a class="sourceLine" id="cb4-2" title="2">  <span class="co"># __init__ is the constructor, the first argument</span></a>
<a class="sourceLine" id="cb4-3" title="3">  <span class="co"># doesn&#39;t *have* to be &quot;this&quot;, this is just a</span></a>
<a class="sourceLine" id="cb4-4" title="4">  <span class="co"># convention (&quot;self&quot; is also popular)</span></a>
<a class="sourceLine" id="cb4-5" title="5">  <span class="co">#</span></a>
<a class="sourceLine" id="cb4-6" title="6">  <span class="co"># __init__ is defined like any other function, this</span></a>
<a class="sourceLine" id="cb4-7" title="7">  <span class="co"># time we use default values</span></a>
<a class="sourceLine" id="cb4-8" title="8">  <span class="kw">def</span> <span class="fu">__init__</span>(this, fleas<span class="op">=</span><span class="dv">5</span>, greeting<span class="op">=</span><span class="st">&quot;bark&quot;</span>):</a>
<a class="sourceLine" id="cb4-9" title="9">    this.fleas <span class="op">=</span> fleas</a>
<a class="sourceLine" id="cb4-10" title="10">    this.greeting <span class="op">=</span> greeting</a>
<a class="sourceLine" id="cb4-11" title="11"></a>
<a class="sourceLine" id="cb4-12" title="12">  <span class="kw">def</span> bark(this):</a>
<a class="sourceLine" id="cb4-13" title="13">    <span class="bu">print</span>(this.greeting)</a></code></pre></div>
</div><div id="syntax-classes-2" class="slide section level2">
<h1>Syntax – Classes 2</h1>
<p>Using our class…</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" title="1">fido <span class="op">=</span> Dog()</a>
<a class="sourceLine" id="cb5-2" title="2"><span class="co"># single quotes are also allowed for strings</span></a>
<a class="sourceLine" id="cb5-3" title="3">spot <span class="op">=</span> Dog(<span class="dv">3</span>, <span class="st">&#39;woof&#39;</span>)</a>
<a class="sourceLine" id="cb5-4" title="4">doge <span class="op">=</span> Dog(greeting<span class="op">=</span><span class="st">&quot;wow, such class, very types&quot;</span>)</a>
<a class="sourceLine" id="cb5-5" title="5"></a>
<a class="sourceLine" id="cb5-6" title="6"><span class="co"># create a list with the dogs in it</span></a>
<a class="sourceLine" id="cb5-7" title="7">dogs <span class="op">=</span> [fido, spot, doge]</a>
<a class="sourceLine" id="cb5-8" title="8"></a>
<a class="sourceLine" id="cb5-9" title="9"><span class="co"># loop over it</span></a>
<a class="sourceLine" id="cb5-10" title="10"><span class="cf">for</span> dog <span class="kw">in</span> dogs:</a>
<a class="sourceLine" id="cb5-11" title="11">  dog.bark()</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>bark</p>
<p>woof</p>
<p>wow, such class, very types</p>
</blockquote>
</blockquote>
</div><div id="imports" class="slide section level2">
<h1>Imports</h1>
<p>Some functions, such as <span class="math inline">sin ()</span> are in <em>modules</em> which we must import before we can use them. <span class="math inline">sin ()</span> lives in the <code>math</code> module.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" title="1"><span class="im">import</span> math</a>
<a class="sourceLine" id="cb6-2" title="2"></a>
<a class="sourceLine" id="cb6-3" title="3"><span class="bu">print</span>(<span class="st">&quot;pi = &quot;</span>, math.pi)</a>
<a class="sourceLine" id="cb6-4" title="4"><span class="bu">print</span>(<span class="st">&quot;sin(1.5*pi) = &quot;</span>, math.sin(<span class="fl">1.5</span><span class="op">*</span>math.pi))</a>
<a class="sourceLine" id="cb6-5" title="5"></a>
<a class="sourceLine" id="cb6-6" title="6"><span class="co"># we can also import specific items from a module</span></a>
<a class="sourceLine" id="cb6-7" title="7"><span class="im">from</span> math <span class="im">import</span> sin</a>
<a class="sourceLine" id="cb6-8" title="8"><span class="bu">print</span>(<span class="st">&quot;sin(2.5*pi) = &quot;</span>, sin(<span class="fl">2.5</span><span class="op">*</span>math.pi))</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>pi = 3.141592653589793</p>
<p>sin(1.5*pi) = -1.0</p>
<p>sin(2.5*pi) = 1.0</p>
</blockquote>
</blockquote>
</div><div id="duck-typing" class="slide section level2">
<h1>Duck Typing</h1>
<div class="sourceCode" id="cb7"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" title="1"><span class="kw">class</span> Duck:</a>
<a class="sourceLine" id="cb7-2" title="2">  <span class="kw">def</span> quack(this):</a>
<a class="sourceLine" id="cb7-3" title="3">    <span class="bu">print</span>(<span class="st">&quot;Quack quack!&quot;</span>)</a>
<a class="sourceLine" id="cb7-4" title="4"></a>
<a class="sourceLine" id="cb7-5" title="5"><span class="kw">class</span> Goose:</a>
<a class="sourceLine" id="cb7-6" title="6">  <span class="kw">def</span> quack(this):</a>
<a class="sourceLine" id="cb7-7" title="7">    <span class="bu">print</span>(<span class="st">&quot;Hong honk!&quot;</span>)</a>
<a class="sourceLine" id="cb7-8" title="8"></a>
<a class="sourceLine" id="cb7-9" title="9">duck <span class="op">=</span> Duck()</a>
<a class="sourceLine" id="cb7-10" title="10">goose <span class="op">=</span> Goose()</a>
<a class="sourceLine" id="cb7-11" title="11"><span class="cf">for</span> bird <span class="kw">in</span> [duck, goose]:</a>
<a class="sourceLine" id="cb7-12" title="12">  bird.quack()</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>Quack quack!</p>
<p>Hong honk!</p>
</blockquote>
</blockquote>
</div><div id="input-file" class="slide section level2">
<h1>Input – File</h1>
<div class="sourceCode" id="cb8"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" title="1"></a>
<a class="sourceLine" id="cb8-2" title="2"><span class="co"># open example.txt for reading, the &quot;with&quot; will</span></a>
<a class="sourceLine" id="cb8-3" title="3"><span class="co"># cause the file to be closed automatically when we</span></a>
<a class="sourceLine" id="cb8-4" title="4"><span class="co"># reach the end of the &quot;with&quot; block, so we don&#39;t</span></a>
<a class="sourceLine" id="cb8-5" title="5"><span class="co"># have to call f.close()</span></a>
<a class="sourceLine" id="cb8-6" title="6"><span class="cf">with</span> <span class="bu">open</span>(<span class="st">&quot;example.txt&quot;</span>, <span class="st">&quot;r&quot;</span>) <span class="im">as</span> f:</a>
<a class="sourceLine" id="cb8-7" title="7">  lineno <span class="op">=</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb8-8" title="8">  <span class="cf">for</span> line <span class="kw">in</span> f:</a>
<a class="sourceLine" id="cb8-9" title="9">    <span class="bu">print</span>(<span class="st">&quot;line&quot;</span>, lineno, <span class="st">&quot;is&quot;</span>, line)</a>
<a class="sourceLine" id="cb8-10" title="10">    lineno <span class="op">+=</span> <span class="dv">1</span></a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>line 0 is line 1</p>
<p>line 1 is line two</p>
<p>line 2 is this is the third line</p>
</blockquote>
</blockquote>
</div><div id="input-standard-in" class="slide section level2">
<h1>Input – Standard In</h1>
<p>This example shows how to loop over all the lines of standard input…</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" title="1"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb9-2" title="2"></a>
<a class="sourceLine" id="cb9-3" title="3">lineno <span class="op">=</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb9-4" title="4"><span class="cf">for</span> line <span class="kw">in</span> sys.stdin:</a>
<a class="sourceLine" id="cb9-5" title="5">  <span class="bu">print</span>(<span class="st">&quot;line&quot;</span>, lineno, <span class="st">&quot;is&quot;</span>, line)</a>
<a class="sourceLine" id="cb9-6" title="6">  lineno <span class="op">+=</span> <span class="dv">1</span></a></code></pre></div>
</div><div id="output-file" class="slide section level2">
<h1>Output – File</h1>
<div class="sourceCode" id="cb10"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" title="1"><span class="cf">with</span> <span class="bu">open</span>(<span class="st">&quot;output.txt&quot;</span>, <span class="st">&quot;w&quot;</span>) <span class="im">as</span> f:</a>
<a class="sourceLine" id="cb10-2" title="2">  <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">5</span>):</a>
<a class="sourceLine" id="cb10-3" title="3">    f.write(<span class="st">&quot;line #</span><span class="sc">{}</span><span class="ch">\n</span><span class="st">&quot;</span>.<span class="bu">format</span>(i))</a>
<a class="sourceLine" id="cb10-4" title="4"><span class="cf">with</span> <span class="bu">open</span>(<span class="st">&quot;output.txt&quot;</span>, <span class="st">&quot;r&quot;</span>) <span class="im">as</span> f:</a>
<a class="sourceLine" id="cb10-5" title="5">  <span class="cf">for</span> line <span class="kw">in</span> f:</a>
<a class="sourceLine" id="cb10-6" title="6">    <span class="bu">print</span>(line)</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>line #0</p>
<p>line #1</p>
<p>line #2</p>
<p>line #3</p>
<p>line #4</p>
</blockquote>
</blockquote>
</div>
<div id="getting-fancy" class="title-slide slide section level1"><h1>Getting Fancy</h1></div><div id="list-comprehensions-map" class="slide section level2">
<h1>List Comprehensions (Map)</h1>
<p>For a list <span class="math inline"><em>L</em></span>, apply a function <span class="math inline"><em>f</em></span> to each item, creating a new list <span class="math inline"><em>L</em>′</span> such that <span class="math inline"><em>L</em>′[<em>i</em>] = <em>f</em>(<em>L</em>[<em>i</em>])∀<em>i</em></span>.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" title="1">numbers <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</a>
<a class="sourceLine" id="cb11-2" title="2">squared <span class="op">=</span> [x<span class="op">*</span>x <span class="cf">for</span> x <span class="kw">in</span> numbers]</a>
<a class="sourceLine" id="cb11-3" title="3"><span class="bu">print</span>(<span class="st">&quot;squared=&quot;</span>, squared)</a>
<a class="sourceLine" id="cb11-4" title="4"></a>
<a class="sourceLine" id="cb11-5" title="5"><span class="co"># convert a string to a list of it&#39;s ASCII codes</span></a>
<a class="sourceLine" id="cb11-6" title="6">s <span class="op">=</span> <span class="st">&quot;Hello!&quot;</span></a>
<a class="sourceLine" id="cb11-7" title="7"><span class="bu">print</span>(<span class="st">&quot;characters=&quot;</span>, [<span class="bu">ord</span>(c) <span class="cf">for</span> c <span class="kw">in</span> s])</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>squared= [1, 4, 9, 16]</p>
<p>characters= [72, 101, 108, 108, 111, 33]</p>
</blockquote>
</blockquote>
</div><div id="setting-up-to-plot" class="slide section level2">
<h1>Setting up to Plot</h1>
<p>Code taken from <a href="https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/simple_plot.html">matplotlib.org</a>.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" title="1"><span class="im">import</span> matplotlib</a>
<a class="sourceLine" id="cb12-2" title="2"><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</a>
<a class="sourceLine" id="cb12-3" title="3"><span class="im">import</span> numpy <span class="im">as</span> np</a>
<a class="sourceLine" id="cb12-4" title="4"></a>
<a class="sourceLine" id="cb12-5" title="5"></a>
<a class="sourceLine" id="cb12-6" title="6"><span class="co"># Data for plotting</span></a>
<a class="sourceLine" id="cb12-7" title="7">t <span class="op">=</span> np.arange(<span class="fl">0.0</span>, <span class="fl">2.0</span>, <span class="fl">0.01</span>)</a>
<a class="sourceLine" id="cb12-8" title="8">s <span class="op">=</span> <span class="dv">1</span> <span class="op">+</span> np.sin(<span class="dv">2</span> <span class="op">*</span> np.pi <span class="op">*</span> t)</a></code></pre></div>
</div><div id="plot-the-data" class="slide section level2">
<h1>Plot the Data</h1>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb13-1" title="1">fig, ax <span class="op">=</span> plt.subplots()</a>
<a class="sourceLine" id="cb13-2" title="2"></a>
<a class="sourceLine" id="cb13-3" title="3">ax.plot(t, s)</a>
<a class="sourceLine" id="cb13-4" title="4">ax.<span class="bu">set</span>(xlabel<span class="op">=</span><span class="st">&#39;time (s)&#39;</span>, ylabel<span class="op">=</span><span class="st">&#39;voltage (mV)&#39;</span>,</a>
<a class="sourceLine" id="cb13-5" title="5">       title<span class="op">=</span><span class="st">&#39;About as simple as it gets, folks&#39;</span>)</a>
<a class="sourceLine" id="cb13-6" title="6">ax.grid()</a>
<a class="sourceLine" id="cb13-7" title="7"></a>
<a class="sourceLine" id="cb13-8" title="8"><span class="co"># un-comment to save out to a file</span></a>
<a class="sourceLine" id="cb13-9" title="9"><span class="co"># fig.savefig(&quot;test.png&quot;)</span></a>
<a class="sourceLine" id="cb13-10" title="10"></a>
<a class="sourceLine" id="cb13-11" title="11"><span class="co"># un-comment to show GUI plot window</span></a>
<a class="sourceLine" id="cb13-12" title="12"><span class="co"># plt.show()</span></a></code></pre></div>
</div><div id="the-result" class="slide section level2">
<h1>The Result…</h1>
<p><embed src="./plot.pdf" /></p>
</div><div id="attributes-arent-pre-declared" class="slide section level2">
<h1>Attributes Aren’t Pre-Declared</h1>
<p>Remember our <code>class Dog</code> from earlier? This technique is great for annotating objects you didn’t instantiate (but be careful to avoid name collisions)</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode python run"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" title="1">fido <span class="op">=</span> Dog()</a>
<a class="sourceLine" id="cb14-2" title="2">fido.name <span class="op">=</span> <span class="st">&quot;Fido&quot;</span></a>
<a class="sourceLine" id="cb14-3" title="3">fido.bark()</a>
<a class="sourceLine" id="cb14-4" title="4"><span class="bu">print</span>(fido.name)</a></code></pre></div>
<blockquote>
<p>Output:</p>
<blockquote>
<p>bark</p>
<p>Fido</p>
</blockquote>
</blockquote>
</div>
<div id="what-next" class="title-slide slide section level1"><h1>What Next?</h1></div><div id="libraries" class="slide section level2">
<h1>Libraries</h1>
<ul>
<li>Numerical computing
<ul>
<li><a href="https://numpy.org/">NumPy</a></li>
<li><a href="https://www.scipy.org/">SciPy</a></li>
</ul></li>
<li>Plots
<ul>
<li><a href="https://matplotlib.org/">matplotlib</a></li>
</ul></li>
<li>GUIs
<ul>
<li><a href="https://docs.python.org/3.7/library/tkinter.html">tkinter</a>
<ul>
<li><a href="http://cdaniels.net/talks.html#py3_tk">shameless plug</a></li>
</ul></li>
</ul></li>
<li>Argument Parsing
<ul>
<li><a href="https://docs.python.org/3.7/library/argparse.html">argarse</a></li>
</ul></li>
</ul>
</div><div id="how-to-install-them" class="slide section level2">
<h1>How to Install Them</h1>
<ul>
<li><code>pip</code> will let you install Python modules from the internet.
<ul>
<li>Official docs on <a href="https://packaging.python.org/tutorials/installing-packages/">python.org</a></li>
</ul></li>
<li><p>Search packages: <code>pip3 search searchterm</code></p></li>
<li>Install a package <code>pip3 install --user packagename</code>
<ul>
<li>Don’t install globally with <code>sudo pip install</code> unless you know what you are doing.</li>
</ul></li>
<li>Find packages on <a href="https://pypi.org/">pypi.org</a>.
<ul>
<li>Also try <a href="https://awesome-python.com/">awesome-python.com</a></li>
</ul></li>
</ul>
</div><div id="questions" class="slide section level2">
<h1>Questions?</h1>
</div><div id="end." class="slide section level2">
<h1>End.</h1>
</div><div id="thanks" class="slide section level2">
<h1>Thanks</h1>
<ul>
<li><p>This slideshow was written using <code>pandoc</code> with <a href="https://github.com/caiofcm/filter_pandoc_run_py">caiofcm/filter_pandoc_run_py</a> used to execute in-line Python code and embed the output.</p></li>
<li><p>Thanks to Josh for copyediting.</p></li>
</ul>
</div>
</body>
</html>

A src/assets/talks/intro_to_python/slides.md => src/assets/talks/intro_to_python/slides.md +317 -0
@@ 0,0 1,317 @@
% Introduction to Python
% Charles Daniels
% September 17, 2020

# Introduction

## Who Am I?

* Charles Daniels
* PhD CS student
* [HeRC research group](https://cse.sc.edu/~jbakos/group/)
* B.S.E in Computer Engineering from USC
* TA for 313, 317, 611

## Why Learn Python?

* Easy to write
  * Much less boilerplate than Java
  * No need for manual memory management like C/C++
* Popular
  * Widely used in scientific computing and industry
* Huge, mature library ecosystem
  * Numpy/Scipy
  * Matplotlib
  * PIL
  * many, many more

## How Do I Use Python?

I use Python all the time in my work as a graduate student...

* Data processing scripts
  * Convert from one format to another
  * Summarize or gather statistics
* Create figures
* Automate other programs
* Create prototypes for systems to be lowered to C/hardware later

## Python Essentials

* Interpreted, not compiled
* Duck-typed
* Object-oriented
* Garbage-collected

# The Basics

## Want to Follow Along?

* Just open a terminal and run `python3`
* Or use [repl.it](https://repl.it/)
* Or use `ipython3` if you feel fancy

## Syntax - Variables

Python syntax is different from C and Java...

```{.python .run}
# assign a new variable x
x = 7
y = 3
# print x^y
print("{} to the power of {}={}".format(x, y, x**y))
```

## Syntax -- Loops

```{.python .run}
x = 2
while x > 0:
  print("x is ", x)
  x -= 1

for y in range(0, 3):
  print("y is ", y)
```

## Syntax -- Functions

Defining a function…

```{.python .run}
def doubleit(x):
  return x * 2

# here, message has a default value
def sayit(x, message="value is: "):
  print(message, x)

print("doubleit(3)=", doubleit(3))
sayit(5)
sayit(5, "different message!")
```

## Syntax -- Classes 1

Defining a class...

```{.python .run}
class Dog:
  # __init__ is the constructor, the first argument
  # doesn't *have* to be "this", this is just a
  # convention ("self" is also popular)
  #
  # __init__ is defined like any other function, this
  # time we use default values
  def __init__(this, fleas=5, greeting="bark"):
    this.fleas = fleas
    this.greeting = greeting

  def bark(this):
    print(this.greeting)
```


## Syntax -- Classes 2

Using our class...


```{.python .run}
fido = Dog()
# single quotes are also allowed for strings
spot = Dog(3, 'woof')
doge = Dog(greeting="wow, such class, very types")

# create a list with the dogs in it
dogs = [fido, spot, doge]

# loop over it
for dog in dogs:
  dog.bark()
```

## Imports

Some functions, such as $\sin()$ are in *modules* which we must import before
we can use them. $\sin()$ lives in the `math` module.

```{.python .run}
import math

print("pi = ", math.pi)
print("sin(1.5*pi) = ", math.sin(1.5*math.pi))

# we can also import specific items from a module
from math import sin
print("sin(2.5*pi) = ", sin(2.5*math.pi))
```

## Duck Typing

```{.python .run}
class Duck:
  def quack(this):
    print("Quack quack!")

class Goose:
  def quack(this):
    print("Hong honk!")

duck = Duck()
goose = Goose()
for bird in [duck, goose]:
  bird.quack()
```

## Input -- File

```{.python .run}

# open example.txt for reading, the "with" will
# cause the file to be closed automatically when we
# reach the end of the "with" block, so we don't
# have to call f.close()
with open("example.txt", "r") as f:
  lineno = 0
  for line in f:
    print("line", lineno, "is", line)
    lineno += 1

```

## Input -- Standard In

This example shows how to loop over all the lines of standard input…

```python
import sys

lineno = 0
for line in sys.stdin:
  print("line", lineno, "is", line)
  lineno += 1

```

## Output -- File

```{.python .run}
with open("output.txt", "w") as f:
  for i in range(5):
    f.write("line #{}\n".format(i))
with open("output.txt", "r") as f:
  for line in f:
    print(line)
```

# Getting Fancy

## List Comprehensions (Map)

For a list $L$, apply a function $f$ to each item,
creating a new list $L'$ such that $L'[i]=f(L[i])
\forall i$.

```{.python .run}
numbers = [1, 2, 3, 4]
squared = [x*x for x in numbers]
print("squared=", squared)

# convert a string to a list of it's ASCII codes
s = "Hello!"
print("characters=", [ord(c) for c in s])
```

## Setting up to Plot

Code taken from
[matplotlib.org](https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/simple_plot.html).

```python
import matplotlib
import matplotlib.pyplot as plt
import numpy as np


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
```

## Plot the Data

```python
fig, ax = plt.subplots()

ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

# un-comment to save out to a file
# fig.savefig("test.png")

# un-comment to show GUI plot window
# plt.show()
```

## The Result...

![](./plot.pdf)

## Attributes Aren't Pre-Declared

Remember our `class Dog` from earlier? This
technique is great for annotating objects you
didn't instantiate (but be careful to avoid name
collisions)

```{.python .run}
fido = Dog()
fido.name = "Fido"
fido.bark()
print(fido.name)
```

# What Next?

## Libraries

* Numerical computing
  * [NumPy](https://numpy.org/)
  * [SciPy](https://www.scipy.org/)
* Plots
  * [matplotlib](https://matplotlib.org/)
* GUIs
  * [tkinter](https://docs.python.org/3.7/library/tkinter.html)
    * [shameless plug](http://cdaniels.net/talks.html#py3_tk)
* Argument Parsing
  * [argarse](https://docs.python.org/3.7/library/argparse.html)

## How to Install Them

* `pip` will let you install Python modules from the internet.
  * Official docs on [python.org](https://packaging.python.org/tutorials/installing-packages/)

* Search packages: `pip3 search searchterm`

* Install a package `pip3 install --user packagename`
  * Don't install globally with `sudo pip install` unless you know what you are
  doing.

* Find packages on [pypi.org](https://pypi.org/).
  * Also try [awesome-python.com](https://awesome-python.com/)

## Questions?

## End.

## Thanks

* This slideshow was written using `pandoc` with
[caiofcm/filter_pandoc_run_py](https://github.com/caiofcm/filter_pandoc_run_py)
used to execute in-line Python code and embed the output.

* Thanks to Josh for copyediting.

A src/assets/talks/intro_to_python/slides.pdf => src/assets/talks/intro_to_python/slides.pdf +0 -0

M src/talks.md => src/talks.md +22 -0
@@ 1,5 1,27 @@
# My Talks

<h2 class=navhdr id="introduction_to_python">
	Introduction to Python
	<a href=#introduction_to_python class=navhandle>¶ </a>
</h2>

Python is a hugely popular, easy-to-use language with a rich library ecosystem.
It has taken both academic and industry computing by storm, and for good
reason. This talk provides a crash course in introductory Python. Basic
programming knowledge is assumed, but previous Python experience is not.

* [Slides as PDF](assets/talks/intro_to_python/slides.pdf)
* [Slides as HTML](assets/talks/intro_to_python/slides.html)
* [Slides as Markdown](assets/talks/intro_to_python/slides.md)
* [Talk Notes](assets/talks/intro_to_python/notes.pdf)
  * Same content as slides, but formatted for printing on letter sized paper.
* [Talk Sources](assets/talks/intro_to_python/Introduction%20to%20Python.tgz)


| Date       | Location                     | Organization / Event   |
|------------|------------------------------|------------------------|
| 2020-09-17 | University of South Carolina | ACM Weekly Meeting     |

<h2 class=navhdr id="introduction_to_graphviz">
	Introduction to GraphViz
	<a href=#introduction_to_graphviz class=navhandle>¶ </a>