|
|
<!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: Dictionaries
|
|
|
type: lesson
|
|
|
duration: 00:30
|
|
|
creator: Susi Remondi
|
|
|
Private gist location: xxxxxx
|
|
|
Presentation URL: xxxxx
|
|
|
-->
|
|
|
<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: Dictionaries
|
|
|
</h1>
|
|
|
<!--
|
|
|
|
|
|
|
|
|
## Overview
|
|
|
This lesson introduces students to the concept of dictionaries. It begins with simple creation, printing, and changing values. It ends with a series of You Do exercises to build students' confidence.
|
|
|
|
|
|
## Important Notes or Prerequisites
|
|
|
This is the beginning of the object-oriented programming unit. Take a few minutes before starting this lesson to recap the idea of lists and storing elements in objects.
|
|
|
|
|
|
## Learning Objectives
|
|
|
In this lesson, students will:
|
|
|
- Perform common dictionary actions.
|
|
|
- Build more complex dictionaries.
|
|
|
|
|
|
## Duration
|
|
|
30 minutes
|
|
|
|
|
|
### Timing note:
|
|
|
- If there's time remaining at the end, it'd be great to give exercises involving multiple types of values in dictionaries.
|
|
|
- There is, in the `xx-additional-exercises` folder in the parent folder, a reverse lookup challenge that you should assign in class if there's time. If not, assign it as homework.
|
|
|
|
|
|
## Suggested Agenda
|
|
|
|
|
|
| Time | Activity |
|
|
|
| --- | --- |
|
|
|
| 0:00 - 0:03 | Welcome |
|
|
|
| 0:03 - 0:12 | Introducing Dictionaries |
|
|
|
| 0:12 - 0:27 | Complex Thoughts |
|
|
|
| 0:27 - 0:30 | 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>Perform common dictionary actions.</li>
|
|
|
<li>Build more complex dictionaries.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Quickly go over these learning objectives.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="kicking-off-unit-3" class="level2">
|
|
|
<h2>Kicking Off Unit 3</h2>
|
|
|
<p>In Unit 2, we ended by changing what our movie app printed depending on the value of a variable.</p>
|
|
|
<p>Unit 3 is about <strong>objects</strong> in programming.</p>
|
|
|
<ul>
|
|
|
<li>Objects are different kinds of things variables can hold.</li>
|
|
|
<li>Objects help give our programs more structure and functionality.</li>
|
|
|
<li>You already have one object down! Lists are an object with built-in functionality like <code>append()</code> and <code>pop()</code>.</li>
|
|
|
</ul>
|
|
|
<p>In Unit 3, we’re going to add many more objects. By the end, your movie app will have the same functionality, but it will be structured in a totally different way.</p>
|
|
|
<p>Ready? Let’s go!</p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Recap what students learned in Unit 2. Give a quick overview of what they’ll learn in Unit 3.</li>
|
|
|
<li>Feel free to speak about your own definition of “object-oriented programming” here.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Now that you have a feel for control flow, let’s talk about objects.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="introducing-dictionaries" class="level2">
|
|
|
<h2>Introducing Dictionaries</h2>
|
|
|
<p>Think about dictionaries — they’re filled with words and definitions that are paired together.</p>
|
|
|
<p>Programming has a dictionary object just like this!</p>
|
|
|
<ul>
|
|
|
<li>Dictionaries hold keys (words) and values (the definitions).</li>
|
|
|
<li>In a real dictionary, you can look up a word and find the definition. In a Python dictionary, you can look up a key and find the value.</li>
|
|
|
</ul>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="introducing-dictionaries-1" class="level2">
|
|
|
<h2>Introducing Dictionaries</h2>
|
|
|
<p><img src="https://s3.amazonaws.com/ga-instruction/assets/python-fundamentals/dictionary.png" /></p>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is a pretty quick slide. Make sure everyone has the idea of looking up a word in a dictionary in their head.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>We can have an object that holds keys (<code>'puppy'</code>, <code>'pineapple'</code>, <code>'tea'</code>) and their values (<code>'furry, energetic animal'</code>, etc.).</li>
|
|
|
<li>Just like you can look up a word and find the definition in a real dictionary, in a Python dictionary you can look up a key and find the value.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="declaring-a-dictionary" class="level2">
|
|
|
<h2>Declaring a Dictionary</h2>
|
|
|
<p>Dictionaries in programming are made of <strong>key-value pairs</strong>.</p>
|
|
|
<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"># Here's the syntax:</span></a>
|
|
|
<a class="sourceLine" id="cb1-2" data-line-number="2">name_of_dictionary <span class="op">=</span> {<span class="st">"Key1"</span>: <span class="st">"Value"</span>, <span class="st">"Key2"</span>: <span class="st">"Value"</span>, <span class="st">"Key3"</span>: <span class="st">"Value"</span>}</a>
|
|
|
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="bu">print</span>(name_of_dictionary[key_to_look_up])</a>
|
|
|
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co"># Prints the value</span></a>
|
|
|
<a class="sourceLine" id="cb1-5" data-line-number="5"></a>
|
|
|
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="co"># And in action...</span></a>
|
|
|
<a class="sourceLine" id="cb1-7" data-line-number="7">my_dictionary <span class="op">=</span> {<span class="st">"Puppy"</span>: <span class="st">"Furry, energetic animal"</span>, <span class="st">"Pineapple"</span>: <span class="st">"Acidic tropical fruit"</span>, <span class="st">"Tea"</span>: <span class="st">"Herb-infused drink"</span>}</a>
|
|
|
<a class="sourceLine" id="cb1-8" data-line-number="8"></a>
|
|
|
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="bu">print</span>(my_dictionary)</a>
|
|
|
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="co"># Prints the whole dictionary</span></a>
|
|
|
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="bu">print</span>(my_dictionary[<span class="st">"Puppy"</span>])</a>
|
|
|
<a class="sourceLine" id="cb1-12" data-line-number="12"><span class="co"># => Prints Puppy's value: "Furry, energetic animal"</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Walk through the syntax:
|
|
|
<ul>
|
|
|
<li>Curly braces.</li>
|
|
|
<li>Colons between keys and values.</li>
|
|
|
<li>Commas between entries. The commas can be hard to see.</li>
|
|
|
</ul></li>
|
|
|
<li>Stress that this is basically just like a real dictionary.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Just like you can look up any word in the dictionary to get the definition, you can just use a key in a dictionary to find the value. This is known as a key-value pair, hence the definition of a dictionary: a set of key-value pairs.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-dictionaries-and-quick-tips" class="level2">
|
|
|
<h2>We Do: Dictionaries and Quick Tips</h2>
|
|
|
<p>The order of keys you see printed may differ from how you entered them. That’s fine!</p>
|
|
|
<p>You can’t have the same key twice. Imagine having two “puppies” in a real dictionary! If you try, the last value will be the one that’s kept.</p>
|
|
|
<p>What’s more, printing a key that doesn’t exist gives an error.</p>
|
|
|
<p>Let’s create a dictionary together.</p>
|
|
|
<iframe height="300px" width="100%" src="https://repl.it/@GAcoding/blank-repl?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>Create a dictionary; print it out; print out a few keys.</li>
|
|
|
<li>Make your dictionary anything you’d like! Get input from the students on what it should be.</li>
|
|
|
<li>Dictionaries can be tough, especially with all the syntax. The more practice, the better! Make sure they follow along.</li>
|
|
|
<li>Try other cases: Use an integer for a key. Try printing a value and show that it fails — just like a real dictionary.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Keys can be a string or integer. (Show this.)</li>
|
|
|
<li>You can look up any key — but not the value (just like a real dictionary!).</li>
|
|
|
<li>Use meaningful keys: <code>my_zip_code</code> is better than <code>some_numbers</code>.</li>
|
|
|
<li>The items that are returned when you access a dictionary come in any order they please.
|
|
|
<ul>
|
|
|
<li>This doesn’t really matter that much, however, because the typical use case for a dictionary is when you know the exact key for the value you’re looking for.</li>
|
|
|
<li>But, you should never count on the contents of a dictionary being in any order at all.</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-dictionary-syntax" class="level2">
|
|
|
<h2>We Do: Dictionary Syntax</h2>
|
|
|
<p>What if a value changes? We can reassign a key’s value: <code>my_dictionary["Puppy"] = "Cheerful"</code>.</p>
|
|
|
<p>What if we have new things to add? It’s the same syntax as changing the value, just with a new key: <code>my_dictionary["Yoga"] = "Peaceful"</code>.</p>
|
|
|
<ul>
|
|
|
<li>Changing values is case sensitive — be careful not to add a new key!</li>
|
|
|
</ul>
|
|
|
<iframe height="300px" width="100%" src="https://repl.it/@SuperTernary/python-programming-dictionary-change-key?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>Be sure students are doing this with you. The more practice, the better.</li>
|
|
|
<li>Demonstrate how to change and add new keys. Print out the dictionary after each one.</li>
|
|
|
<li>Show that capitalization matters!</li>
|
|
|
</ul>
|
|
|
<p><strong>Repl.it note</strong>: This repl.it has:</p>
|
|
|
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" data-line-number="1">my_dictionary <span class="op">=</span> {<span class="st">"Puppy"</span>: <span class="st">"Furry, energetic animal"</span>, <span class="st">"Pineapple"</span>: <span class="st">"Acidic tropical fruit"</span>, <span class="st">"Tea"</span>: <span class="st">"Herb-infused drink"</span>}</a>
|
|
|
<a class="sourceLine" id="cb2-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="bu">print</span>(my_dictionary)</a></code></pre></div>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="quick-review-dictionaries" class="level2">
|
|
|
<h2>Quick Review: Dictionaries</h2>
|
|
|
<p>We can:</p>
|
|
|
<ul>
|
|
|
<li>Make a dictionary.</li>
|
|
|
<li>Print a dictionary.</li>
|
|
|
<li>Print one key’s value.</li>
|
|
|
<li>Change a key’s value.</li>
|
|
|
</ul>
|
|
|
<p>Here’s a best practice: Declare your dictionary across multiple lines for readability. Which is better?</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"># This works but is not proper style.</span></a>
|
|
|
<a class="sourceLine" id="cb3-2" data-line-number="2">my_dictionary <span class="op">=</span> {<span class="st">"Puppy"</span>: <span class="st">"Furry, energetic animal"</span>, <span class="st">"Pineapple"</span>: <span class="st">"Acidic tropical fruit"</span>, <span class="st">"Tea"</span>: <span class="st">"Herb-infused drink"</span>}</a>
|
|
|
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="co"># Do this instead!</span></a>
|
|
|
<a class="sourceLine" id="cb3-5" data-line-number="5">my_dictionary <span class="op">=</span> {</a>
|
|
|
<a class="sourceLine" id="cb3-6" data-line-number="6"> <span class="st">"Puppy"</span>: <span class="st">"Furry, energetic animal"</span>,</a>
|
|
|
<a class="sourceLine" id="cb3-7" data-line-number="7"> <span class="st">"Pineapple"</span>: <span class="st">"Acidic tropical fruit"</span>,</a>
|
|
|
<a class="sourceLine" id="cb3-8" data-line-number="8"> <span class="st">"Tea"</span>: <span class="st">"Herb-infused drink"</span></a>
|
|
|
<a class="sourceLine" id="cb3-9" data-line-number="9">}</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p>2 MINUTES</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Do a quick check for understanding, then mention the line breaks. Make sure it’s clear that the breaks are the only change.</li>
|
|
|
<li>Make sure that students can do this thus far. Perhaps open an interpreter.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>We still have our <strong>key-value</strong> pair format separated by commas, however, making it easily readable to a human, which is a good thing.</li>
|
|
|
<li>Each item is on one line ending with a comma.</li>
|
|
|
<li>We can do this with any dictionary, regardless of its contents.</li>
|
|
|
<li>The syntax will remain the same.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="discussion-collection-identification-practice" class="level2">
|
|
|
<h2>Discussion: Collection Identification Practice</h2>
|
|
|
<p>What are <code>a</code> and <code>b</code> below?:</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"># What object is this?</span></a>
|
|
|
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb4-3" data-line-number="3">collection_1 <span class="op">=</span> [<span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">7</span>, <span class="st">"nine"</span>]</a>
|
|
|
<a class="sourceLine" id="cb4-4" data-line-number="4"></a>
|
|
|
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="co"># What object is this?</span></a>
|
|
|
<a class="sourceLine" id="cb4-6" data-line-number="6">collection_2 <span class="op">=</span> {<span class="st">"three"</span>: <span class="dv">3</span>, <span class="st">"five"</span>: <span class="dv">5</span>, <span class="dv">9</span>: <span class="st">"nine"</span>}</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p>1 MINUTE</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is to quickly see if students can identify lists versus dictionaries.</li>
|
|
|
<li>Talk about the difference between an “object” and a “collection” at a high level.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="looping-through-dictionaries" class="level2">
|
|
|
<h2>Looping Through Dictionaries</h2>
|
|
|
<p>We can print a dictionary with <code>print(my_dictionary)</code>, but, like a list, we can also loop through the items with a <code>for</code> loop:</p>
|
|
|
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-dictionary-for-loop?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 just a quick demo, not an exercise. Walk through this to show it working; describe what each line does.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points:</strong></p>
|
|
|
<ul>
|
|
|
<li>Now, how do we loop through a dictionary? We can use the same <code>for</code> loop structure, but, instead of it iterating over each element in the collection, it will iterate over each key. We can then use the key to access the value.</li>
|
|
|
<li>The lines in our <code>for</code> loop work like this:
|
|
|
<ul>
|
|
|
<li>The first line says, “For every key in the <code>friendzips</code> dictionary…”</li>
|
|
|
<li>The second line says, “… print the value in <code>friendzips</code> associated with the provided key.”</li>
|
|
|
</ul></li>
|
|
|
</ul>
|
|
|
<p><strong>Repl.it note:</strong> This repl.it has:</p>
|
|
|
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1">my_dictionary <span class="op">=</span> {</a>
|
|
|
<a class="sourceLine" id="cb5-2" data-line-number="2"> <span class="st">"Puppy"</span>: <span class="st">"Furry, energetic animal"</span>,</a>
|
|
|
<a class="sourceLine" id="cb5-3" data-line-number="3"> <span class="st">"Pineapple"</span>: <span class="st">"Acidic tropical fruit"</span>,</a>
|
|
|
<a class="sourceLine" id="cb5-4" data-line-number="4"> <span class="st">"Tea"</span>: <span class="st">"Herb-infused drink"</span></a>
|
|
|
<a class="sourceLine" id="cb5-5" data-line-number="5">}</a>
|
|
|
<a class="sourceLine" id="cb5-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb5-7" data-line-number="7"><span class="cf">for</span> key <span class="kw">in</span> my_dictionary:</a>
|
|
|
<a class="sourceLine" id="cb5-8" data-line-number="8"> <span class="bu">print</span>(my_dictionary[key])</a>
|
|
|
<a class="sourceLine" id="cb5-9" data-line-number="9"></a>
|
|
|
<a class="sourceLine" id="cb5-10" data-line-number="10"><span class="cf">for</span> key <span class="kw">in</span> my_dictionary:</a>
|
|
|
<a class="sourceLine" id="cb5-11" data-line-number="11"> <span class="bu">print</span>(key, <span class="st">":"</span>, my_dictionary[key])</a></code></pre></div>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="partner-exercise-dictionary-practice" class="level2">
|
|
|
<h2>Partner Exercise: Dictionary Practice</h2>
|
|
|
<p>You know the drill: Grab a partner and pick a driver!</p>
|
|
|
<p>Create a new local file, <code>dictionary_practice.py</code>. Write a script that declares a dictionary called <code>my_name</code>.</p>
|
|
|
<ul>
|
|
|
<li>Add a key for each letter in your name with a value of how many times that letter appears.</li>
|
|
|
</ul>
|
|
|
<p>As an example, here is the dictionary you’d make for <code>"Callee"</code>:</p>
|
|
|
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1">my_name <span class="op">=</span> {<span class="st">"c"</span>: <span class="dv">1</span>, <span class="st">"a"</span>: <span class="dv">1</span>, <span class="st">"l"</span>: <span class="dv">2</span>, <span class="st">"e"</span>: <span class="dv">2</span>}</a></code></pre></div>
|
|
|
<p>Write a loop that prints the dictionary, but formatted.</p>
|
|
|
<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="co"># The letter l appears in my name 2 times.</span></a></code></pre></div>
|
|
|
<p><strong>Bonus (if you have time)</strong>: If it’s only one time, instead print <code>The letter l appears in my name once</code>. If it’s only two times, instead print <code>The letter l appears in my name twice.</code></p>
|
|
|
<aside class="notes">
|
|
|
<p>5 MINUTES</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is a partner exercise, so pair students up.</li>
|
|
|
<li>Some students might be tempted to write a complex script to count the letters in their name; make sure they can do it the simple way first (just setting <code>"l"</code> to <code>2</code>, for example).</li>
|
|
|
<li>When students are done or time is up, go over the answer. Also, go over adding the <code>if</code> statements for the bonus.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="partner-exercise-most-popular-word" class="level2">
|
|
|
<h2>Partner Exercise: Most Popular Word</h2>
|
|
|
<p>With the same partner, switch who’s driving.</p>
|
|
|
<p>Write a function, <code>mostPopularWord()</code>, that takes a list of strings and returns the string that appears the most.</p>
|
|
|
<p>For example:</p>
|
|
|
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" data-line-number="1">words <span class="op">=</span> [</a>
|
|
|
<a class="sourceLine" id="cb8-2" data-line-number="2"> <span class="st">"hello"</span>,</a>
|
|
|
<a class="sourceLine" id="cb8-3" data-line-number="3"> <span class="st">"water"</span>,</a>
|
|
|
<a class="sourceLine" id="cb8-4" data-line-number="4"> <span class="st">"hello"</span></a>
|
|
|
<a class="sourceLine" id="cb8-5" data-line-number="5"> ]</a>
|
|
|
<a class="sourceLine" id="cb8-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb8-7" data-line-number="7"><span class="bu">print</span>(mostPopularWord(words))</a>
|
|
|
<a class="sourceLine" id="cb8-8" data-line-number="8"><span class="co"># Prints "hello"</span></a></code></pre></div>
|
|
|
<p><strong>Hints:</strong></p>
|
|
|
<ul>
|
|
|
<li>Create a dictionary with words as keys and the count as the values.</li>
|
|
|
<li>Check if a key already exists with <code>if "key_to_check" in my_dictionary:</code>.</li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p>5 MINUTES</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is a partner exercise, so pair students up.</li>
|
|
|
<li>When students are done or time is up, go over the answer.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="other-values" class="level2">
|
|
|
<h2>Other Values</h2>
|
|
|
<p>We’re almost there! Let’s make this more complex.</p>
|
|
|
<p>In a list or a dictionary, anything can be a value. - This is a good reason to split dictionary declarations over multiple lines!</p>
|
|
|
<p>Open this repl.it in a new window so you can see it all (the button in the top-right corner).</p>
|
|
|
<iframe height="300px" width="100%" src="https://repl.it/@SuperTernary/python-programming-dictionary-other-values?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 slide can be skipped in classes with struggling students; it’s good information but not crucial to know at a beginner’s level.</li>
|
|
|
<li>This is a demo, not an exercise.</li>
|
|
|
<li>This can be tough. Spend some time here.</li>
|
|
|
<li>This repl.it is huge! Open it in a new window so students can see it all.</li>
|
|
|
<li>All the code is there, you just need to walk through it.</li>
|
|
|
</ul>
|
|
|
<p><strong>Repl.it note:</strong> This code is:</p>
|
|
|
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" data-line-number="1">other_values_in_a_dictionary <span class="op">=</span> {</a>
|
|
|
<a class="sourceLine" id="cb9-2" data-line-number="2"> <span class="st">"CA"</span>: {<span class="st">"key1"</span> : <span class="st">"value 1"</span>,</a>
|
|
|
<a class="sourceLine" id="cb9-3" data-line-number="3"> <span class="st">"another_key"</span> : <span class="st">"a value"</span>,</a>
|
|
|
<a class="sourceLine" id="cb9-4" data-line-number="4"> <span class="st">"Joe"</span> : <span class="st">"Even more dictionary!"</span></a>
|
|
|
<a class="sourceLine" id="cb9-5" data-line-number="5"> },</a>
|
|
|
<a class="sourceLine" id="cb9-6" data-line-number="6"> <span class="st">"WA"</span>: [<span class="st">"Trevor"</span>, <span class="st">"Courtney"</span>, <span class="st">"Brianna"</span>, <span class="st">"Kai"</span>],</a>
|
|
|
<a class="sourceLine" id="cb9-7" data-line-number="7"> <span class="st">"NY"</span>: <span class="st">"Just Tatyana"</span></a>
|
|
|
<a class="sourceLine" id="cb9-8" data-line-number="8">}</a>
|
|
|
<a class="sourceLine" id="cb9-9" data-line-number="9"></a>
|
|
|
<a class="sourceLine" id="cb9-10" data-line-number="10"><span class="bu">print</span>(<span class="st">"Here's a dictionary and list in a dictionary:"</span>, other_values_in_a_dictionary)</a>
|
|
|
<a class="sourceLine" id="cb9-11" data-line-number="11"></a>
|
|
|
<a class="sourceLine" id="cb9-12" data-line-number="12"><span class="bu">print</span>(<span class="st">"----------"</span>)</a>
|
|
|
<a class="sourceLine" id="cb9-13" data-line-number="13"></a>
|
|
|
<a class="sourceLine" id="cb9-14" data-line-number="14">other_values_in_a_list <span class="op">=</span> [</a>
|
|
|
<a class="sourceLine" id="cb9-15" data-line-number="15"> <span class="st">"a value"</span>,</a>
|
|
|
<a class="sourceLine" id="cb9-16" data-line-number="16"> {<span class="st">"key1"</span> : <span class="st">"value 1"</span>, <span class="st">"key2"</span> : <span class="st">"value 2"</span>},</a>
|
|
|
<a class="sourceLine" id="cb9-17" data-line-number="17"> [<span class="st">"now"</span>, <span class="st">"a"</span>, <span class="st">"list"</span>]</a>
|
|
|
<a class="sourceLine" id="cb9-18" data-line-number="18"> ]</a>
|
|
|
<a class="sourceLine" id="cb9-19" data-line-number="19"><span class="bu">print</span>(<span class="st">"Here's a list and dictionary in a list:"</span>, other_values_in_a_list)</a></code></pre></div>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-and-qa" class="level2">
|
|
|
<h2>Summary and Q&A</h2>
|
|
|
<p>Dictionaries:</p>
|
|
|
<ul>
|
|
|
<li>Are another kind of collection, instead of a list.</li>
|
|
|
<li>Use <strong>keys</strong> to access <strong>values</strong>, not indices!</li>
|
|
|
<li>Should be used instead of lists when:
|
|
|
<ul>
|
|
|
<li>You don’t care about the order of the items.</li>
|
|
|
<li>You’d prefer more meaningful keys than just index numbers.</li>
|
|
|
</ul></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">my_dictionary <span class="op">=</span> {</a>
|
|
|
<a class="sourceLine" id="cb10-2" data-line-number="2"> <span class="st">"Puppy"</span>: <span class="st">"Furry, energetic animal"</span>,</a>
|
|
|
<a class="sourceLine" id="cb10-3" data-line-number="3"> <span class="st">"Pineapple"</span>: <span class="st">"Acidic tropical fruit"</span>,</a>
|
|
|
<a class="sourceLine" id="cb10-4" data-line-number="4"> <span class="st">"Tea"</span>: <span class="st">"Herb-infused drink"</span></a>
|
|
|
<a class="sourceLine" id="cb10-5" data-line-number="5">}</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is a quick recap. Check for understanding.</li>
|
|
|
<li>If there is time (~15 minutes), there is an exercise on the next slide to assign as a You Do. Otherwise, the same exercise is in the <code>xx-additional-exercises</code> folder as <code>dictionary_reverse_lookup</code>; assign it as additional homework.</li>
|
|
|
<li>After running the exercise (or not), talk about next steps.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="you-do-reverse-lookup" class="level2">
|
|
|
<h2>You Do: Reverse Lookup</h2>
|
|
|
<p>Finding the value from a key is easy: <code>my_dictionary[key]</code>. But, what if you only have the value and want to find the key?</p>
|
|
|
<p>You task is to write a function, <code>reverse_lookup()</code>, that takes a dictionary and a value and returns the corresponding key.</p>
|
|
|
<p>For example:</p>
|
|
|
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" data-line-number="1">state_capitals <span class="op">=</span> {</a>
|
|
|
<a class="sourceLine" id="cb11-2" data-line-number="2"> <span class="st">"Alaska"</span> : <span class="st">"Juneau"</span>,</a>
|
|
|
<a class="sourceLine" id="cb11-3" data-line-number="3"> <span class="st">"Colorado"</span> : <span class="st">"Denver"</span>,</a>
|
|
|
<a class="sourceLine" id="cb11-4" data-line-number="4"> <span class="st">"Oregon"</span> : <span class="st">"Salem"</span>,</a>
|
|
|
<a class="sourceLine" id="cb11-5" data-line-number="5"> <span class="st">"Texas"</span> : <span class="st">"Austin"</span></a>
|
|
|
<a class="sourceLine" id="cb11-6" data-line-number="6"> }</a>
|
|
|
<a class="sourceLine" id="cb11-7" data-line-number="7"></a>
|
|
|
<a class="sourceLine" id="cb11-8" data-line-number="8"><span class="bu">print</span>(reverse_lookup(<span class="st">"Denver"</span>))</a>
|
|
|
<a class="sourceLine" id="cb11-9" data-line-number="9"><span class="co"># Prints Colorado</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p>~15 MINUTES</p>
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>This is a You Do to assign only if there’s time. Otherwise, the same exercise is in the <code>xx-additional-exercises</code> folder as <code>dictionary_reverse_lookup</code>; assign it as additional homework.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
</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>
|