Skip to content

Commit

Permalink
updates to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joelchan committed Feb 5, 2024
1 parent 4abdbc4 commit 0dbe106
Show file tree
Hide file tree
Showing 55 changed files with 552 additions and 493 deletions.
215 changes: 130 additions & 85 deletions 10_Pandas-1.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 11_Pandas-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,7 @@ <h5>What’s happening under the hood<a class="headerlink" href="#what-s-happeni
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">KeyError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_30808</span><span class="o">/</span><span class="mf">3685041712.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_61602</span><span class="o">/</span><span class="mf">3685041712.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">----&gt; </span><span class="mi">1</span> <span class="n">courses</span> <span class="o">=</span> <span class="n">courses</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="s2">&quot;has_intro&quot;</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">2</span> <span class="n">courses</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>

Expand Down
11 changes: 8 additions & 3 deletions 2b_Variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -1048,14 +1048,15 @@ <h3>Semantics<a class="headerlink" href="#semantics" title="Permalink to this he
<p class="admonition-title">Answer:</p>
<p>For me at least, the 2nd version makes it clearer that the program shouldn’t have <code class="docutils literal notranslate"><span class="pre">+</span></code> in there: it should be <code class="docutils literal notranslate"><span class="pre">*</span></code>, since pay is a function of hours worked <em>times</em> hourly rate.</p>
</div>
<p>Also, say an employee told you they needed to update their number of hours worked. Which variable would you need to update?</p>
<p>Going back to the issue of data types, I like to name my variables in a way that suggests their data type.</p>
<p>For example:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">userName</span></code> instead of <code class="docutils literal notranslate"><span class="pre">a</span></code>, which makes it clear that there’s probably some kind of <code class="docutils literal notranslate"><span class="pre">str</span></code> in there.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">isFunny</span></code> instead of <code class="docutils literal notranslate"><span class="pre">x</span></code>, which makes it clear that there’s probably a <code class="docutils literal notranslate"><span class="pre">boolean</span></code> in there</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">numCredits</span></code> instead of <code class="docutils literal notranslate"><span class="pre">y</span></code>, which makes it clear that there’s probably some kind of number in there</p></li>
</ul>
<p>Also, by convention, you might see people use certain names for certain kinds of things. For example, <code class="docutils literal notranslate"><span class="pre">i</span></code> is often used to refer to a counter value
<p>By convention, you might see people use certain names for certain kinds of things. For example, <code class="docutils literal notranslate"><span class="pre">i</span></code> is often used to refer to a counter value
<code class="docutils literal notranslate"><span class="pre">s</span></code> (or some variant of it) is often used to refer to a string.</p>
<p>You should feel free to name variables whatever makes sense to you, as long as you feel they accurately signal the logic of the program they’re in. Your future self (and current/future collaborators) will thank you for following this fundamental principle. You’ll be surprised how often you can get unstuck simply by clarifying the names of the variables (which makes the structure of the program clearer, and the source of the problem obvious).</p>
<p>Example: debug a program that is supposed to compute a total check with 20% tip after accounting for 7% tax</p>
Expand Down Expand Up @@ -1106,8 +1107,12 @@ <h3>Semantics<a class="headerlink" href="#semantics" title="Permalink to this he
<p><code class="docutils literal notranslate"><span class="pre">tipAmount</span> <span class="pre">=</span> <span class="pre">tipRate</span> <span class="pre">*</span> <span class="pre">(baseAmount</span> <span class="pre">+</span> <span class="pre">baseAmount*taxRate)</span></code></p>
</div>
<p>Again, these are the same exact programs, from Python’s perspective! The variable names make all the difference.</p>
<p>Just for fun, you can read some programming horror stories about variable naming <a class="reference external" href="https://www.reddit.com/r/programminghorror/comments/251nsl/bad_variable_names/">here</a>.</p>
<p>And see <a class="reference external" href="https://stackoverflow.com/questions/454178/what-is-readable-code-what-are-the-best-practices-to-follow-while-naming-variab">this StackOverflow thread</a> for discussion of the importance of variable naming (in the context of discussing code readability, a central thing we care about it in this class, enough to make it a rubric item on your Projects!). The thread includes some links to style guides from Microsof, Python, and other sources.</p>
<p>To reinforce the point, I recommend:</p>
<ul class="simple">
<li><p>a collection of programming horror stories about variable naming <a class="reference external" href="https://www.reddit.com/r/programminghorror/comments/251nsl/bad_variable_names/">here</a></p></li>
<li><p><a class="reference external" href="https://stackoverflow.com/questions/454178/what-is-readable-code-what-are-the-best-practices-to-follow-while-naming-variab">this StackOverflow thread</a> for discussion of the importance of variable naming (in the context of discussing code readability, a central thing we care about it in this class, enough to make it a rubric item on your Projects!). The thread includes some links to style guides from Microsof, Python, and other sources.</p></li>
<li><p>and <a class="reference external" href="https://builtin.com/data-science/variable-names">this discussion</a> of variable naming in a data science context</p></li>
</ul>
</section>
</section>
<section id="the-nameerror">
Expand Down
19 changes: 10 additions & 9 deletions 3_Functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ <h3>Function call<a class="headerlink" href="#function-call" title="Permalink to
<li><p>Run the code associated with the function name <code class="docutils literal notranslate"><span class="pre">minutes_to_hours</span></code> with <code class="docutils literal notranslate"><span class="pre">mins</span></code> as input, and return the result</p></li>
<li><p>Pass that result to the <code class="docutils literal notranslate"><span class="pre">print</span></code> function (yes, this is a function also!) as an input argument.</p></li>
</ol>
<p>It may help to <a class="reference external" href="https://pythontutor.com/python-compiler.html#mode=edit">plug this code (and really all the other examples in this lecture) into pythontutor</a> to step through it line by line.</p>
<p>Let’s look at another example pair.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
Expand All @@ -998,12 +999,12 @@ <h3>Function call<a class="headerlink" href="#function-call" title="Permalink to
</div>
</div>
</div>
<p>Q: Where are the parameters and arguments?</p>
<div class="toggle admonition">
<p class="admonition-title">A:</p>
<p>Parameter: age
Argument: your_age</p>
</div>
<!-- Q: Where are the parameters and arguments?
```{admonition} A:
:class: toggle
Parameter: age
Argument: your_age
``` -->
</section>
<section id="key-idea-arguments-vs-parameters">
<h3>Key idea: Arguments vs. parameters<a class="headerlink" href="#key-idea-arguments-vs-parameters" title="Permalink to this headline">#</a></h3>
Expand Down Expand Up @@ -1077,7 +1078,7 @@ <h3>Writing a function from scratch<a class="headerlink" href="#writing-a-functi
</li>
<li><p>Run the function definition cell (this defines the function for Python)</p></li>
</ol>
<p>Let’s do an example together!</p>
<p>Let’s look at an example together!</p>
<p>Let’s write a function that applies a discount to a sale, given the sale amount and the percentage discount.</p>
<p>Here is code that successfully computes a saleAmount after applying a discount</p>
<div class="cell docutils container">
Expand Down Expand Up @@ -1105,7 +1106,7 @@ <h3>Writing a function from scratch<a class="headerlink" href="#writing-a-functi
</div>
</div>
</div>
<p>Here’s an example of how we can call that code, using our explicit argument-parameter notation from before.</p>
<p>Here’s an example of how we can call that function, using our explicit argument-parameter notation from before.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">apply_discount</span><span class="p">(</span><span class="n">saleAmount</span><span class="o">=</span><span class="mf">325.99</span><span class="p">,</span> <span class="n">percentageDiscount</span><span class="o">=</span><span class="mf">.2</span><span class="p">)</span>
Expand Down Expand Up @@ -1373,7 +1374,7 @@ <h3>Mismatching arguments and parameters<a class="headerlink" href="#mismatching
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_30836</span><span class="o">/</span><span class="mf">3405736382.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_52246</span><span class="o">/</span><span class="mf">3405736382.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="g g-Whitespace"> </span><span class="mi">4</span> <span class="k">return</span> <span class="n">result</span>
<span class="g g-Whitespace"> </span><span class="mi">5</span>
<span class="ne">----&gt; </span><span class="mi">6</span> <span class="n">minus</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
Expand Down
2 changes: 1 addition & 1 deletion 4_Conditionals.html
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ <h2>Practice: construct basic conditional blocks<a class="headerlink" href="#pra
</div>
</div>
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt"> File</span><span class="nn"> &quot;/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_30843/471952310.py&quot;</span><span class="gt">, line </span><span class="mi">7</span>
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt"> File</span><span class="nn"> &quot;/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_61639/471952310.py&quot;</span><span class="gt">, line </span><span class="mi">7</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Graduate&quot;</span><span class="p">)}</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span>: unmatched &#39;}&#39;
Expand Down
2 changes: 1 addition & 1 deletion 5_Lists.html
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,7 @@ <h3>IndexError<a class="headerlink" href="#indexerror" title="Permalink to this
</div>
</div>
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt"> File</span><span class="nn"> &quot;/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_30856/3285008488.py&quot;</span><span class="gt">, line </span><span class="mi">6</span>
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt"> File</span><span class="nn"> &quot;/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_61654/3285008488.py&quot;</span><span class="gt">, line </span><span class="mi">6</span>
<span class="n">This</span> <span class="n">happens</span> <span class="n">most</span> <span class="k">with</span> <span class="n">operations</span> <span class="n">that</span> <span class="n">can</span> <span class="n">be</span> <span class="n">done</span> <span class="n">wiht</span> <span class="n">methods</span> <span class="ow">and</span> <span class="n">functions</span><span class="p">,</span> <span class="n">such</span> <span class="k">as</span> <span class="n">sorting</span>
<span class="o">^</span>
<span class="ne">SyntaxError</span>: invalid syntax
Expand Down
2 changes: 1 addition & 1 deletion 6_Iteration-Solutions-Day1.html
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ <h4>Mapping / transforming<a class="headerlink" href="#mapping-transforming" tit
</div>
</div>
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt"> File</span><span class="nn"> &quot;/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_30868/2612048376.py&quot;</span><span class="gt">, line </span><span class="mi">8</span>
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt"> File</span><span class="nn"> &quot;/var/folders/xz/_hjc5hsx743dclmg8n5678nc0000gn/T/ipykernel_61666/2612048376.py&quot;</span><span class="gt">, line </span><span class="mi">8</span>
<span class="c1"># add transformed item to transformed items list</span>
<span class="o">^</span>
<span class="ne">IndentationError</span>: expected an indented block
Expand Down
2 changes: 1 addition & 1 deletion 6_Iteration-Solutions-Day2.html
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ <h3>Anatomy of an indefinite (while) loop in Python<a class="headerlink" href="#
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">StdinNotImplementedError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_30876</span><span class="o">/</span><span class="mf">3638488589.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_61673</span><span class="o">/</span><span class="mf">3638488589.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">----&gt; </span><span class="mi">1</span> <span class="n">guess</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&quot;Try to guess the number between 1 and 10, or say `exit` to quit&quot;</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">2</span> <span class="n">number</span> <span class="o">=</span> <span class="mi">5</span>
<span class="g g-Whitespace"> </span><span class="mi">3</span> <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
Expand Down
2 changes: 1 addition & 1 deletion 6_Iteration.html
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ <h2>What are loops and why should we care about them?<a class="headerlink" href=
<div class="cell_output docutils container">
<div class="output traceback highlight-ipythontb notranslate"><div class="highlight"><pre><span></span><span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">NameError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_30862</span><span class="o">/</span><span class="mf">3929469459.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="o">/</span><span class="n">var</span><span class="o">/</span><span class="n">folders</span><span class="o">/</span><span class="n">xz</span><span class="o">/</span><span class="n">_hjc5hsx743dclmg8n5678nc0000gn</span><span class="o">/</span><span class="n">T</span><span class="o">/</span><span class="n">ipykernel_61660</span><span class="o">/</span><span class="mf">3929469459.</span><span class="n">py</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="c1"># stir the sauce until it is thick</span>
<span class="ne">----&gt; </span><span class="mi">2</span> <span class="k">while</span> <span class="n">check_sauce</span><span class="p">()</span> <span class="o">==</span> <span class="s2">&quot;thick&quot;</span><span class="p">:</span>
<span class="g g-Whitespace"> </span><span class="mi">3</span> <span class="n">stir</span><span class="p">()</span>
Expand Down
Loading

0 comments on commit 0dbe106

Please sign in to comment.