|
|
<!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 Python Lists
|
|
|
type: lesson
|
|
|
duration: "00:30"
|
|
|
creator: Brandi Butler
|
|
|
Private gist location: https://gist.github.com/brandiw/621c35f1987e5ab680e7de7b05dfe039
|
|
|
Presentation URL: https://presentations.generalassemb.ly/621c35f1987e5ab680e7de7b05dfe039#/
|
|
|
-->
|
|
|
<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: Lists
|
|
|
</h1>
|
|
|
<!--
|
|
|
|
|
|
## Overview
|
|
|
This lesson introduces students to the concept of lists. This begins as basic list operations - accessing elements, `len`, `insert`, `append`, and `pop`. After an exercise to recap that, it segues into operations on numerical lists - `sum`, `min`, and `max`. It ends with a longer exercise recapping the list operations.
|
|
|
|
|
|
## Learning Objectives
|
|
|
In this lesson, students will:
|
|
|
- Create lists in Python.
|
|
|
- Print out specific elements in a list.
|
|
|
- Perform common list operations.
|
|
|
|
|
|
## Duration
|
|
|
30 minutes
|
|
|
|
|
|
### Notes on Timing
|
|
|
|
|
|
A 30 minute interval has been allotted for this lesson. You may finish up early due to the fact that this lesson doesn't get into loops or ranges. If you have extra time, put it on the activities or start the next lesson early so students do have buffer time later, when they need it.
|
|
|
|
|
|
That said, at the point you give this lesson, students are still on day one. They will require more time than you probably expect to poke around the code.
|
|
|
|
|
|
## Suggested Agenda
|
|
|
|
|
|
| Time | Activity |
|
|
|
| --- | --- |
|
|
|
| 0:00 - 0:03 | Welcome |
|
|
|
| 0:03 - 0:15 | Basic List Operations |
|
|
|
| 0:15 - 0:25 | Numerical List Operations |
|
|
|
| 0:25 - 0:30 | 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>Create lists in Python.</li>
|
|
|
<li>Print out specific elements in a list.</li>
|
|
|
<li>Perform common list operations.</li>
|
|
|
</ul>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="what-is-a-list" class="level2">
|
|
|
<h2>What is a List?</h2>
|
|
|
<p>Variables hold one item.</p>
|
|
|
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb1-1" data-line-number="1">my_color <span class="op">=</span> <span class="st">"red"</span></a>
|
|
|
<a class="sourceLine" id="cb1-2" data-line-number="2">my_peer <span class="op">=</span> <span class="st">"Brandi"</span></a></code></pre></div>
|
|
|
<p><strong>Lists</strong> hold multiple items - and lists can hold anything.</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="co"># Declaring lists</span></a>
|
|
|
<a class="sourceLine" id="cb2-2" data-line-number="2">colors <span class="op">=</span> [<span class="st">"red"</span>, <span class="st">"yellow"</span>, <span class="st">"green"</span>]</a>
|
|
|
<a class="sourceLine" id="cb2-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>]</a>
|
|
|
<a class="sourceLine" id="cb2-4" data-line-number="4"></a>
|
|
|
<a class="sourceLine" id="cb2-5" data-line-number="5"><span class="co"># Strings</span></a>
|
|
|
<a class="sourceLine" id="cb2-6" data-line-number="6">colors <span class="op">=</span> [<span class="st">"red"</span>, <span class="st">"yellow"</span>, <span class="st">"green"</span>]</a>
|
|
|
<a class="sourceLine" id="cb2-7" data-line-number="7"></a>
|
|
|
<a class="sourceLine" id="cb2-8" data-line-number="8"><span class="co"># Numbers</span></a>
|
|
|
<a class="sourceLine" id="cb2-9" data-line-number="9">my_nums <span class="op">=</span> [<span class="dv">4</span>, <span class="dv">7</span>, <span class="dv">9</span>, <span class="dv">1</span>, <span class="dv">4</span>]</a>
|
|
|
<a class="sourceLine" id="cb2-10" data-line-number="10"></a>
|
|
|
<a class="sourceLine" id="cb2-11" data-line-number="11"><span class="co"># Both!</span></a>
|
|
|
<a class="sourceLine" id="cb2-12" data-line-number="12">my_nums <span class="op">=</span> [<span class="st">"red"</span>, <span class="dv">7</span>, <span class="st">"yellow"</span>, <span class="dv">1</span>, <span class="dv">4</span>]</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>After explaining what a list is, walk through the syntax (dashes, commas).</li>
|
|
|
<li>Point out anything can be in a list - it’s just a variable that holds many things.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li><p>"Until now we’ve used a few different types of variables such as numbers and strings. However, what if we wanted to keep track of more than one thing? Instead of just my single favorite color, how can I store the names of all the colors I like? How can I store the numbers of everyone on my baseball team?</p></li>
|
|
|
<li><p>“Python has this problem solved with something called a <em>List</em>.”</p></li>
|
|
|
<li><p>“Because a variable is just a box that can hold information, it can also hold lists. Python knows that your variable will hold a list if it begins and ends with square brackets”</p></li>
|
|
|
<li><p>“A list is a data structure in Python, which is a fancy way of saying we can put data inside of it. In the same way you recognize strings by the quotation marks surround them, you can recognize lists by square brackets that surround them.”</p></li>
|
|
|
<li><p>“Notice in the example below, we have a list but there are strings inside of it. A list can store data of other types! In this case, I have a list of strings that stores my classmates names.”</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="accessing-elements" class="level2">
|
|
|
<h2>Accessing Elements</h2>
|
|
|
<p><strong>List Index</strong> means the location of something (an <em>element</em>) in the list.</p>
|
|
|
<p>List indexes start counting at 0!</p>
|
|
|
<table>
|
|
|
<thead>
|
|
|
<tr class="header">
|
|
|
<th style="text-align: center;">List</th>
|
|
|
<th style="text-align: center;">“Brandi”</th>
|
|
|
<th style="text-align: center;">“Zoe”</th>
|
|
|
<th style="text-align: center;">“Steve”</th>
|
|
|
<th style="text-align: center;">“Aleksander”</th>
|
|
|
<th style="text-align: center;">“Dasha”</th>
|
|
|
</tr>
|
|
|
</thead>
|
|
|
<tbody>
|
|
|
<tr class="odd">
|
|
|
<td style="text-align: center;">Index</td>
|
|
|
<td style="text-align: center;">0</td>
|
|
|
<td style="text-align: center;">1</td>
|
|
|
<td style="text-align: center;">2</td>
|
|
|
<td style="text-align: center;">3</td>
|
|
|
<td style="text-align: center;">4</td>
|
|
|
</tr>
|
|
|
</tbody>
|
|
|
</table>
|
|
|
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" data-line-number="1">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>]</a>
|
|
|
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="bu">print</span>(my_class[<span class="dv">0</span>]) <span class="co"># Prints "Brandi"</span></a>
|
|
|
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="bu">print</span>(my_class[<span class="dv">1</span>]) <span class="co"># Prints "Zoe"</span></a>
|
|
|
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="bu">print</span>(my_class[<span class="dv">4</span>]) <span class="co"># Prints "Dasha"</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Starting at 0 is easy to understand and hard to remember. Remind them throughout the presentation.</li>
|
|
|
<li>Point out the print syntax.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li><p>“A very important thing to note about lists is that they start counting at 0. Thus, the first element is considered index 0, the second element is considered index 1, and so on.”</p></li>
|
|
|
<li><p>“In our previous example, let’s print a few specific items. We can access an item by counting from 0 and using square brackets to tell the list which item we want.”</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="we-do-lists" class="level2">
|
|
|
<h2>We Do: Lists</h2>
|
|
|
<ol type="1">
|
|
|
<li>Create a <strong>list</strong> with the names <code>"Holly"</code>, <code>"Juan"</code>, and <code>"Ming"</code>.</li>
|
|
|
<li>Print the third name.</li>
|
|
|
<li>Create a <strong>list</strong> with the numbers <code>2</code>,<code>4</code>, <code>6</code>, and <code>8</code>.</li>
|
|
|
<li>Print the first number.</li>
|
|
|
</ol>
|
|
|
<iframe height="400px" 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>1 MINUTE</p>
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Run through this quickly - this is just to show them lists working get them practicing typing lists. This is not mean to be a full fledged exercise.</li>
|
|
|
</ul>
|
|
|
<p><strong>Repl.it Note</strong> It’s blank.</p>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="list-operations---length" class="level2">
|
|
|
<h2>List Operations - Length</h2>
|
|
|
<p><code>len()</code>:</p>
|
|
|
<ul>
|
|
|
<li>A built in <code>list</code> operation.</li>
|
|
|
<li>How long is the list?</li>
|
|
|
</ul>
|
|
|
<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"># length_variable = len(your_list)</span></a>
|
|
|
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb4-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>]</a>
|
|
|
<a class="sourceLine" id="cb4-4" data-line-number="4">num_students <span class="op">=</span> <span class="bu">len</span>(my_class)</a>
|
|
|
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="bu">print</span>(<span class="st">"There are"</span>, num_students, <span class="st">"students in the class"</span>)</a>
|
|
|
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="co"># => 5</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Stress that even though all examples are strings, these can be performed on any list, no matter what’s in it.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li><p>“How many people are in my list? Just as with strings, we can determine how long a list is (i.e., how many elements it has) using the len() method like so.”</p></li>
|
|
|
<li><p>“Note: We’ll get more into functions later. For now, just know that they perform some operation for you and that you can recognize them by the parentheses on the end.”</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="adding-elements-append" class="level2">
|
|
|
<h2>Adding Elements: Append</h2>
|
|
|
<p><code>.append()</code>:</p>
|
|
|
<ul>
|
|
|
<li>A built in <code>list</code> operation.</li>
|
|
|
<li>Adds to the end of the list.</li>
|
|
|
<li>Takes any element.</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"><span class="co"># your_list.append(item)</span></a>
|
|
|
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb5-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>]</a>
|
|
|
<a class="sourceLine" id="cb5-4" data-line-number="4">my_class.append(<span class="st">"Sonyl"</span>)</a>
|
|
|
<a class="sourceLine" id="cb5-5" data-line-number="5"><span class="bu">print</span>(my_class)</a>
|
|
|
<a class="sourceLine" id="cb5-6" data-line-number="6"><span class="co"># => ["Brandi", "Zoe", "Steve", "Aleksander", "Dasha", "Sonyl"]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li>“Forgot to add something to that list? No problem; you can use the .append() method. Suppose a new student joins our class. We can add them to the end of the list with <code>append</code>, which is a function built directly into a list. (Notice it is called with a dot after the list, unlike the other function we’ve used, <code>len</code>)”</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="adding-elements-insert" class="level2">
|
|
|
<h2>Adding Elements: Insert</h2>
|
|
|
<p><code>.insert()</code>:</p>
|
|
|
<ul>
|
|
|
<li>A built in <code>list</code> operation.</li>
|
|
|
<li>Adds to any point in the list</li>
|
|
|
<li>Takes any element and an index.</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"><span class="co"># your_list.insert(index, item)</span></a>
|
|
|
<a class="sourceLine" id="cb6-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb6-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>, <span class="st">"Sonyl"</span>]</a>
|
|
|
<a class="sourceLine" id="cb6-4" data-line-number="4">my_class.insert(<span class="dv">1</span>, <span class="st">"Sanju"</span>)</a>
|
|
|
<a class="sourceLine" id="cb6-5" data-line-number="5"><span class="bu">print</span>(my_class)</a>
|
|
|
<a class="sourceLine" id="cb6-6" data-line-number="6"><span class="co"># => ["Brandi", "Sanju", "Zoe", "Steve", "Aleksander", "Dasha", "Sonyl"]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li>“However, what happens if we want to add something somewhere else? We can use the .insert() method, which specifies where (i.e., to which index) we want to add the element.”</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="removing-elements---pop" class="level2">
|
|
|
<h2>Removing elements - Pop</h2>
|
|
|
<p><code>.pop()</code>:</p>
|
|
|
<ul>
|
|
|
<li>A built in <code>list</code> operation.</li>
|
|
|
<li>Removes an item from the end of the list.</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="co"># your_list.pop()</span></a>
|
|
|
<a class="sourceLine" id="cb7-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb7-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>, <span class="st">"Sonyl"</span>]</a>
|
|
|
<a class="sourceLine" id="cb7-4" data-line-number="4">student_that_left <span class="op">=</span> my_class.pop()</a>
|
|
|
<a class="sourceLine" id="cb7-5" data-line-number="5"><span class="bu">print</span>(<span class="st">"The student"</span>, student_that_left, <span class="st">"has left the class."</span>)</a>
|
|
|
<a class="sourceLine" id="cb7-6" data-line-number="6"><span class="co"># => "Sonyl"</span></a>
|
|
|
<a class="sourceLine" id="cb7-7" data-line-number="7"><span class="bu">print</span>(my_class)</a>
|
|
|
<a class="sourceLine" id="cb7-8" data-line-number="8"><span class="co"># => ["Brandi", "Zoe", "Steve", "Aleksander", "Dasha"]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li><p>“What if someone leaves our class? We need to remove them from the list.”</p></li>
|
|
|
<li><p>“We can do this with <code>pop</code>. Pop drops the last thing off the list. It gives us back the value that it removed. We can take that value and assign it to a new variable, <code>student that left</code>. This is called a <code>return value</code>.”</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="removing-elements---popindex" class="level2">
|
|
|
<h2>Removing elements - Pop(index)</h2>
|
|
|
<p><code>.pop(index)</code>:</p>
|
|
|
<ul>
|
|
|
<li>A built in <code>list</code> operation.</li>
|
|
|
<li>Removes an item from the list.</li>
|
|
|
<li>Can take an index.</li>
|
|
|
</ul>
|
|
|
<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="co"># your_list.pop(index)</span></a>
|
|
|
<a class="sourceLine" id="cb8-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb8-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">"Brandi"</span>, <span class="st">"Zoe"</span>, <span class="st">"Steve"</span>, <span class="st">"Aleksander"</span>, <span class="st">"Dasha"</span>, <span class="st">"Sonyl"</span>]</a>
|
|
|
<a class="sourceLine" id="cb8-4" data-line-number="4">student_that_left <span class="op">=</span> my_class.pop(<span class="dv">2</span>) <span class="co"># Remember to count from 0!</span></a>
|
|
|
<a class="sourceLine" id="cb8-5" data-line-number="5"><span class="bu">print</span>(<span class="st">"The student"</span>, student_that_left, <span class="st">"has left the class."</span>)</a>
|
|
|
<a class="sourceLine" id="cb8-6" data-line-number="6"><span class="co"># => "Steve"</span></a>
|
|
|
<a class="sourceLine" id="cb8-7" data-line-number="7"><span class="bu">print</span>(my_class)</a>
|
|
|
<a class="sourceLine" id="cb8-8" data-line-number="8"><span class="co"># => ["Brandi", "Zoe", "Aleksander", "Dasha", "Sonyl"]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li><p>“What if someone specific leaves the class?”</p></li>
|
|
|
<li><p>“We can do this with <code>pop</code> again. Here, we can give pop the index we want removed. It gives us back the value that it removed. We can take that value and assign it to a new variable, <code>student that left</code>.”</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="partner-exercise-pop-insert-and-append" class="level2">
|
|
|
<h2>Partner Exercise: Pop, Insert, and Append</h2>
|
|
|
<p>Partner up! Choose one person to be the driver and one to be the navigator, and see if you can do the prompts:</p>
|
|
|
<iframe height="400px" width="100%" src="https://repl.it/@SuperTernary/python-programming-lists-intro?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>
|
|
|
<p>3 MINUTES</p>
|
|
|
<ul>
|
|
|
<li>Try to get them in pairs they haven’t worked in before.</li>
|
|
|
<li>Give them just small bit of time to work through this, then go over the answer.</li>
|
|
|
</ul>
|
|
|
<p><strong>Repl.it Note:</strong> This replit has:</p>
|
|
|
<pre><code># 1. Declare a list with the names of your classmates
|
|
|
# 2. Print out the length of that list
|
|
|
# 3. Print the 3rd name on the list
|
|
|
# 4. Delete the first name on the list
|
|
|
# 5. Re-add the name you deleted to the end of the list</code></pre>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="pop-insert-append-solution" class="level2">
|
|
|
<h2>Pop, Insert, Append Solution</h2>
|
|
|
<iframe height="400px" width="100%" src="https://repl.it/@sonylnagale/python-programming-lists-intro-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>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Repl.it Note</strong>: This replit has:</p>
|
|
|
</section>
|
|
|
<section id="declare-a-list-with-the-names-of-your-classmates" class="level1">
|
|
|
<h1>1. Declare a list with the names of your classmates</h1>
|
|
|
<p>my_class = [“Brandi”, “Zoe”, “Steve”, “Aleksander”, “Dasha”, “Sonyl”]</p>
|
|
|
</section>
|
|
|
<section id="print-out-the-length-of-that-list" class="level1">
|
|
|
<h1>2. Print out the length of that list</h1>
|
|
|
<p>print(len(my_class))</p>
|
|
|
</section>
|
|
|
<section id="print-the-3rd-name-on-the-list" class="level1">
|
|
|
<h1>3. Print the 3rd name on the list</h1>
|
|
|
<p>print(my_class[2])</p>
|
|
|
</section>
|
|
|
<section id="delete-the-first-name-on-the-list" class="level1">
|
|
|
<h1>4. Delete the first name on the list</h1>
|
|
|
<p>deleted_classmate = my_class.pop(0)</p>
|
|
|
</section>
|
|
|
<section id="re-add-the-name-you-deleted-to-the-end-of-the-list" class="level1">
|
|
|
<h1>5. Re-add the name you deleted to the end of the list</h1>
|
|
|
<p>my_class.append(deleted_classmate)</p>
|
|
|
<p>print(my_class)</p>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
<section id="list-mutation-warning" class="level2">
|
|
|
<h2>!! List Mutation: Warning !!</h2>
|
|
|
<p>This won’t work as expected - don’t do this!</p>
|
|
|
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" data-line-number="1">colors <span class="op">=</span> [<span class="st">"red"</span>, <span class="st">"yellow"</span>, <span class="st">"green"</span>]</a>
|
|
|
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="bu">print</span> colors.append(<span class="st">"blue"</span>)</a>
|
|
|
<a class="sourceLine" id="cb10-3" data-line-number="3"><span class="co"># => None</span></a></code></pre></div>
|
|
|
<p>This will work - do this!</p>
|
|
|
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" data-line-number="1">colors <span class="op">=</span> [<span class="st">"red"</span>, <span class="st">"yellow"</span>, <span class="st">"green"</span>]</a>
|
|
|
<a class="sourceLine" id="cb11-2" data-line-number="2">colors.append(<span class="st">"blue"</span>)</a>
|
|
|
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="bu">print</span> colors</a>
|
|
|
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="co"># => ["red", "yellow", "green", "blue"]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Talk about why this happens, especially in case a student accidentally does it.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li>“All of the methods above mutate, i.e., change the array in place; they don’t give you the mutated, or changed, array back.”</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="quick-review-basic-list-operations" class="level2">
|
|
|
<h2>Quick Review: Basic List Operations</h2>
|
|
|
<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="co"># List Creation</span></a>
|
|
|
<a class="sourceLine" id="cb12-2" data-line-number="2">my_list <span class="op">=</span> [<span class="st">"red"</span>, <span class="dv">7</span>, <span class="st">"yellow"</span>, <span class="dv">1</span>]</a>
|
|
|
<a class="sourceLine" id="cb12-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="co"># List Length</span></a>
|
|
|
<a class="sourceLine" id="cb12-5" data-line-number="5">list_length <span class="op">=</span> <span class="bu">len</span>(my_list) <span class="co"># 4</span></a>
|
|
|
<a class="sourceLine" id="cb12-6" data-line-number="6"></a>
|
|
|
<a class="sourceLine" id="cb12-7" data-line-number="7"><span class="co"># List Index</span></a>
|
|
|
<a class="sourceLine" id="cb12-8" data-line-number="8"><span class="bu">print</span>(my_list[<span class="dv">0</span>]) <span class="co"># red</span></a>
|
|
|
<a class="sourceLine" id="cb12-9" data-line-number="9"></a>
|
|
|
<a class="sourceLine" id="cb12-10" data-line-number="10"><span class="co"># List Append</span></a>
|
|
|
<a class="sourceLine" id="cb12-11" data-line-number="11">my_list.append(<span class="st">"Yi"</span>) <span class="co"># ["red", 7, "yellow", 1, "Yi"]</span></a>
|
|
|
<a class="sourceLine" id="cb12-12" data-line-number="12"></a>
|
|
|
<a class="sourceLine" id="cb12-13" data-line-number="13"><span class="co"># List Insert at Index</span></a>
|
|
|
<a class="sourceLine" id="cb12-14" data-line-number="14">my_list.insert(<span class="dv">1</span>, <span class="st">"Sanju"</span>) <span class="co"># ["red", "Sanju", 7, "yellow", 1, "Yi"]</span></a>
|
|
|
<a class="sourceLine" id="cb12-15" data-line-number="15"></a>
|
|
|
<a class="sourceLine" id="cb12-16" data-line-number="16"><span class="co"># List Delete</span></a>
|
|
|
<a class="sourceLine" id="cb12-17" data-line-number="17">student_that_left <span class="op">=</span> my_list.pop() <span class="co"># "Yi"; ["red", "Sanju", 7, "yellow", 1]</span></a>
|
|
|
<a class="sourceLine" id="cb12-18" data-line-number="18"></a>
|
|
|
<a class="sourceLine" id="cb12-19" data-line-number="19"><span class="co"># List Delete at Index</span></a>
|
|
|
<a class="sourceLine" id="cb12-20" data-line-number="20">student_that_left <span class="op">=</span> my_list.pop(<span class="dv">2</span>) <span class="co"># 7; ["red", "Sanju", "yellow", 1]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Quickly review. Check to see if anyone’s stuck.</li>
|
|
|
<li>Remind them that these operations work on lists with both strings and numbers.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="numerical-list-operations---sum" class="level2">
|
|
|
<h2>Numerical List Operations - Sum</h2>
|
|
|
<p>Some actions can only be performed on lists with numbers.</p>
|
|
|
<p><code>sum()</code>:</p>
|
|
|
<ul>
|
|
|
<li>A built in <code>list</code> operation.</li>
|
|
|
<li>Adds the list together.</li>
|
|
|
<li>Only works on lists with numbers!</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="co"># sum(your_numeric_list)</span></a>
|
|
|
<a class="sourceLine" id="cb13-2" data-line-number="2"></a>
|
|
|
<a class="sourceLine" id="cb13-3" data-line-number="3">team_batting_avgs <span class="op">=</span> [.<span class="dv">328</span>, <span class="fl">.299</span>, <span class="fl">.208</span>, <span class="fl">.301</span>, <span class="fl">.275</span>, <span class="fl">.226</span>, <span class="fl">.253</span>, <span class="fl">.232</span>, <span class="fl">.287</span>]</a>
|
|
|
<a class="sourceLine" id="cb13-4" data-line-number="4">sum_avgs <span class="op">=</span> <span class="bu">sum</span>(team_batting_avgs)</a>
|
|
|
<a class="sourceLine" id="cb13-5" data-line-number="5"><span class="bu">print</span>(<span class="st">"The total of all the batting averages is"</span>, sum_avgs)</a>
|
|
|
<a class="sourceLine" id="cb13-6" data-line-number="6"><span class="co"># => 2.409</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>If baseball’s not your thing, feel free to change this.</li>
|
|
|
<li>Stress that these only work on lists with entirely numerical values.</li>
|
|
|
<li>You might need to explain batting averages.</li>
|
|
|
<li>Consider demoing trying to sum string numbers.</li>
|
|
|
</ul>
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li>“There’s another built-in function, <code>sum</code>, used to add a list of numbers together.”</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="list-operations---maxmin" class="level2">
|
|
|
<h2>List Operations - Max/Min</h2>
|
|
|
<p><code>max()</code> or <code>min()</code>:</p>
|
|
|
<ul>
|
|
|
<li>Built in <code>list</code> operations.</li>
|
|
|
<li>Finds highest, or lowest, in the list.</li>
|
|
|
<li>Only works on lists with numbers!</li>
|
|
|
</ul>
|
|
|
<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="co"># max(your_numeric_list)</span></a>
|
|
|
<a class="sourceLine" id="cb14-2" data-line-number="2"><span class="co"># min(your_numeric_list)</span></a>
|
|
|
<a class="sourceLine" id="cb14-3" data-line-number="3"></a>
|
|
|
<a class="sourceLine" id="cb14-4" data-line-number="4">team_batting_avgs <span class="op">=</span> [.<span class="dv">328</span>, <span class="fl">.299</span>, <span class="fl">.208</span>, <span class="fl">.301</span>, <span class="fl">.275</span>, <span class="fl">.226</span>, <span class="fl">.253</span>, <span class="fl">.232</span>, <span class="fl">.287</span>]</a>
|
|
|
<a class="sourceLine" id="cb14-5" data-line-number="5"><span class="bu">print</span>(<span class="st">"The highest batting average is"</span>, <span class="bu">max</span>(team_batting_avgs))</a>
|
|
|
<a class="sourceLine" id="cb14-6" data-line-number="6"><span class="co"># => 0.328</span></a>
|
|
|
<a class="sourceLine" id="cb14-7" data-line-number="7"><span class="bu">print</span>(<span class="st">"The lowest batting average is"</span>, <span class="bu">min</span>(team_batting_avgs))</a>
|
|
|
<a class="sourceLine" id="cb14-8" data-line-number="8"><span class="co"># => 0.208</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Talking Points</strong>:</p>
|
|
|
<ul>
|
|
|
<li>“We might want to simply know what is the largest or smallest item in a list. In this case, we can use the built-in functions <code>max</code> and <code>min</code>.”</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="you-do-lists" class="level2">
|
|
|
<h2>You Do: Lists</h2>
|
|
|
<p>On your local computer, create a <code>.py</code> file named <code>list_practice.py</code>. In it:</p>
|
|
|
<ol type="1">
|
|
|
<li>Save a list with the numbers <code>2</code>, <code>4</code>, <code>6</code>, and <code>8</code> into a variable called <code>numbers</code>.</li>
|
|
|
<li>Print the max of <code>numbers</code>.</li>
|
|
|
<li>Pop the last element in <code>numbers</code> off; re-insert it at index <code>2</code>.</li>
|
|
|
<li>Pop the second number in <code>numbers</code> off.</li>
|
|
|
<li>Append <code>3</code> to <code>numbers</code>.</li>
|
|
|
<li>Print out the average number (divide the sum of <code>numbers</code> by the length).</li>
|
|
|
<li>Print <code>numbers</code>.</li>
|
|
|
</ol>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Have students run through this exercise on their own. Circulate the room to check questions and challenges.</li>
|
|
|
<li><p>Students might pop off index 2 versus the actual 2nd element - remind them to watch out for that.</p></li>
|
|
|
<li><p>Answer that’s printed: Max: 8 Average: 4.75 Final list: [2 8 6 3]</p></li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-and-qa" class="level2">
|
|
|
<h2>Summary and Q&A</h2>
|
|
|
<p>We accomplished quite a bit!</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="co"># List Creation</span></a>
|
|
|
<a class="sourceLine" id="cb15-2" data-line-number="2">my_list <span class="op">=</span> [<span class="st">"red"</span>, <span class="dv">7</span>, <span class="st">"yellow"</span>, <span class="dv">1</span>]</a>
|
|
|
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="co"># List Length</span></a>
|
|
|
<a class="sourceLine" id="cb15-4" data-line-number="4">list_length <span class="op">=</span> <span class="bu">len</span>(my_list) <span class="co"># 4</span></a>
|
|
|
<a class="sourceLine" id="cb15-5" data-line-number="5"><span class="co"># List Index</span></a>
|
|
|
<a class="sourceLine" id="cb15-6" data-line-number="6"><span class="bu">print</span>(my_list[<span class="dv">0</span>]) <span class="co"># red</span></a>
|
|
|
<a class="sourceLine" id="cb15-7" data-line-number="7"><span class="co"># List Append</span></a>
|
|
|
<a class="sourceLine" id="cb15-8" data-line-number="8">my_list.append(<span class="st">"Yi"</span>) <span class="co"># ["red", 7, "yellow", 1, "Yi"]</span></a>
|
|
|
<a class="sourceLine" id="cb15-9" data-line-number="9"><span class="co"># List Insert at Index</span></a>
|
|
|
<a class="sourceLine" id="cb15-10" data-line-number="10">my_list.insert(<span class="dv">1</span>, <span class="st">"Sanju"</span>) <span class="co"># ["red", "Sanju", 7, "yellow", 1, "Yi"]</span></a>
|
|
|
<a class="sourceLine" id="cb15-11" data-line-number="11"><span class="co"># List Delete</span></a>
|
|
|
<a class="sourceLine" id="cb15-12" data-line-number="12">student_that_left <span class="op">=</span> my_list.pop() <span class="co"># "Yi"; ["red", "Sanju", 7, "yellow", 1]</span></a>
|
|
|
<a class="sourceLine" id="cb15-13" data-line-number="13"><span class="co"># List Delete at Index</span></a>
|
|
|
<a class="sourceLine" id="cb15-14" data-line-number="14">student_that_left <span class="op">=</span> my_list.pop(<span class="dv">2</span>) <span class="co"># 7; ["red", "Sanju", "yellow", 1]</span></a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Quickly sum up and check for understanding.</li>
|
|
|
<li>Reassure students that while this is a lot, the more they practice typing it, the more they’ll get used to it. If they always copy the code off the slide, they won’t be very quick to learn it!</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="summary-and-qa-1" class="level2">
|
|
|
<h2>Summary and Q&A</h2>
|
|
|
<p>And for numerical lists only…</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="co"># Sum all numbers in list</span></a>
|
|
|
<a class="sourceLine" id="cb16-2" data-line-number="2">sum_avgs <span class="op">=</span> <span class="bu">sum</span>(team_batting_avgs)</a>
|
|
|
<a class="sourceLine" id="cb16-3" data-line-number="3"><span class="co"># Find minimum value of list</span></a>
|
|
|
<a class="sourceLine" id="cb16-4" data-line-number="4"><span class="bu">min</span>(team_batting_avgs)</a>
|
|
|
<a class="sourceLine" id="cb16-5" data-line-number="5"><span class="co"># Find maximum value of list</span></a>
|
|
|
<a class="sourceLine" id="cb16-6" data-line-number="6"><span class="bu">max</span>(team_batting_avgs)</a></code></pre></div>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips</strong>:</p>
|
|
|
<ul>
|
|
|
<li>Quickly sum up and check for understanding.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
<hr />
|
|
|
</section>
|
|
|
<section id="additional-resources" class="level2">
|
|
|
<h2>Additional Resources</h2>
|
|
|
<ul>
|
|
|
<li><a href="https://www.youtube.com/watch?v=zEyEC34MY1A">Python Lists - Khan Academy Video</a></li>
|
|
|
<li><a href="https://developers.google.com/edu/python/lists">Google For Education: Python Lists</a></li>
|
|
|
<li><a href="https://www.tutorialspoint.com/python/python_lists.htm">Python-Lists</a></li>
|
|
|
</ul>
|
|
|
<aside class="notes">
|
|
|
<p><strong>Teaching Tips:</strong></p>
|
|
|
<ul>
|
|
|
<li>Encourage students to go through these in their spare time, especially if they’re a bit lost.</li>
|
|
|
</ul>
|
|
|
</aside>
|
|
|
</section>
|
|
|
</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>
|