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.

734 lines
40 KiB

This file contains ambiguous Unicode 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: Intro to Intermediate Python
type: introduction
duration: "00:15"
creator: Brandi Michelle 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>
Intro to Intermediate Python
</h1>
<!--
## Overview
This lesson starts with a recap of all of the topics covered up to this point, giving students a final chance to express their questions before we move beyond the basics. Most slides are in a "Lecture + Question -> Answer" format. It then has a very quick overview of the upcoming unit — user input and file I/O, abstraction, modules, and APIs.
- As you go through this lesson, do frequent checks for understanding. It's important that students understand everything before we add more complicated things like `itertools` and APIs.
- When you get to the new unit overview, put questions in the parking lot — there's a presentation on each topic.
## Differentiation and Extensions
- If students are breezing through this lesson, that's great! Don't stop for long on each slide.
- If students are having trouble in the recap, add in We Dos.
## Learning Objectives
In this lesson, students will:
- Confidently recap the previous units.
- Describe key components of the upcoming unit.
## Duration
20 minutes
## Suggested Agenda
| Time | Activity |
| --- | --- |
| 0:00 - 0:03 | Welcome |
| 0:04 - 0:12 | Basic Topics Recap |
| 0:13 - 0:17 | Intermediate Topics Preview |
| 0:17 - 0:20 | Summary |
## In Class: Materials
- Projector
- Internet connection
- Python 3
-->
<hr />
</section>
<section id="learning-objectives" class="level2">
<h2>Learning Objectives</h2>
<p><em>After this lesson, you will be able to:</em></p>
<ul>
<li>Confidently recap the previous units.</li>
<li>Describe key components of the upcoming unit.</li>
</ul>
<aside class="notes">
<p><strong>Talking Points:</strong></p>
<ul>
<li>This lesson is pretty solidly a review.</li>
<li>Then, well very briefly introduce all of the concepts covered the next unit. Dont worry about learning them here! Its the final Python unit and it covers quite a lot, so well just give you an overview in advance.</li>
</ul>
</aside>
<hr />
</section>
<section id="leveling-up" class="level2">
<h2>Leveling Up</h2>
<p>Youre leveling up!</p>
<p>You have the proper foundation. Now, lets check how youre doing.</p>
<aside class="notes">
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Reassure learners that theyre great for hanging in this far!</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-lists" class="level2">
<h2>Lets Review: Lists</h2>
<ul>
<li>A collection of items stored in a single variable.</li>
<li>Created with square brackets (<code>[]</code>).</li>
<li>Begin counting at <code>0</code>.</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">my_queens <span class="op">=</span> [<span class="st">&quot;Cersei&quot;</span>, <span class="st">&quot;Daenerys&quot;</span>, <span class="st">&quot;Arwen&quot;</span>, <span class="st">&quot;Elsa&quot;</span>, <span class="st">&quot;Guinevere&quot;</span>]</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">step_counts_this_week <span class="op">=</span> [<span class="dv">8744</span>, <span class="dv">5256</span>, <span class="dv">7453</span>, <span class="dv">3097</span>, <span class="dv">4122</span>, <span class="dv">2908</span>, <span class="dv">6720</span>]</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co"># We can also mix types.</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">weird_list <span class="op">=</span> [<span class="dv">1</span>, <span class="st">&quot;weird&quot;</span>, [<span class="st">&quot;nested list&quot;</span>], <span class="st">&quot;eh?&quot;</span>]</a></code></pre></div>
<blockquote>
<p><strong>Challenge:</strong> Can you recall how to slice a section of the list? For example, items 2 through 5 of <code>step_counts_this_week</code>?</p>
</blockquote>
<aside class="notes">
<p><strong>Talking Points:</strong></p>
<ul>
<li>Lists are a collection of items stored in a single variable.</li>
<li>Lists can be of any type, but they are typically of related items.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="answer-lists-challenge" class="level2">
<h2>Answer: Lists Challenge</h2>
<ul>
<li>Python uses a <code>:</code> to represent a range of indices.</li>
<li>Beware of off-by-one errors!</li>
</ul>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" data-line-number="1">step_counts_this_week <span class="op">=</span> [<span class="dv">8744</span>, <span class="dv">5256</span>, <span class="dv">7453</span>, <span class="dv">3097</span>, <span class="dv">4122</span>, <span class="dv">2908</span>, <span class="dv">6720</span>]</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">days_2_thru_5 <span class="op">=</span> step_counts_this_week[<span class="dv">2</span>:<span class="dv">6</span>] <span class="co"># Items 2, 3, 4, and 5</span></a></code></pre></div>
<blockquote>
<p><strong>Pro tip:</strong> Its <code>6</code> instead of <code>5</code> because the range is exclusive.</p>
</blockquote>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Many languages have a <code>slice()</code> method, but Python simply uses the colon to represent a range of indices.&quot;</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-loops-and-iteration" class="level2">
<h2>Lets Review: Loops and Iteration</h2>
<p>What about looping a list?</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" data-line-number="1">my_queens <span class="op">=</span> [<span class="st">&quot;Cersei&quot;</span>, <span class="st">&quot;Daenerys&quot;</span>, <span class="st">&quot;Arwen&quot;</span>, <span class="st">&quot;Elsa&quot;</span>, <span class="st">&quot;Guinevere&quot;</span>]</a>
<a class="sourceLine" id="cb3-2" data-line-number="2"></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="cf">for</span> queen <span class="kw">in</span> my_queens:</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"> <span class="bu">print</span>(queen, <span class="st">&quot;is the most powerful queen!&quot;</span>)</a></code></pre></div>
<blockquote>
<p><strong>Challenge:</strong> What if I want to loop from 1 to 10 and print out the numbers? How do I do this without a data structure to loop over?</p>
</blockquote>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>We just reviewed looping over a dictionary. Lets loop over a list.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="answer-loops-challenge" class="level2">
<h2>Answer: Loops Challenge</h2>
<p>To loop 110 without a data structure:</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="co"># Remember, &quot;i&quot; is a common name for a counter/index in programming!</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">11</span>):</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"> <span class="bu">print</span>(i)</a></code></pre></div>
<ul>
<li>Why do you think we put <code>11</code> in the code?</li>
<li>What values does this print?</li>
</ul>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Remember that ranges in Python are exclusive on the end!</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-sets" class="level2">
<h2>Lets Review: Sets</h2>
<ul>
<li>Lists that dont have duplicates.</li>
<li>Created with curly braces (<code>{}</code>) or from lists with the <code>set()</code> function.</li>
<li>Arent indexed — elements are in any order!</li>
<li>Handy for storing emails, user names, and other unique elements.</li>
</ul>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1">email_set <span class="op">=</span> {<span class="st">&#39;my_email@gmail.com&#39;</span>, <span class="st">&#39;second_email@yahoo.com&#39;</span>, <span class="st">&quot;third_email@hotmail.com&quot;</span>}</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="co"># Or from a list:</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">my_list <span class="op">=</span> [<span class="st">&quot;red&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;green&quot;</span>, <span class="st">&quot;red&quot;</span>, <span class="st">&quot;green&quot;</span>]</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">my_set <span class="op">=</span> <span class="bu">set</span>(my_list)</a>
<a class="sourceLine" id="cb5-5" data-line-number="5"><span class="co"># =&gt; {&quot;red&quot;, &quot;yellow&quot;, &quot;green&quot;}</span></a></code></pre></div>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Sets are lists that dont have duplicates.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-tuples" class="level2">
<h2>Lets Review: Tuples</h2>
<ul>
<li>Lists that cant be changed!</li>
<li>Created with parentheses (<code>()</code>).</li>
<li>Cant add, pop, remove, or otherwise change elements after creation.</li>
</ul>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1">rainbow_colors_tuple <span class="op">=</span> (<span class="st">&quot;red&quot;</span>, <span class="st">&quot;orange&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;green&quot;</span>, <span class="st">&quot;blue&quot;</span>, <span class="st">&quot;indigo&quot;</span>, <span class="st">&quot;violet&quot;</span>)</a></code></pre></div>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Tuples are lists that cant be changed.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-dictionaries" class="level2">
<h2>Lets Review: Dictionaries</h2>
<ul>
<li>A collection of key-value pairs.</li>
<li>Created with curly braces (<code>{key: value, key: value}</code>).</li>
<li>Values can be anything!</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">my_puppy <span class="op">=</span> {</a>
<a class="sourceLine" id="cb7-2" data-line-number="2"> <span class="st">&quot;name&quot;</span>: <span class="st">&quot;Fido&quot;</span>,</a>
<a class="sourceLine" id="cb7-3" data-line-number="3"> <span class="st">&quot;breed&quot;</span>: <span class="st">&quot;Corgi&quot;</span>,</a>
<a class="sourceLine" id="cb7-4" data-line-number="4"> <span class="st">&quot;age&quot;</span>: <span class="dv">3</span>,</a>
<a class="sourceLine" id="cb7-5" data-line-number="5"> <span class="st">&quot;vaccinated&quot;</span>: <span class="va">True</span>,</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"> <span class="st">&quot;fave toy&quot;</span>: [<span class="st">&quot;chew sticks&quot;</span>, <span class="st">&quot;big sticks&quot;</span>, <span class="st">&quot;any sticks&quot;</span>]</a>
<a class="sourceLine" id="cb7-7" data-line-number="7">}</a></code></pre></div>
<blockquote>
<p><strong>Challenge:</strong> Can you recall how to iterate (loop) over each key of <code>my_puppy</code> and print out both the key and the corresponding value?</p>
</blockquote>
<aside class="notes">
<p><strong>Talking Points:</strong></p>
<ul>
<li>Dictionaries are made of key-value pairs.</li>
<li>We split lines for readability.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="answer-dictionaries-challenge" class="level2">
<h2>Answer: Dictionaries Challenge</h2>
<p>Iterating a dictionary is similar to a list:</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="cf">for</span> key <span class="kw">in</span> my_puppy:</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"> <span class="bu">print</span>(key, <span class="st">&quot;-&quot;</span>, my_puppy[key])</a></code></pre></div>
<p>Outputs:</p>
<pre><code>name - Fido
breed - Corgi
age - 3
vaccinated - True
fave toy - chew sticks</code></pre>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Dictionaries are made of key-value pairs.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-functions" class="level2">
<h2>Lets Review: Functions</h2>
<ul>
<li>Bits of code that can be used repeatedly.</li>
<li>Enable DRY — Dont Repeat Yourself.</li>
<li>Declared with <code>def</code>, <code>()</code>, and <code>:</code>.</li>
<li>Declare the function <em>above</em> the function call!</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="co"># Function definition:</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="kw">def</span> say_hello():</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"> <span class="bu">print</span>(<span class="st">&quot;hello!&quot;</span>)</a>
<a class="sourceLine" id="cb10-4" data-line-number="4"></a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="co"># Run the function three times.</span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6">say_hello()</a>
<a class="sourceLine" id="cb10-7" data-line-number="7">say_hello()</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">say_hello()</a></code></pre></div>
<aside class="notes">
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-function-parameters" class="level2">
<h2>Lets Review: Function Parameters</h2>
<p>Parameters are in the function definition.</p>
<ul>
<li>Arguments are in the function call.</li>
<li>Useful for very similar code with only minor variations.</li>
</ul>
<p><strong>Challenge:</strong> Rewrite the code below to use a single function with one parameter.</p>
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-intro-intermed?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><strong>Teaching Tips:</strong></p>
<ul>
<li>This is long; encourage students to open it in a new window.</li>
<li>Review the answer afterward.</li>
</ul>
<p><strong>Talking Point:</strong></p>
<ul>
<li>Parameters to functions allow us to pass in arguments to use within the function. This is useful when you have very similar code.</li>
</ul>
<p><strong>Repl.it note:</strong> The code here is:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="co"># Function definitions:</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="kw">def</span> say_hello_ada():</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"> <span class="bu">print</span>(<span class="st">&quot;hello, Ada&quot;</span>)</a>
<a class="sourceLine" id="cb11-4" data-line-number="4"></a>
<a class="sourceLine" id="cb11-5" data-line-number="5"><span class="kw">def</span> say_hello_alan():</a>
<a class="sourceLine" id="cb11-6" data-line-number="6"> <span class="bu">print</span>(<span class="st">&quot;hello, Alan&quot;</span>)</a>
<a class="sourceLine" id="cb11-7" data-line-number="7"></a>
<a class="sourceLine" id="cb11-8" data-line-number="8"><span class="kw">def</span> say_hello_linus():</a>
<a class="sourceLine" id="cb11-9" data-line-number="9"> <span class="bu">print</span>(<span class="st">&quot;hello, Linus&quot;</span>)</a>
<a class="sourceLine" id="cb11-10" data-line-number="10"></a>
<a class="sourceLine" id="cb11-11" data-line-number="11"><span class="co"># Call the functions:</span></a>
<a class="sourceLine" id="cb11-12" data-line-number="12">say_hello_ada()</a>
<a class="sourceLine" id="cb11-13" data-line-number="13">say_hello_alan()</a>
<a class="sourceLine" id="cb11-14" data-line-number="14">say_hello_linus()</a></code></pre></div>
<blockquote>
<p><strong>Challenge:</strong> Could we do this with a single function that has a parameter called “name”?</p>
</blockquote>
</aside>
<hr />
</section>
<section id="function-parameters-solution" class="level2">
<h2>Function Parameters: Solution</h2>
<iframe height="400px" width="100%" src="https://repl.it/@sonylnagale/python-programming-intro-intermed-helloperson-solution?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>
<hr />
</section>
<section id="lets-review-return-statements" class="level2">
<h2>Lets Review: Return Statements</h2>
<ul>
<li>Bring data out of a function.</li>
<li>Cause the function to exit.</li>
<li>Arent a <code>print</code> statement!</li>
</ul>
<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> multiply(x, y):</a>
<a class="sourceLine" id="cb12-2" data-line-number="2"> <span class="cf">return</span> x <span class="op">*</span> y</a>
<a class="sourceLine" id="cb12-3" data-line-number="3"></a>
<a class="sourceLine" id="cb12-4" data-line-number="4">result <span class="op">=</span> multiply(<span class="dv">3</span>, <span class="dv">4</span>) <span class="co"># Result is now equal to 12.</span></a></code></pre></div>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Return statements bring data out of a function. They are not the same thing as <code>print</code> statements, which simply output text.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Quickly check for understanding; bring up an interpreter, file, or blank repl.it to demo only if needed.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-classes" class="level2">
<h2>Lets Review: Classes</h2>
<ul>
<li>Templates (aka, blueprints) for objects.</li>
<li>Can contain methods and/or variables.</li>
<li><code>self</code> is a reference to the created object.</li>
</ul>
<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">class</span> Animal():</a>
<a class="sourceLine" id="cb13-2" data-line-number="2"> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb13-3" data-line-number="3"> <span class="va">self</span>.energy <span class="op">=</span> <span class="dv">50</span></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"></a>
<a class="sourceLine" id="cb13-5" data-line-number="5"> <span class="kw">def</span> get_status(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb13-6" data-line-number="6"> <span class="cf">if</span> <span class="va">self</span>.energy <span class="op">&lt;</span> <span class="dv">20</span>:</a>
<a class="sourceLine" id="cb13-7" data-line-number="7"> <span class="bu">print</span>(<span class="st">&quot;I&#39;m hungry!&quot;</span>)</a>
<a class="sourceLine" id="cb13-8" data-line-number="8"> <span class="cf">elif</span> <span class="va">self</span>.energy <span class="op">&gt;</span> <span class="dv">100</span>:</a>
<a class="sourceLine" id="cb13-9" data-line-number="9"> <span class="bu">print</span>(<span class="st">&quot;I&#39;m stuffed!&quot;</span>)</a>
<a class="sourceLine" id="cb13-10" data-line-number="10"> <span class="cf">else</span>:</a>
<a class="sourceLine" id="cb13-11" data-line-number="11"> <span class="bu">print</span>(<span class="st">&quot;I&#39;m doing well!&quot;</span>)</a></code></pre></div>
<blockquote>
<p><strong>Challenge:</strong> How do you declare a new <code>Animal</code>?</p>
</blockquote>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Classes are essentially blueprints for object-making factories. They can be used to make several objects of the same type.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Answer on the next slide.</li>
</ul>
</aside>
<hr />
</section>
<section id="answer-classes" class="level2">
<h2>Answer: Classes</h2>
<p>Declaring a new <code>Animal</code> from the class:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" data-line-number="1">my_animal <span class="op">=</span> Animal() <span class="co"># Creates a new Animal instance.</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">my_animal.get_status() <span class="co"># Prints &quot;I&#39;m doing well!&quot;</span></a>
<a class="sourceLine" id="cb14-3" data-line-number="3">my_animal.energy <span class="op">+=</span> <span class="dv">100</span> <span class="co"># We can access properties!</span></a>
<a class="sourceLine" id="cb14-4" data-line-number="4">my_animal.get_status() <span class="co"># Prints &quot;I&#39;m stuffed!&quot;</span></a></code></pre></div>
<aside class="notes">
<p><strong>Teaching Tips:</strong></p>
<ul>
<li>Review the answer with the class and demonstrate how there may be multiple animals created.</li>
<li>Show how we can directly access properties.</li>
</ul>
</aside>
<hr />
</section>
<section id="lets-review-inheritance" class="level2">
<h2>Lets Review: Inheritance</h2>
<p>A class can inherit properties and methods from another class.</p>
<p><strong>You Do:</strong> Create a new class, <code>Dog</code>, which inherits from <code>Animal</code>.</p>
<ul>
<li><code>Dog</code> has an extra function, <code>bark()</code>, that prints <code>&quot;bark&quot;</code>.</li>
<li><code>Dog</code> has an extra property, <code>breed</code>.</li>
</ul>
<iframe height="400px" width="100%" src="https://repl.it/@sonylnagale/python-programming-intro-inter-classes?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><strong>Talking Point:</strong></p>
<ul>
<li>Inheritance is all about reusing code. You may have two classes that are related. Inheritance allows you to take advantage of that.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>After a minute, go over the answer:</li>
</ul>
<p><strong>Repl.it note: Here is our <code>Animal()</code> class</strong></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">class</span> Animal():</a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="va">self</span>.energy <span class="op">=</span> <span class="dv">50</span></a>
<a class="sourceLine" id="cb15-4" data-line-number="4"></a>
<a class="sourceLine" id="cb15-5" data-line-number="5"><span class="kw">def</span> get_status(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb15-6" data-line-number="6"><span class="cf">if</span> <span class="va">self</span>.energy <span class="op">&lt;</span> <span class="dv">20</span>:</a>
<a class="sourceLine" id="cb15-7" data-line-number="7"> <span class="bu">print</span>(<span class="st">&quot;I&#39;m hungry!&quot;</span>)</a>
<a class="sourceLine" id="cb15-8" data-line-number="8"><span class="cf">elif</span> <span class="va">self</span>.energy <span class="op">&gt;</span> <span class="dv">100</span>:</a>
<a class="sourceLine" id="cb15-9" data-line-number="9"> <span class="bu">print</span>(<span class="st">&quot;I&#39;m stuffed!&quot;</span>)</a>
<a class="sourceLine" id="cb15-10" data-line-number="10"><span class="cf">else</span>:</a>
<a class="sourceLine" id="cb15-11" data-line-number="11"> <span class="bu">print</span>(<span class="st">&quot;I&#39;m doing well!&quot;</span>)</a>
<a class="sourceLine" id="cb15-12" data-line-number="12"></a>
<a class="sourceLine" id="cb15-13" data-line-number="13"><span class="co"># Directions Part 1: Create a class, `Dog`.</span></a>
<a class="sourceLine" id="cb15-14" data-line-number="14"><span class="co"># * `Dog` inherits from `Animal`.</span></a>
<a class="sourceLine" id="cb15-15" data-line-number="15"><span class="co"># * `Dog` has an extra function, `bark`.</span></a>
<a class="sourceLine" id="cb15-16" data-line-number="16"><span class="co"># * `Dog` has an extra property, `breed`.</span></a>
<a class="sourceLine" id="cb15-17" data-line-number="17"></a>
<a class="sourceLine" id="cb15-18" data-line-number="18"><span class="co"># Directions Part 2: Declare a new dog.</span></a>
<a class="sourceLine" id="cb15-19" data-line-number="19"><span class="co"># * Call the `bark()` function.</span></a>
<a class="sourceLine" id="cb15-20" data-line-number="20"><span class="co"># * Give the dog a breed.</span></a></code></pre></div>
</aside>
<hr />
</section>
<section id="inheritance-answer" class="level2">
<h2>Inheritance: Answer</h2>
<iframe height="400px" width="100%" src="https://repl.it/@sonylnagale/python-programming-intro-inheritance-answer?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>
<hr />
</section>
<section id="knowledge-check" class="level2">
<h2>Knowledge Check</h2>
<p>Were about to move on to the next unit: Intermediate Python.</p>
<p>Any questions?</p>
<blockquote>
<p>Dont be shy! If you have a question, so do others!</p>
</blockquote>
<aside class="notes">
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>Encourage questions. Remind students that there are no bad ones.</li>
</ul>
</aside>
<hr />
</section>
<section id="switching-gears-preview" class="level2">
<h2>Switching Gears: Preview</h2>
<p>The next unit covers many topics, including:</p>
<ul>
<li>User input</li>
<li>File I/O</li>
<li>Abstraction</li>
<li>Modules and libraries</li>
<li>APIs</li>
</ul>
<p>You dont need to memorize them now! This is just an overview.</p>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>Heres a preview of whats coming. Well have lessons on the following topics.</li>
</ul>
<p><strong>Teaching Tip:</strong></p>
<ul>
<li>The idea is not to teach these topics in this lesson. Instead, give students a quick demo or a few talking points so they have an idea of whats coming.</li>
</ul>
</aside>
<hr />
</section>
<section id="user-input-and-file-io" class="level2">
<h2>User Input and File I/O</h2>
<p>Youve seen this a few times already with <code>input()</code>.</p>
<p>Well build real interactions between your Python programs and other files — or the person using your app!</p>
<aside class="notes">
<p><strong>Talking Point:</strong></p>
<ul>
<li>In this section well cover opening, reading, writing, and closing files, as well as browsing directory contents.</li>
</ul>
</aside>
<hr />
</section>
<section id="abstraction" class="level2">
<h2>Abstraction</h2>
<p>Python has built-in functions for performing common tasks.</p>
<p>Youve seen things like <code>my_list.len()</code>, which tells you the length of a list.</p>
<p>But Python has more specialized built-in functions, like chaining lists together:</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb16-1" data-line-number="1">food <span class="op">=</span> [<span class="st">&#39;pizza&#39;</span>, <span class="st">&#39;tacos&#39;</span>, <span class="st">&#39;sushi&#39;</span>]</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">colors <span class="op">=</span> [<span class="st">&#39;red&#39;</span>, <span class="st">&#39;green&#39;</span>]</a>
<a class="sourceLine" id="cb16-3" data-line-number="3"><span class="co"># =&gt; lists_chained =[&#39;pizza&#39;, &#39;tacos&#39;, &#39;sushi&#39;, &#39;red&#39;, &#39;green&#39;]</span></a></code></pre></div>
<p>This helps you get complex things done more quickly.</p>
<p>Well learn several of these.</p>
<aside class="notes">
<p><strong>Teaching Tip:</strong> - Abstraction is best explained in an example. Ask the students to come up with their own examples.</p>
<strong>Talking Point:</strong> - A rectangle is a simple concept… or is it? Can you describe the definition of a rectangle?
</aside>
<hr />
</section>
<section id="modules-and-libraries" class="level2">
<h2>Modules and Libraries</h2>
<p>We mentioned these in the pre-work!</p>
<p>Modules and libraries are:</p>
<ul>
<li>Code that others have written.</li>
<li>Free to use!</li>
<li>Useful extensions of the Python language (e.g., a fancy date and time formatter).</li>
</ul>
<p>This one tells us when Mothers Day is for a given year:</p>
<iframe height="300px" width="100%" src="https://repl.it/@SuperTernary/python-programming-intro-inter-modules?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><strong>Teaching Tip:</strong></p>
<ul>
<li>This is just a demo slide — run it and mention what it does. Dont go into modules or syntax, etc.</li>
</ul>
<p><strong>Talking Points:</strong></p>
<ul>
<li>We can use code other people have written. Here, we can get the date of Mothers Day from 2013 with just one line of code!</li>
</ul>
<p><strong>Repl.it note:</strong> This repl.it has:</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="im">from</span> pytime <span class="im">import</span> pytime</a>
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="co"># Now we can use any function in the datetime module.</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3"></a>
<a class="sourceLine" id="cb17-4" data-line-number="4"><span class="bu">print</span>(pytime.mother(<span class="dv">2013</span>))</a></code></pre></div>
</aside>
<hr />
</section>
<section id="what-is-an-api" class="level2">
<h2>What Is an API?</h2>
<p>Not only can we use code other people have written; we can also use data that theyve made available to us.</p>
<p>We can incorporate stocks, movie ratings, or GIFs from the internet into your program!</p>
<p>This API lists <em>Star Wars</em> characters.</p>
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-intro-intermediate-apis?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><strong>Teaching Tip:</strong></p>
<ul>
<li>This is just a demo slide — run it and mention what it does. Dont go into modules or syntax, etc.</li>
</ul>
<p><strong>Talking Point:</strong></p>
<ul>
<li>“API” is a very general term. Usually we actually mean some information from other people.</li>
</ul>
<p><strong>Repl.it note:</strong> This code is:</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="co"># Import requests module.</span></a>
<a class="sourceLine" id="cb18-2" data-line-number="2"><span class="im">import</span> requests</a>
<a class="sourceLine" id="cb18-3" data-line-number="3"></a>
<a class="sourceLine" id="cb18-4" data-line-number="4"><span class="co"># Call the Star Wars API (swapi).</span></a>
<a class="sourceLine" id="cb18-5" data-line-number="5">res <span class="op">=</span> requests.get(<span class="st">&#39;https://swapi.co/api/people&#39;</span>).json()</a>
<a class="sourceLine" id="cb18-6" data-line-number="6"></a>
<a class="sourceLine" id="cb18-7" data-line-number="7"><span class="co"># Print the result count.</span></a>
<a class="sourceLine" id="cb18-8" data-line-number="8"><span class="bu">print</span>(<span class="st">&quot;found&quot;</span>, res[<span class="st">&quot;count&quot;</span>], <span class="st">&quot;results. Here are the first 10:</span><span class="ch">\n</span><span class="st">&quot;</span>)</a>
<a class="sourceLine" id="cb18-9" data-line-number="9"></a>
<a class="sourceLine" id="cb18-10" data-line-number="10"><span class="co"># Loop through characters: Append to file and print to screen</span></a>
<a class="sourceLine" id="cb18-11" data-line-number="11"><span class="cf">for</span> person <span class="kw">in</span> res[<span class="st">&quot;results&quot;</span>]:</a>
<a class="sourceLine" id="cb18-12" data-line-number="12"><span class="bu">print</span>(person[<span class="st">&quot;name&quot;</span>])</a></code></pre></div>
</aside>
<hr />
</section>
<section id="summary-and-qa" class="level2">
<h2>Summary and Q&amp;A</h2>
<p>We reviewed topics from earlier lessons:</p>
<ul>
<li>Lists, sets, tuples, and dictionaries.</li>
<li>Loops and iteration.</li>
<li>Functions, parameters, and return statements.</li>
<li>Classes and inheritance.</li>
</ul>
<p>We brushed the surface on some upcoming topics:</p>
<ul>
<li>User input and file I/O.</li>
<li>Abstraction.</li>
<li>Modules and libraries.</li>
<li>APIs.</li>
</ul>
<p>Lets jump in to it!</p>
<aside class="notes">
<p><strong>Teaching Tips:</strong></p>
<ul>
<li>Address any questions from earlier lessons.</li>
<li>If there are questions on the topics for the upcoming unit, put them in the parking lot — this is just a general overview and there are full presentations on each of these.</li>
</ul>
</aside>
<hr />
</section>
<section id="additional-reading-and-resources" class="level2">
<h2>Additional Reading and Resources</h2>
<p>Now that you have an understanding of basic programming, here are some cool people to read about:</p>
<ul>
<li><strong><a href="https://en.wikipedia.org/wiki/Ada_Lovelace">Ada Lovelace</a>:</strong> Regarded as the first programmer.</li>
<li><strong><a href="https://en.wikipedia.org/wiki/Alan_Turing">Alan Turing</a>:</strong> Considered the father of theoretical computer and artificial intelligence; helped crack the enigma code during World War II.</li>
<li><strong><a href="https://en.wikipedia.org/wiki/Linus_Torvalds">Linus Torvalds</a>:</strong> Creator of Linux OS and Git.</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>