You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

883 lines
57 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="../../../../lib/css/zenburn.css">
<link rel="stylesheet" href="../../../../lib/css/prism.css">
<link rel="stylesheet" href="../../../../css/reveal.css">
<link rel="stylesheet" href="../../../../css/theme/ga-title.css" id="theme">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="https://s3.amazonaws.com/python-ga/proxima-nova/fonts.css" />
</head>
<body class="language-javascript">
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!--
title: Python Programming: Advanced Function Arguments
type: lesson
duration: "01:00"
creator: Brandi Butler
-->
<section id="section" class="level2 separator">
<h2><img src="https://s3.amazonaws.com/python-ga/images/GA_Cog_Medium_White_RGB.png" /></h2>
<h1>
Python Programming: Advanced Function Arguments
</h1>
<!--
## Overview
This lesson starts with a review of the previous function lesson, leading into a few recap exercises. After that, it discusses `*args`, `kwargs`, and default argument values.
## Important Notes or Prerequisites
- Important Note: They don't know dictionaries yet! We introduce `**kwargs`, but are careful not to phrase it in terms of dictionaries.
## Learning Objectives
In this lesson, students will:
- Use arbitrary numbers of arguments in functions.
- Use keyword arguments in functions.
- Use default values in functions.
## Duration
40 minutes
## Suggested Agenda
| Time | Activity |
| --- | --- |
| 0:00 - 0:03 | Welcome |
| 0:03 - 0:10 | Function Review |
| 0:10 - 0:30 | Args / Kwargs |
| 0:30 - 0:42 | Other Args |
| 0:42 - 0:45 | Summary |
## In Class: Materials
- Projector
- Internet connection
- Python3
-->
<hr />
</section>
<section id="lesson-objectives" class="level2">
<h2>Lesson Objectives</h2>
<p><em>After this lesson, you will be able to…</em></p>
<ul>
<li>Review all topics to this point.</li>
<li>Use keyword arguments in functions.</li>
</ul>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Quickly go over the learning objectives.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>What if I want to have a variable number of arguments?</li>
<li>What if I want to have my arguments out of order?</li>
<li>What if I want to specify some arguments but not others?</li>
<li>Were going to learn all of this.</li>
</ul>
</aside>
<hr />
</section>
<section id="review-functions" class="level2">
<h2>Review: Functions</h2>
<p>Main points:</p>
<ul>
<li>Define functions using the <code>def</code> keyword.</li>
<li>A function must be <strong>called</strong> before the code in it will run!</li>
<li>You will recognize function calls by the <code>()</code> at the end.</li>
</ul>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co"># This part is the function definition!</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="kw">def</span> say_hello():</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"> <span class="bu">print</span>(<span class="st">&quot;hello world!&quot;</span>)</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="co"># This part is actually calling/running the function!</span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6">say_hello()</a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Theyve seen this a few times, but make sure to recap the syntax again. Were throwing a lot at them quickly.</li>
<li>Try not to spend too much time on any of these review slides; theres a lot of new stuff to cover.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>Recap the point of functions (e.g. DRY)</li>
</ul>
</aside>
<hr />
</section>
<section id="review-function-arguments" class="level2">
<h2>Review: Function Arguments</h2>
<ul>
<li>Provide an argument to a function when you need something small to vary.</li>
</ul>
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-args-practice?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
</iframe>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>This is a demo and recap, not an exercise.</li>
<li>Theyve seen this a few times, but make sure to recap the syntax again. Were throwing a lot at them quickly.</li>
<li>Give other examples where parameters would be useful.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>“A lot of times, you want to run the same code, but with a teeny tiny difference. Remember the function we looked at before which had the orders with the $5 shipping charge?”</li>
</ul>
<p><strong>Repl.it Note</strong>: The code here is:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="kw">def</span> print_order(product):</a>
<a class="sourceLine" id="cb2-2" data-line-number="2"> <span class="bu">print</span>(<span class="st">&quot;Thank you for ordering the &quot;</span> <span class="op">+</span> product <span class="op">+</span> <span class="st">&quot;.&quot;</span>)</a>
<a class="sourceLine" id="cb2-3" data-line-number="3"> <span class="bu">print</span>(<span class="st">&quot;There will be a $5.00 shipping charge for this order.&quot;</span>)</a>
<a class="sourceLine" id="cb2-4" data-line-number="4"></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">print_order(<span class="st">&quot;Trampoline&quot;</span>)</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">print_order(<span class="st">&quot;Spider-Man Comic&quot;</span>)</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">print_order(<span class="st">&quot;Hot Cheetos&quot;</span>)</a></code></pre></div>
</aside>
<hr />
</section>
<section id="multiple-parameters" class="level2">
<h2>Multiple Parameters</h2>
<p>Functions can have…</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="co"># No parameters</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="kw">def</span> add_2_and_3():</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"> x <span class="op">=</span> <span class="dv">2</span> <span class="op">+</span> <span class="dv">3</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4"> <span class="bu">print</span>(x)</a>
<a class="sourceLine" id="cb3-5" data-line-number="5"></a>
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="co"># One parameter</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="kw">def</span> add_2(x):</a>
<a class="sourceLine" id="cb3-8" data-line-number="8"> <span class="bu">print</span>(x <span class="op">+</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb3-9" data-line-number="9"></a>
<a class="sourceLine" id="cb3-10" data-line-number="10"><span class="co"># Multiple parameters</span></a>
<a class="sourceLine" id="cb3-11" data-line-number="11"><span class="kw">def</span> add(x, y, z):</a>
<a class="sourceLine" id="cb3-12" data-line-number="12"> <span class="bu">print</span>(x <span class="op">+</span> y <span class="op">+</span> z)</a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Theyve seen this a few times, but make sure to recap the syntax again. Were throwing a lot at them quickly.</li>
<li>Give an example where each case would be useful.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>“Functions can have many parameters… or not!”</li>
</ul>
</aside>
<hr />
</section>
<section id="discussion-print-vs-return" class="level2">
<h2>Discussion: Print vs Return</h2>
<p>Why doesnt this do anything?</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="kw">def</span> add(x, y, z):</a>
<a class="sourceLine" id="cb4-2" data-line-number="2"> <span class="cf">return</span> x <span class="op">+</span> y <span class="op">+</span> z</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">add(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="co"># does nothing!</span></a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Facilitate a discussion! See if one of the students can tell you.</li>
<li>Bring it up in a repl.it - <a href="https://repl.it/@SuperTernary/python-programming-return-print?lite=true">here it is with the code</a>. Add back in the print statement.</li>
</ul>
</aside>
<hr />
</section>
<section id="we-do-review-exercises" class="level2">
<h2>We Do: Review Exercises</h2>
<p>Locally, lets create a file called <code>function_practice.py</code>.</p>
<ul>
<li><p>Well define a function named <code>areBothEven</code>.</p></li>
<li><p>It will accept two parameters: <code>num1</code> and <code>num2</code>.</p></li>
<li><p>Inside the function, well return <code>True</code> if <code>num1</code> and <code>num2</code> are both even but <code>False</code> if they are not.</p></li>
<li><p>Well test this with <code>print(areBothEven(1, 4))</code>, <code>print(areBothEven(2, 4))</code>, and <code>print(areBothEven(2, 3))</code>.</p></li>
</ul>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li><p>Do this with them, but make sure they pull up a file and are following along. We dont have time for a full exercise - we just want to be sure everyone understands functions before we make this far harder.</p></li>
<li><p>Talk them through this - “Use the modulus operator (<code>%</code>) to see if both numbers are even. If you use <code>num % 2</code>, it will return <code>1</code> if the number is odd and <code>0</code> if the number is even.”&quot;</p></li>
</ul>
</aside>
<hr />
</section>
<section id="we-do-another-review-exercise" class="level2">
<h2>We Do: Another Review Exercise!</h2>
<p>In our file, well define another function named <code>lightOrDark</code> that takes the parameter <code>hour</code>.</p>
<ul>
<li><p>If <code>hour</code> is greater than 24, the function will print “Thats not an hour in the day!” and <strong>return nothing.</strong></p></li>
<li><p>If <code>hour</code> is less than 7 or greater than 17, the function will return “Its dark outside!”</p></li>
<li><p>Otherwise, the function will return “Its light outside!”</p></li>
<li><p>Well test this with <code>print(lightOrDark(4))</code>, <code>print(lightOrDark(26))</code>, and <code>print(lightOrDark(</code>10<code>))</code>.</p></li>
</ul>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li><p>Do this with them, but make sure they pull up a file and are following along. We dont have time for a full exercise - we just want to be sure everyone understands functions before we make this far harder.</p></li>
<li><p>Talk about how we can just have <code>if</code> statements - we dont need an <code>elif</code> because the <code>return</code> exits the function.</p></li>
</ul>
</aside>
<hr />
</section>
<section id="discussion-arguments" class="level2">
<h2>Discussion: Arguments</h2>
<p>Now, lets make functions a little more sophisticated.</p>
<p>What do you think the following code does?</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="kw">def</span> multiply(x, y):</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"> <span class="bu">print</span>(x <span class="op">*</span> y)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3"></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">multiply(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>) <span class="co"># Too many arguments! What happens?</span></a></code></pre></div>
<p>What if we want all of these to work?</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="kw">def</span> multiply(x, y):</a>
<a class="sourceLine" id="cb6-2" data-line-number="2"> <span class="bu">print</span>(x <span class="op">*</span> y)</a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">multiply(<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>)</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">multiply(<span class="dv">4</span>, <span class="dv">5</span>)</a>
<a class="sourceLine" id="cb6-6" data-line-number="6">multiply(<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">7</span>, <span class="dv">3</span>, <span class="dv">9</span>)</a></code></pre></div>
<aside class="notes">
<p>Teaching tip:</p>
<ul>
<li>After they guess, show them the error in an interpreter.</li>
<li>Theres a lesson on errors later - dont go into it here. Just show that it breaks, but we want to be able to pass <code>multiply</code> any number of arguments.</li>
</ul>
<p>Talking point:</p>
<ul>
<li>“When we tried to run the function with too many arguments, we got an error.”</li>
<li>“What if I want to have any number of arguments?”</li>
</ul>
</aside>
<hr />
</section>
<section id="introducing-args" class="level2">
<h2>Introducing <code>*args</code></h2>
<p><code>*args</code> is a parameter that says “Put as many parameters as youd like!”</p>
<ul>
<li>Pronounced like a pirate - “arrrrghhhs!”</li>
<li>Known as <strong>positional arguments</strong></li>
<li>The <code>*</code> at the beginning is what specifies the variable number of arguments</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="kw">def</span> multiply(<span class="op">*</span>args):</a>
<a class="sourceLine" id="cb7-2" data-line-number="2"> product <span class="op">=</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3"></a>
<a class="sourceLine" id="cb7-4" data-line-number="4"> <span class="co"># We don&#39;t know the number of args, so we need a loop</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5"> <span class="cf">for</span> num <span class="kw">in</span> args:</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"> product <span class="op">*=</span> num</a>
<a class="sourceLine" id="cb7-7" data-line-number="7"> <span class="bu">print</span>(product)</a>
<a class="sourceLine" id="cb7-8" data-line-number="8"></a>
<a class="sourceLine" id="cb7-9" data-line-number="9">multiply(<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>) <span class="co"># Prints 120!</span></a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Point out why we need a loop, and why we need the <code>product</code> declaration at the top.</li>
<li>Remind students about the shorthand <code>*=</code> syntax.</li>
<li>Make sure students dont forget the <code>*</code> or the <code>s</code> in <code>*args</code></li>
<li>Theres a repl.it <a href="https://repl.it/@SuperTernary/python-programming-args-multiplication?lite=true">here</a> if you want to run this.</li>
</ul>
</aside>
<hr />
</section>
<section id="we-do-args" class="level2">
<h2>We Do: <code>*args</code></h2>
<p>Lets create a local file for this lesson - <code>args_practice.py</code>.</p>
<ul>
<li>Well write a function, <code>sum_everything</code> that takes any numbers of arguments and adds them together.</li>
<li>At the end, well print out the sum.</li>
<li>Lets try it with <code>sum_everything(4, 5, 6)</code> and <code>sum_everything(6, 4, 5)</code>. The order doesnt matter!</li>
<li><code>*args</code> says “any number” - you can pass in none at all!</li>
</ul>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Do this with them, but make sure they have a file out and are following along. The more they practice writing this, the better.</li>
<li>See if they can guess what a line should be before you write it.</li>
<li>Since its a loop, the order of arguments doesnt matter.</li>
<li>Once the code is written, show them a varying number of arguments being passed in. 1? 4? Without any arguments passed in at all (especially this one!).</li>
</ul>
<p><strong>Solution Code</strong>:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="kw">def</span> sum_everything(<span class="op">*</span>args):</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"> <span class="bu">sum</span> <span class="op">=</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"> <span class="co"># We don&#39;t know the number of args, so we need a loop</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5"> <span class="cf">for</span> num <span class="kw">in</span> args:</a>
<a class="sourceLine" id="cb8-6" data-line-number="6"> <span class="bu">sum</span> <span class="op">+=</span> num</a>
<a class="sourceLine" id="cb8-7" data-line-number="7"> <span class="bu">print</span>(<span class="bu">sum</span>)</a>
<a class="sourceLine" id="cb8-8" data-line-number="8"></a>
<a class="sourceLine" id="cb8-9" data-line-number="9">sum_everything(<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>) <span class="co"># Prints 15</span></a></code></pre></div>
</aside>
<hr />
</section>
<section id="discussion-often-order-does-matter." class="level2">
<h2>Discussion: Often, Order Does Matter.</h2>
<p>Lets switch gears. Back to a set number of arguments!</p>
<p>Check this out:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="kw">def</span> triple_divide(x, y, z):</a>
<a class="sourceLine" id="cb9-2" data-line-number="2"> <span class="bu">print</span>(x <span class="op">/</span> y <span class="op">/</span> z)</a>
<a class="sourceLine" id="cb9-3" data-line-number="3"></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">triple_divide(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">10</span>) <span class="co"># Prints 0.05</span></a></code></pre></div>
<p>Without otherwise specifying, <code>x</code> is <code>1</code>, <code>y</code> is <code>2</code>, and <code>z</code> is <code>10</code>.</p>
<ul>
<li>What if we want <code>x</code>, the first parameter to get the value <code>10</code>?</li>
<li>Is there a way to specify which argument goes to which parameter?</li>
</ul>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Emphasize that were done with <code>*args</code> specifically; were moving on to a different type of arg specifics.</li>
<li>Run this a few different ways to show order matters.</li>
<li>Remind them what “argument” versus “parameter” is.</li>
<li>Try to get them guessing, here.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>“This lesson is about all types of arguments in functions. Weve seen a variable number - now lets look at something else.”</li>
</ul>
</aside>
<hr />
</section>
<section id="forcing-the-order" class="level2">
<h2>Forcing the Order</h2>
<p>Here weve forced the order to be reversed from the default. In fact, we can specify any ordering we want by using the names of the parameters (keywords) when providing the argument values.</p>
<iframe height="400px" width="100%" src="https://repl.it/@brandiw/02-Python-Kwargs-09?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
</iframe>
<p>Take a moment to play around with the values until you really believe it!</p>
<hr />
</section>
<section id="keyword-arguments-kwargs" class="level2">
<h2>Keyword Arguments (kwargs)</h2>
<p>Using kwargs, odrer deonst mtater:</p>
<ul>
<li>Arguments are named according to their corresponding parameters.</li>
<li>Order doesnt matter - Python will check the names and match them!</li>
<li>Values are assigned because the <em>keyword argument</em> and the <em>parameter name</em> match.</li>
</ul>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="kw">def</span> triple_divide(x, y, z):</a>
<a class="sourceLine" id="cb10-2" data-line-number="2"> <span class="bu">print</span>(x <span class="op">/</span> y <span class="op">/</span> z)</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">triple_divide(x<span class="op">=</span><span class="dv">10</span>, y<span class="op">=</span><span class="dv">2</span>, z<span class="op">=</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="co"># This runs 10 / 2 / 1, and prints 5</span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6">triple_divide(y<span class="op">=</span><span class="dv">2</span>, z<span class="op">=</span><span class="dv">1</span>, x<span class="op">=</span><span class="dv">10</span>)</a>
<a class="sourceLine" id="cb10-7" data-line-number="7"><span class="co"># This ALSO runs 10 / 2 / 1, and prints 5.</span></a></code></pre></div>
<blockquote>
<p><strong>Protip</strong>: Keep your parameter names simple and concise to prevent typos and misspellings!</p>
</blockquote>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Show this in an interpreter, or bring up a repl.it. Change the values and order of x, y, and z here so students can really understand.</li>
<li>Get students comfortable with when order matters and what we can do about it.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>“In normal cases for function args a default order is assumed. Much in the same way, if you go to a restaurant, by default you will get drinks, then appetizers, then entrees, then desserts. However, you are free to ask your waiter to bring your dessert first or bring your appetizer with the meal. You dont need to specify anything if the default order will do, but if you are going to reinvent dinner order, you will need to say something!”</li>
</ul>
</aside>
<hr />
</section>
<section id="mix-it-up.-but-not-with-every-argument" class="level2">
<h2>Mix It Up…. but Not With Every Argument?</h2>
<p>Fun fact: You can provide some args in order - <strong>positional</strong> - and some with keywords.</p>
<ul>
<li>Undefined are assigned in sequential order.</li>
<li>Keywords have to come last! - then, in any order.</li>
</ul>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" data-line-number="1">dinner(app<span class="op">=</span><span class="st">&quot;chicken wings&quot;</span>, main_course<span class="op">=</span><span class="st">&quot;medium rare steak&quot;</span>, drink<span class="op">=</span><span class="st">&quot;water&quot;</span>, dessert<span class="op">=</span><span class="st">&quot;milkshake&quot;</span>)</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">dinner(<span class="st">&quot;chicken wings&quot;</span>, <span class="st">&quot;water&quot;</span>, dessert<span class="op">=</span><span class="st">&quot;milkshake&quot;</span>, main_course<span class="op">=</span><span class="st">&quot;medium rare steak&quot;</span>)</a></code></pre></div>
<iframe height="300px" width="100%" src="https://repl.it/@brandiw/02-Python-Kwargs-08?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
</iframe>
<aside class="notes">
<p>Talking points:</p>
<ul>
<li>As you make the demo more and more complex, make sure to talk about what youre doing.</li>
</ul>
<p>Teaching tips:</p>
<ul>
<li>This is a demo, so dont worry about them following along (though its great if they do!).</li>
<li>First, recap and call the function with the kwargs in different orders: <code>dinner(app=&quot;nachos&quot;, main_course=&quot;steak&quot;, drink=&quot;water&quot;, dessert=&quot;cake&quot;)</code></li>
<li>Then, talk about how you dont necessarily need every keyword defined - but theyll automatically be assigned in order. Demo that: <code>dinner(&quot;water&quot;, &quot;nachos&quot;, dessert=&quot;cake&quot;, main_course=&quot;steak&quot;)</code> <code>dinner(&quot;nachos&quot;, &quot;water&quot;, dessert=&quot;steak&quot;, main_course=&quot;steak&quot;)</code></li>
<li>Then, show what happens if you put a kwarg first: <code>dinner(main_course= &quot;steak&quot;, &quot;nachos&quot;, &quot;water&quot;, dessert=&quot;cake&quot;)</code></li>
<li>What happens if you name two arguments the same? <code>dinner(app=&quot;nachos&quot;, app=&quot;steak&quot;, app=&quot;water&quot;, dessert=&quot;cake&quot;)</code></li>
<li>End with correct syntax, so thats freshest in their mind.</li>
</ul>
<p><strong>Repl.it Note:</strong> The code here is:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="kw">def</span> dinner(drink, app, main_course, dessert):</a>
<a class="sourceLine" id="cb12-2" data-line-number="2"> <span class="bu">print</span>(<span class="st">&quot;You&#39;re drinking&quot;</span>, drink)</a>
<a class="sourceLine" id="cb12-3" data-line-number="3"> <span class="bu">print</span>(<span class="st">&quot;You&#39;re snacking on&quot;</span>, app)</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"> <span class="bu">print</span>(<span class="st">&quot;You&#39;re drinking&quot;</span>, main_course)</a>
<a class="sourceLine" id="cb12-5" data-line-number="5"> <span class="bu">print</span>(<span class="st">&quot;You&#39;re wrapping up with&quot;</span>, dessert)</a>
<a class="sourceLine" id="cb12-6" data-line-number="6"></a>
<a class="sourceLine" id="cb12-7" data-line-number="7">dinner(app<span class="op">=</span><span class="st">&quot;nachos&quot;</span>, main_course<span class="op">=</span><span class="st">&quot;steak&quot;</span>, drink<span class="op">=</span><span class="st">&quot;water&quot;</span>, dessert<span class="op">=</span><span class="st">&quot;cake&quot;</span>)</a></code></pre></div>
</aside>
<hr />
</section>
<section id="quick-review" class="level2">
<h2>Quick Review</h2>
<p><code>*args</code>: Any number of arguments - even 0! - can be passed in.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="kw">def</span> sum_everything(<span class="op">*</span>args):</a>
<a class="sourceLine" id="cb13-2" data-line-number="2"> <span class="bu">sum</span> <span class="op">=</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3"></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"> <span class="cf">for</span> num <span class="kw">in</span> args:</a>
<a class="sourceLine" id="cb13-5" data-line-number="5"> <span class="bu">sum</span> <span class="op">+=</span> num</a>
<a class="sourceLine" id="cb13-6" data-line-number="6"> <span class="bu">print</span>(<span class="bu">sum</span>)</a>
<a class="sourceLine" id="cb13-7" data-line-number="7"></a>
<a class="sourceLine" id="cb13-8" data-line-number="8">sum_everything(<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>) <span class="co"># Prints 15</span></a></code></pre></div>
<p>Keyword arguments (kwargs): Arguments can be passed in out of order.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="kw">def</span> divide(first, second, third):</a>
<a class="sourceLine" id="cb14-2" data-line-number="2"> <span class="bu">print</span>(first <span class="op">/</span> second <span class="op">/</span> third)</a>
<a class="sourceLine" id="cb14-3" data-line-number="3"></a>
<a class="sourceLine" id="cb14-4" data-line-number="4">divide(first<span class="op">=</span><span class="dv">10</span>, second<span class="op">=</span><span class="dv">2</span>, third<span class="op">=</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb14-5" data-line-number="5">divide(second<span class="op">=</span><span class="dv">2</span>, third<span class="op">=</span><span class="dv">1</span>, first<span class="op">=</span><span class="dv">10</span>)</a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Do a quick check for understanding.</li>
</ul>
</aside>
<hr />
</section>
<section id="discussion-variable-numbers-of-kwargs" class="level2">
<h2>Discussion: Variable Numbers of Kwargs?</h2>
<p>What if I go to Froyo? I need:</p>
<ul>
<li>One argument <code>spoon</code>, to pick a spoon size.</li>
<li>A variable number of arguments for all the flavors of frozen yogurt I might eat!</li>
</ul>
<p><code>def yogurt_land(*args)</code>?</p>
<ul>
<li>No! <code>*args</code> wont work - we need to know which arg is the spoon.</li>
</ul>
<p><code>def yogurt_land(spoon, froyo)</code>?</p>
<ul>
<li>No! We dont know the number of froyo arguments.</li>
</ul>
<p>Any ideas?</p>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Encourage them to give ideas. See if they can come up with putting the two ideas together. Remind them that the <code>*</code> is the important part of <code>*args</code>.</li>
</ul>
</aside>
<hr />
</section>
<section id="introducing-kwargs" class="level2">
<h2>Introducing: <code>**kwargs</code></h2>
<p>The <code>*</code> in <code>*args</code> means: Any number of arguments.</p>
<p>Lets add <code>**</code> to our kwargs: <code>**kwargs</code> can take a variable number of arguments. Note the double <code>**</code>!</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="kw">def</span> yogurt_land(spoon, <span class="op">**</span>kwargs):</a>
<a class="sourceLine" id="cb15-2" data-line-number="2"> <span class="bu">print</span>(spoon)</a>
<a class="sourceLine" id="cb15-3" data-line-number="3"> <span class="co"># We need a loop, because we don&#39;t know how many kwargs there are.</span></a>
<a class="sourceLine" id="cb15-4" data-line-number="4"> <span class="cf">for</span> keyword, flavor <span class="kw">in</span> kwargs.items():</a>
<a class="sourceLine" id="cb15-5" data-line-number="5"> <span class="co"># kwargs.items has the keyword and the value, which we&#39;re calling &quot;flavor&quot; in the loop.</span></a>
<a class="sourceLine" id="cb15-6" data-line-number="6"> <span class="bu">print</span>(<span class="st">&quot;My&quot;</span>, keyword, <span class="st">&quot;is a&quot;</span>, flavor)</a>
<a class="sourceLine" id="cb15-7" data-line-number="7"></a>
<a class="sourceLine" id="cb15-8" data-line-number="8"><span class="co"># Like before, the unnamed arg has to come first!</span></a>
<a class="sourceLine" id="cb15-9" data-line-number="9">yogurt_land(<span class="st">&quot;large!&quot;</span>, first_froyo<span class="op">=</span><span class="st">&quot;vanilla&quot;</span>, second_froyo<span class="op">=</span><span class="st">&quot;chocolate&quot;</span>, third_froyo<span class="op">=</span><span class="st">&quot;banana&quot;</span>)</a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>The next slide has a repl.it so you can demo the code! If students are lost, just turn the slide earlier.</li>
<li>They <strong>havent learned dictionaries</strong>. Dont stress on the structure of kwargs. Just get them to understand that <code>kwargs</code> will hold the keyword and then the value.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>Walk through this. Note that the spoon size has to come first, and stress that here, its two <code>**</code>, not just one. Note the <code>for</code> loop!</li>
<li>Note that kwargs is just a convention, but dont press this point - its a lot to take in already.</li>
</ul>
</aside>
<hr />
</section>
<section id="we-do-4-froyos" class="level2">
<h2>We Do: 4 Froyos</h2>
<ul>
<li>Can we subtract one of the froyos?</li>
<li>Where is my 4th froyo?</li>
<li>What if I drop all my froyos on the ground? (No kwargs)</li>
<li>Can I skip the drink or spoon positional arguments?</li>
</ul>
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-args-froyo?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
</iframe>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>This is a demo slide, not an exercise. Spend enough time to make sure they understand whats going on, then move on.</li>
<li>Show changing the order and number of kwargs. Show having no kwargs. Trying giving all kwargs the same keyword. For each of these, talk about whats happening and why. Try putting the kwargs in the definition first, and in the function call first. Remember not to mention dictionaries!</li>
<li>Show all variations with <code>spoon</code>: having no spoon, then giving the spoon the kwarg <code>spoon</code>, then putting the <code>spoon</code> at the end (and then in the middle) of the kwargs, then putting the spoon argument (with no <code>spoon</code> kwarg at other places). For each of these, talk about whats happening and why.</li>
<li>End again with the correct syntax, so students remember what it should look like.</li>
</ul>
<p><strong>Repl.it Note:</strong> The repl.it has</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="kw">def</span> yogurt_land(drink, spoon, <span class="op">**</span>kwargs):</a>
<a class="sourceLine" id="cb16-2" data-line-number="2"> <span class="cf">if</span> spoon:</a>
<a class="sourceLine" id="cb16-3" data-line-number="3"> <span class="bu">print</span>(<span class="st">&quot;Here is your spoon!&quot;</span>)</a>
<a class="sourceLine" id="cb16-4" data-line-number="4"></a>
<a class="sourceLine" id="cb16-5" data-line-number="5"> <span class="cf">else</span>:</a>
<a class="sourceLine" id="cb16-6" data-line-number="6"> <span class="bu">print</span>(<span class="st">&quot;No spoon, no worries&quot;</span>)</a>
<a class="sourceLine" id="cb16-7" data-line-number="7"></a>
<a class="sourceLine" id="cb16-8" data-line-number="8"> <span class="bu">print</span>(<span class="st">&quot;Here is your&quot;</span>, drink)</a>
<a class="sourceLine" id="cb16-9" data-line-number="9"></a>
<a class="sourceLine" id="cb16-10" data-line-number="10"> <span class="cf">for</span> keyword, flavor <span class="kw">in</span> kwargs.items():</a>
<a class="sourceLine" id="cb16-11" data-line-number="11"> <span class="bu">print</span>(<span class="st">&quot;My&quot;</span>, keyword, <span class="st">&quot;is a&quot;</span>, flavor)</a>
<a class="sourceLine" id="cb16-12" data-line-number="12"></a>
<a class="sourceLine" id="cb16-13" data-line-number="13"></a>
<a class="sourceLine" id="cb16-14" data-line-number="14">yogurt_land(<span class="st">&quot;water&quot;</span>, <span class="st">&quot;large&quot;</span>, first_froyo<span class="op">=</span><span class="st">&quot;vanilla&quot;</span>, second_froyo<span class="op">=</span><span class="st">&quot;chocolate&quot;</span>, third_froyo<span class="op">=</span><span class="st">&quot;banana&quot;</span>)</a></code></pre></div>
</aside>
<hr />
</section>
<section id="quick-review-of-useful-argument-types" class="level2">
<h2>Quick Review of Useful Argument Types:</h2>
<p>At this point, we have <code>*args</code>, <code>kwargs</code> and <code>**kwargs</code>:</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="co"># Args: Any number of arguments:</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="kw">def</span> multiply(<span class="op">*</span>args):</a>
<a class="sourceLine" id="cb17-3" data-line-number="3"> product <span class="op">=</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb17-4" data-line-number="4"> <span class="cf">for</span> num <span class="kw">in</span> args:</a>
<a class="sourceLine" id="cb17-5" data-line-number="5"> product <span class="op">*=</span> num</a>
<a class="sourceLine" id="cb17-6" data-line-number="6"></a>
<a class="sourceLine" id="cb17-7" data-line-number="7">multiply(<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>)</a>
<a class="sourceLine" id="cb17-8" data-line-number="8"></a>
<a class="sourceLine" id="cb17-9" data-line-number="9"><span class="co"># Kwargs: Named (keyword) arguments</span></a>
<a class="sourceLine" id="cb17-10" data-line-number="10"><span class="kw">def</span> triple_divide(x, y, z):</a>
<a class="sourceLine" id="cb17-11" data-line-number="11"> <span class="bu">print</span>(x <span class="op">/</span> y <span class="op">/</span> z)</a>
<a class="sourceLine" id="cb17-12" data-line-number="12"></a>
<a class="sourceLine" id="cb17-13" data-line-number="13">triple_divide(x<span class="op">=</span><span class="dv">10</span>, y<span class="op">=</span><span class="dv">2</span>, z<span class="op">=</span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb17-14" data-line-number="14"></a>
<a class="sourceLine" id="cb17-15" data-line-number="15"><span class="co"># **Kwargs: Any number of Kwargs</span></a>
<a class="sourceLine" id="cb17-16" data-line-number="16"><span class="kw">def</span> my_froyo(spoon, <span class="op">**</span>kwargs):</a>
<a class="sourceLine" id="cb17-17" data-line-number="17"> <span class="cf">for</span> froyo, flavor <span class="kw">in</span> kwargs.items():</a>
<a class="sourceLine" id="cb17-18" data-line-number="18"> <span class="bu">print</span>(froyo, <span class="st">&quot;is a&quot;</span>, flavor)</a>
<a class="sourceLine" id="cb17-19" data-line-number="19"></a>
<a class="sourceLine" id="cb17-20" data-line-number="20">my_froyo(<span class="st">&quot;large!&quot;</span>, froyo1<span class="op">=</span><span class="st">&quot;vanilla&quot;</span>, froyo2<span class="op">=</span><span class="st">&quot;chocolate&quot;</span>, froyo3<span class="op">=</span><span class="st">&quot;banana&quot;</span>)</a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Do a quick check for understanding.</li>
</ul>
</aside>
<hr />
</section>
<section id="discussion-printing" class="level2">
<h2>Discussion: Printing</h2>
<p><code>print</code> is a function! Thats why it has parentheses! - Its built into Python, so you dont have to define it. You can just use it.</p>
<p>When printing, commas automatically add spaces:</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="bu">print</span>(<span class="st">&quot;Hi!&quot;</span>, <span class="st">&quot;Vanilla,&quot;</span>, <span class="st">&quot;please.&quot;</span>)</a></code></pre></div>
<p>But since <code>print</code> is a function, too - do you think theres anything we can do to change those spaces to something else?</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="co"># Hi!-Vanilla,-please,-but-chocolate-is-cool.</span></a>
<a class="sourceLine" id="cb19-2" data-line-number="2"><span class="co"># Hi!-and-Vanilla,-and-please.</span></a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Its probably a revelation that <code>print</code> is a function. Start with that - make sure they understand that, first.</li>
<li>Encourage a discussion. Can they get to the idea of adding arguments to <code>print</code>?</li>
</ul>
</aside>
<hr />
</section>
<section id="print-is-awesome-optional-parameters" class="level2">
<h2>Print is AWESOME: Optional Parameters</h2>
<p>Turns out…</p>
<ul>
<li><code>print</code> accepts an optional keyword argument: <code>sep</code>.</li>
</ul>
<p>The <code>sep</code> value given will be used as a <strong>separator.</strong></p>
<ul>
<li>Its optional! Without it, <code>print</code> by default uses a space, which is why you havent seen it.</li>
<li><strong>This only applies when using commas.</strong></li>
</ul>
<div class="sourceCode" id="cb20"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="bu">print</span>(<span class="st">&quot;Hi!&quot;</span>, <span class="st">&quot;Vanilla&quot;</span>, <span class="st">&quot;please,&quot;</span>, <span class="st">&quot;but&quot;</span>, <span class="st">&quot;chocolate&quot;</span>, <span class="st">&quot;is&quot;</span>, <span class="st">&quot;cool.&quot;</span>, sep<span class="op">=</span><span class="st">&quot;--&quot;</span>)</a>
<a class="sourceLine" id="cb20-2" data-line-number="2"><span class="co"># =&gt; Hi!--Vanilla,--please,--but--chocolate--is--cool.</span></a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Stress that this only works for commas, not for using <code>+</code>!</li>
<li>Note that the default value, or optional value, is what happens for an argument when you dont specify it. Well get to optional parameters next.</li>
<li>Theres a replit on the next slide to demo this.</li>
</ul>
<p>Talking Points:</p>
<ul>
<li>Remind them again that <code>print</code> is a function.</li>
<li>“Remember at the beginning of the class when we were learning variables, and we had a section on how awesome the print statement is? Well, it has even more neat options to offer!”</li>
</ul>
</aside>
<hr />
</section>
<section id="delicious-printing" class="level2">
<h2>Delicious Printing</h2>
<ul>
<li>We can replace <code>name</code> and <code>dessert</code> with your own name and favorite dessert. Its a regular print!</li>
<li><code>sep</code> can be any string, or even an icon (theyre made of strings - well see later!) - but not an int.</li>
</ul>
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/02-Python-Kwargs-6?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals">
</iframe>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>This is just a demo! Show it working and check for understanding.</li>
<li>Change the value of sep to numbers, other strings, and even back to the default space. Then, leave it out again to remind them that this is a normal print statement. Then try putting <code>sep</code> first and show that it doesnt work - its a kwarg, after all!</li>
<li>Point out the special character and how cool Python is, but dont go into unicode or how its done. We can talk about that in unit 4.</li>
</ul>
<p><strong>Repl.it Note</strong> The code here is:</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb21-1" data-line-number="1">name <span class="op">=</span> <span class="st">&quot;Brandi&quot;</span></a>
<a class="sourceLine" id="cb21-2" data-line-number="2">dessert <span class="op">=</span> <span class="st">&quot;froyo&quot;</span></a>
<a class="sourceLine" id="cb21-3" data-line-number="3"><span class="bu">print</span>(<span class="st">&quot;Hello&quot;</span>, <span class="st">&quot;my&quot;</span>, <span class="st">&quot;name&quot;</span>, <span class="st">&quot;is&quot;</span>, name, <span class="st">&quot;and&quot;</span>, <span class="st">&quot;I&quot;</span>, <span class="st">&quot;enjoy&quot;</span>, dessert, <span class="st">&quot;:)&quot;</span>)</a>
<a class="sourceLine" id="cb21-4" data-line-number="4"><span class="bu">print</span>(<span class="st">&quot;Hello&quot;</span>, <span class="st">&quot;my&quot;</span>, <span class="st">&quot;name&quot;</span>, <span class="st">&quot;is&quot;</span>, name, <span class="st">&quot;and&quot;</span>, <span class="st">&quot;I&quot;</span>, <span class="st">&quot;enjoy&quot;</span>, dessert, <span class="st">&quot;:)&quot;</span>, sep<span class="op">=</span><span class="st">&quot; HELLO &quot;</span>)</a>
<a class="sourceLine" id="cb21-5" data-line-number="5"><span class="bu">print</span>(<span class="st">&quot;Hello&quot;</span>, <span class="st">&quot;my&quot;</span>, <span class="st">&quot;name&quot;</span>, <span class="st">&quot;is&quot;</span>, name, <span class="st">&quot;and&quot;</span>, <span class="st">&quot;I&quot;</span>, <span class="st">&quot;enjoy&quot;</span>, dessert, <span class="st">&quot;:)&quot;</span>, sep<span class="op">=</span><span class="st">&quot;🍧&quot;</span>)</a></code></pre></div>
</aside>
<hr />
</section>
<section id="quick-review-1" class="level2">
<h2>Quick Review</h2>
<p>So far, weve learned:</p>
<ul>
<li><code>*args</code>:
<ul>
<li>A variable number of function arguments.</li>
</ul></li>
<li>kwargs:
<ul>
<li>A set number of function arguments.</li>
<li>Can be defined out of order</li>
</ul></li>
<li><code>**kwargs</code>:
<ul>
<li>Any number of positional arguments.</li>
</ul></li>
<li><code>sep</code> in print.</li>
</ul>
<p>Theres one more: Optional parameters.</p>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Do a quick check for understanding.</li>
</ul>
</aside>
<hr />
</section>
<section id="optional-parameters-with-default-values" class="level2">
<h2>Optional Parameters with Default Values</h2>
<p>This idea exists in programming - youve already seen it!</p>
<p>The default value for <code>sep</code> in <code>print</code> is <code>&quot; &quot;</code>. You dont <strong>need</strong> to include it.</p>
<p>This makes it optional! <strong>Optional parameters</strong> have default values, so you dont need to include them.</p>
<ul>
<li>Only include them if you want to change them!</li>
</ul>
<div class="sourceCode" id="cb22"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb22-1" data-line-number="1"></a>
<a class="sourceLine" id="cb22-2" data-line-number="2"><span class="co"># Here, `sep` is optional to include. It defaults to a space &quot; &quot;.</span></a>
<a class="sourceLine" id="cb22-3" data-line-number="3"><span class="bu">print</span>(<span class="st">&quot;Hello&quot;</span>, <span class="st">&quot;my&quot;</span>, <span class="st">&quot;name&quot;</span>, <span class="st">&quot;is&quot;</span>, name, <span class="st">&quot;and&quot;</span>, <span class="st">&quot;I&quot;</span>, <span class="st">&quot;enjoy&quot;</span>, dessert, <span class="st">&quot;:)&quot;</span>)</a>
<a class="sourceLine" id="cb22-4" data-line-number="4"></a>
<a class="sourceLine" id="cb22-5" data-line-number="5"><span class="co"># But we can include it, if we want, and `sep` will use our value instead of the space.</span></a>
<a class="sourceLine" id="cb22-6" data-line-number="6"><span class="bu">print</span>(<span class="st">&quot;Hello&quot;</span>, <span class="st">&quot;my&quot;</span>, <span class="st">&quot;name&quot;</span>, <span class="st">&quot;is&quot;</span>, name, <span class="st">&quot;and&quot;</span>, <span class="st">&quot;I&quot;</span>, <span class="st">&quot;enjoy&quot;</span>, dessert, <span class="st">&quot;:)&quot;</span>, sep<span class="op">=</span><span class="st">&quot; HELLO &quot;</span>)</a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>If they seem confused here, add more real world examples. For example…
<ul>
<li>By default, the name people call you is your legal name, but you can optionally specify a nickname - “Joseph” versus “Joe”.<br />
</li>
<li>By default, the address the mail man delivers to is your house, unless you optionally specify a P.O. Box.</li>
<li>By default, the seat you sit in is the same one every day, unless you specify you want to sit somewhere else.</li>
</ul></li>
<li>Remind them that this optional argument had to come last when calling the function.</li>
</ul>
</aside>
<p>Default parameters are in the <em>function declaration</em>.</p>
<p>Theyre there if you dont include a value.</p>
<hr />
</section>
<section id="any-functions-optional-parameters-with-default-values" class="level2">
<h2>Any Functions: Optional Parameters with Default Values</h2>
<p>These can be added to any functions.</p>
<p>Here, <code>c</code> has a default of <code>20</code>. We dont need to include it!</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb23-1" data-line-number="1"><span class="co"># Optional parameters: Default values are only used if needed.</span></a>
<a class="sourceLine" id="cb23-2" data-line-number="2"><span class="kw">def</span> my_func(a, b, c<span class="op">=</span><span class="dv">20</span>):</a>
<a class="sourceLine" id="cb23-3" data-line-number="3"> <span class="bu">print</span>(a <span class="op">+</span> b <span class="op">+</span> c)</a>
<a class="sourceLine" id="cb23-4" data-line-number="4">my_func(<span class="dv">1</span>, <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb23-5" data-line-number="5"><span class="co"># Uses the default! Prints 23.</span></a>
<a class="sourceLine" id="cb23-6" data-line-number="6">my_func(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>)</a>
<a class="sourceLine" id="cb23-7" data-line-number="7"><span class="co"># Overrides the default! Prints 7.</span></a></code></pre></div>
<aside class="notes">
<p>Teaching tips:</p>
<ul>
<li>Show this running a few ways. <a href="https://repl.it/@SuperTernary/python-programming-optional-parameters">Here is the code in a repl.it</a> - open this in a new window and talk through it.</li>
<li>For advanced students, show kwargs and optional parameters together.</li>
</ul>
<p>Talking points:</p>
<ul>
<li>What are the default values doing?</li>
<li>Why is this different than keyword arguments?</li>
</ul>
</aside>
<hr />
</section>
<section id="partner-exercise-poke-at-it" class="level2">
<h2>Partner Exercise: Poke At It!</h2>
<p>Pair up! Choose a driver and a navigator.</p>
<ul>
<li>In your local file, write a function, <code>print_food</code> that has four optional parameters (all with defaults of your choice): <code>favorite_food</code>, <code>lunch_today</code>, <code>lunch_yesterday</code>, and <code>breakfast</code>.</li>
</ul>
<p><code>print_food</code> should print out each of these.</p>
<p>Call this with a couple different arguments:</p>
<ul>
<li>No arguments.</li>
<li>All arguments - a regular function call.</li>
<li>2 keyword arguments. Give all four arguments, but use a keyword for <code>lunch_yesterday</code> and <code>breakfast</code>.</li>
<li>All keyword arguments - out of order.</li>
</ul>
<aside class="notes">
<p>Teaching tips:</p>
<p>5 minutes or so.</p>
<ul>
<li>Pair students up.</li>
<li>Walk around and offer advice, key questions, and help overcome challenges.</li>
<li>When everyone, or the majority, have it figured out, go over the answers - bring up a file and walk through this.</li>
</ul>
</aside>
<hr />
</section>
<section id="partner-exercise-keep-poking" class="level2">
<h2>Partner Exercise: Keep Poking!</h2>
<p>Change roles!</p>
<p>Underneath <code>print_food</code>, rewrite it, twice.</p>
<p>First, write <code>print_food_args</code>, using <code>*args</code> as the parameter. Start the function by printing <code>args</code>, so you can see whats going on. Then, print the values you pass in.</p>
<p>Then, write <code>print_food_kwargs</code>, using <code>**kwargs</code> as the parameter. Start the function by printing <code>kwargs</code>, so you can see whats going on. Then, as above, print the values you pass in.</p>
<aside class="notes">
<p>Teaching tips:</p>
<p>5 minutes or so.</p>
<ul>
<li>Pair students up.</li>
<li>Walk around and offer advice, key questions, and help overcome challenges.</li>
<li>When everyone, or the majority, have it figured out, go over the answers - bring up a file and walk through this.</li>
</ul>
</aside>
<hr />
</section>
<section id="summary-qa" class="level2">
<h2>Summary + Q&amp;A</h2>
<ul>
<li><code>*args</code>:
<ul>
<li>A variable number of function arguments.</li>
<li>Taken in any order.</li>
<li><code>def multiply(*args):</code></li>
</ul></li>
<li>kwargs:
<ul>
<li>A set number of function arguments.</li>
<li>Can be defined out of order</li>
<li><code>my_func(a=1, b=2, c=3)</code></li>
</ul></li>
<li><code>**kwargs</code>: - Any number of positional arguments.
<ul>
<li><code>def froyo(*kwargs)</code></li>
</ul></li>
<li>sep in print.</li>
<li>Optional parameters:
<ul>
<li>Default values in the function declaration</li>
<li><code>def my_func(a=10, b=15, c=20)</code></li>
</ul></li>
</ul>
<aside class="notes">
<p><strong>Teaching Tips</strong>:</p>
<ul>
<li>Summarize the lesson and provide a preview of whats coming next.</li>
<li>Open your own blank <a href="https://repl.it/@GAcoding/blank-repl?lite=true">repl.it</a> in a new tab if needed to recap and be sure everyone is clear.</li>
</ul>
</aside>
<hr />
</section>
<section id="additional-resources" class="level2">
<h2>Additional Resources</h2>
<ul>
<li><a href="https://repl.it/@SuperTernary/python-programming-optional-parameters">Optional Parameter Repl.it</a></li>
<li><a href="http://treyhunner.com/2018/04/keyword-arguments-in-python/">Keyword Args</a></li>
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3">Args and Kwargs</a></li>
<li><a href="https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions">Defining Functions</a></li>
</ul>
</section>
</div>
<footer><span class='slide-number'></span></footer>
</div>
<script src="../../../../lib/js/head.min.js"></script>
<script src="../../../../js/reveal.js"></script>
<script>
var dependencies = [
{ src: '../../../../lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: '../../../../plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../../../../plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '../../../../plugin/prism/prism.js', async: true, callback: function() { /*hljs.initHighlightingOnLoad();*/ } },
{ src: '../../../../plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } }
];
if (Reveal.getQueryHash().instructor === 1) {
dependencies.push({ src: '../../../../plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } });
}
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: false,
slideNumber: true,
// available themes are in /css/theme
theme: Reveal.getQueryHash().theme || 'default',
// default/cube/page/concave/zoom/linear/fade/none
transition: Reveal.getQueryHash().transition || 'slide',
// Optional libraries used to extend on reveal.js
dependencies: dependencies
});
if (Reveal.getQueryHash().instructor === 1) {
Reveal.configure(dependencies.push({ src: '../../../../plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }));
}
Reveal.addEventListener('ready', function() {
if (Reveal.getCurrentSlide().classList.contains('separator-subhead')) {
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-subhead.css');
} else if (Reveal.getCurrentSlide().classList.contains('separator')) {
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-title.css')
} else {
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga.css');
}
});
Reveal.addEventListener('slidechanged', function(e) {
if (Reveal.getCurrentSlide().classList.contains('separator-subhead')) {
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-subhead.css');
} else if (Reveal.getCurrentSlide().classList.contains('separator')) {
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga-title.css')
} else {
document.getElementById('theme').setAttribute('href', '../../../../css/theme/ga.css');
}
});
</script>
</body>
</html>