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.

622 lines
39 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 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">&quot;red&quot;</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">my_peer <span class="op">=</span> <span class="st">&quot;Brandi&quot;</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">&quot;red&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;green&quot;</span>]</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">my_class <span class="op">=</span> [<span class="st">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</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">&quot;red&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;green&quot;</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">&quot;red&quot;</span>, <span class="dv">7</span>, <span class="st">&quot;yellow&quot;</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 - its just a variable that holds many things.</li>
</ul>
<p><strong>Talking Points</strong>:</p>
<ul>
<li><p>&quot;Until now weve 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">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</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 &quot;Brandi&quot;</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 &quot;Zoe&quot;</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 &quot;Dasha&quot;</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, lets 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>&quot;Holly&quot;</code>, <code>&quot;Juan&quot;</code>, and <code>&quot;Ming&quot;</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> Its 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">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</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">&quot;There are&quot;</span>, num_students, <span class="st">&quot;students in the class&quot;</span>)</a>
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="co"># =&gt; 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 whats 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: Well 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">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</span>]</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">my_class.append(<span class="st">&quot;Sonyl&quot;</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"># =&gt; [&quot;Brandi&quot;, &quot;Zoe&quot;, &quot;Steve&quot;, &quot;Aleksander&quot;, &quot;Dasha&quot;, &quot;Sonyl&quot;]</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 weve 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">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</span>, <span class="st">&quot;Sonyl&quot;</span>]</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">my_class.insert(<span class="dv">1</span>, <span class="st">&quot;Sanju&quot;</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"># =&gt; [&quot;Brandi&quot;, &quot;Sanju&quot;, &quot;Zoe&quot;, &quot;Steve&quot;, &quot;Aleksander&quot;, &quot;Dasha&quot;, &quot;Sonyl&quot;]</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">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</span>, <span class="st">&quot;Sonyl&quot;</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">&quot;The student&quot;</span>, student_that_left, <span class="st">&quot;has left the class.&quot;</span>)</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"><span class="co"># =&gt; &quot;Sonyl&quot;</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"># =&gt; [&quot;Brandi&quot;, &quot;Zoe&quot;, &quot;Steve&quot;, &quot;Aleksander&quot;, &quot;Dasha&quot;]</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">&quot;Brandi&quot;</span>, <span class="st">&quot;Zoe&quot;</span>, <span class="st">&quot;Steve&quot;</span>, <span class="st">&quot;Aleksander&quot;</span>, <span class="st">&quot;Dasha&quot;</span>, <span class="st">&quot;Sonyl&quot;</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">&quot;The student&quot;</span>, student_that_left, <span class="st">&quot;has left the class.&quot;</span>)</a>
<a class="sourceLine" id="cb8-6" data-line-number="6"><span class="co"># =&gt; &quot;Steve&quot;</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"># =&gt; [&quot;Brandi&quot;, &quot;Zoe&quot;, &quot;Aleksander&quot;, &quot;Dasha&quot;, &quot;Sonyl&quot;]</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 havent 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 wont work as expected - dont 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">&quot;red&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;green&quot;</span>]</a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="bu">print</span> colors.append(<span class="st">&quot;blue&quot;</span>)</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"><span class="co"># =&gt; 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">&quot;red&quot;</span>, <span class="st">&quot;yellow&quot;</span>, <span class="st">&quot;green&quot;</span>]</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">colors.append(<span class="st">&quot;blue&quot;</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"># =&gt; [&quot;red&quot;, &quot;yellow&quot;, &quot;green&quot;, &quot;blue&quot;]</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 dont 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">&quot;red&quot;</span>, <span class="dv">7</span>, <span class="st">&quot;yellow&quot;</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">&quot;Yi&quot;</span>) <span class="co"># [&quot;red&quot;, 7, &quot;yellow&quot;, 1, &quot;Yi&quot;]</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">&quot;Sanju&quot;</span>) <span class="co"># [&quot;red&quot;, &quot;Sanju&quot;, 7, &quot;yellow&quot;, 1, &quot;Yi&quot;]</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"># &quot;Yi&quot;; [&quot;red&quot;, &quot;Sanju&quot;, 7, &quot;yellow&quot;, 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; [&quot;red&quot;, &quot;Sanju&quot;, &quot;yellow&quot;, 1]</span></a></code></pre></div>
<aside class="notes">
<p><strong>Teaching Tips</strong>:</p>
<ul>
<li>Quickly review. Check to see if anyones 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">&quot;The total of all the batting averages is&quot;</span>, sum_avgs)</a>
<a class="sourceLine" id="cb13-6" data-line-number="6"><span class="co"># =&gt; 2.409</span></a></code></pre></div>
<aside class="notes">
<p><strong>Teaching Tips</strong>:</p>
<ul>
<li>If baseballs 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>“Theres 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">&quot;The highest batting average is&quot;</span>, <span class="bu">max</span>(team_batting_avgs))</a>
<a class="sourceLine" id="cb14-6" data-line-number="6"><span class="co"># =&gt; 0.328</span></a>
<a class="sourceLine" id="cb14-7" data-line-number="7"><span class="bu">print</span>(<span class="st">&quot;The lowest batting average is&quot;</span>, <span class="bu">min</span>(team_batting_avgs))</a>
<a class="sourceLine" id="cb14-8" data-line-number="8"><span class="co"># =&gt; 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 thats 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&amp;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">&quot;red&quot;</span>, <span class="dv">7</span>, <span class="st">&quot;yellow&quot;</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">&quot;Yi&quot;</span>) <span class="co"># [&quot;red&quot;, 7, &quot;yellow&quot;, 1, &quot;Yi&quot;]</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">&quot;Sanju&quot;</span>) <span class="co"># [&quot;red&quot;, &quot;Sanju&quot;, 7, &quot;yellow&quot;, 1, &quot;Yi&quot;]</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"># &quot;Yi&quot;; [&quot;red&quot;, &quot;Sanju&quot;, 7, &quot;yellow&quot;, 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; [&quot;red&quot;, &quot;Sanju&quot;, &quot;yellow&quot;, 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 theyll get used to it. If they always copy the code off the slide, they wont be very quick to learn it!</li>
</ul>
</aside>
<hr />
</section>
<section id="summary-and-qa-1" class="level2">
<h2>Summary and Q&amp;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 theyre 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>