Unit 2. Developing Programming Tools
Revision Date: Jan 22, 2017 (Version 2.1.1)Summary
Students will learn to manipulate variables and value assignments through an activity in which they must become the variable. By the end of the lesson, they will have identified variables as memory locations. They will also assign, copy, and destroy values in order to perform a swap algorithm and visualize Python's manipulation of variables and values in memory.
Outcomes
Overview
Student computer usage for this lesson is: none
For the Students:
Optional:
For the Teacher:
team1 = "Miami Heat"
team2 = "Washingon Wizards"
temp = team1
team1 = team2
team2 = temp
What is a variable? What are some things in your life that change often?
See slides in Lesson Resources Folder for a guided introduction.
Disposable cups, index cards, names for variables on strings (to hang around students' necks)
tree
, because the word tree
has no relevance to your age.)team1
because we’re talking about sports teams. Put the name around the student’s neck to indicate they have become the variable team1. This student has now become team1
, and should not respond to any other names! Note: A variable name will never have quotes around it - that would indicate that it is a string (i.e., a type of value rather than a variable's name). Variables can be identified (loosely) by words or letters that are not in quotes and are not keywords (for, if, else, etc.).team1
. Because we have never used this variable before, we are initializing it - putting something in this box (memory location) for the first time. On the board, write team1 = "Miami Heat"
team1
is the same as the Washington Wizards; it is only where were are currently keeping that team. Write “Miami Heat” on a notecard and put it into the cup.team1
and "Miami Heat", this time using team2
and "Washington Wizards".team1
and team2
?team1
and team2
to swap their values. Key point: when we access a variable and put its value in the place of another variable, that value is being copied. By doing so, however, any previous value in the variable is lost. temp
.temp
.)temp
variable, so the variables do not overwrite one another.temp
(named so because we will not be using it very long). Write temp = team1
on the board. Notice that in this assignment, it looks like we are setting a variable equal to another variable. Instead, we are setting the variable temp
equal to the value inside the variable team1
. This means that the value is copied to a second location.temp
.temp
cup.team1
. Write team1 = team2
on the board.team2
and copy the text from the notecard onto a new one. Move to team1
and replace the previous notecard with this new one.team1
. Rip up the notecard or throw it in the trash.team2 = temp
by following the same process. Look at the code written on the board and ask a student to walk us through each step.
CFU: Why did we create the variable temp
?
(Because variables overwrite the values of one another, and if we were to just set team2 = team1
we would lose one of the values.)
Students should notice:
In reality, Python actually has a "shortcut syntax" that allows us to make this swap in one step. It looks like this:
a,b = b,a
If we wanted to swap the values in team1
with team2
, we would simply have to write:
team1,team2 = team2, team1
Here, Python is doing exactly what we were doing. It is internally creating a variable (which has no name but serves the same purpose as our temp
variable), using it as a place holder, and then completing the swap.
Why would the makers of Python build in this function? What other uses does it have?
A version of this worksheet can be found in the Lesson Resources folder, titled "Swap to the Top".
Give students a list of games that have been played by the teams on the board, and the resultant new ranking. Have them create a piece of code that will reorganize the teams into the correct ranking. (They can assume that the variables team1
... team13
already exist and have been initialized to the Friday ranking.) Give them the option of doing so through the use of manipulatives, or on their own paper.
Discuss: All computer programs can be broken down into smaller, simpler steps. By developing components, testing to be sure they are correct and combining them you can create complex, correct programs. Did students get a feeling for systematic development, and how to trace a program one step at a time to verify correctness? Distribute Exit Ticket in Lesson Resources Folder which asks students to figure out the values of variables hello and goodbye after the code in each exercises been executed.
What is the difference between a variable in a math class and in a computer science class? What is the difference between a float and an integer? Why would you use one instead of the other?
Continue working on "Swap to the Top" worksheet.
Create a 3 column ‘one-pager’. In the left column, create a copy of the code from the activity (a swap algorithm) including team1
, team2
, and temp
that performs the complete swap. In the middle column, write steps that occur in the code (Step 1: Initialize variable and assign the value). In the third column, the students should draw a visual of what that looks like in terms of disposable cups and index cards. (They can just write the variable name on the cup, rather than drawing in a person as well.)
Checks for Understanding are embedded in the lesson. They are also shared below.
What is the type of “Miami Heat”?
(“Miami Heat" is a string. We know this because it has quotes around it .)
What is the difference between a value and a variable?
(Values can be stored in variables. The content of a variable can change to different values, but its name will always be the same because the name is just an identifier of a location in memory.)
Think-Pair-Share: A value is only safe (and not lost to the world of cyberspace) if it is in a variable. A variable can only hold one value at a time. How can we swap values between team1 and team2?
(various answers)
Why did we create the variable temp
?
(Because variables overwrite the values of one another, and if we were to just set team2 = team1,
we would lose one of the values.)
Task: Create a piece of code that will rearrange the ranking of the teams in order to reflect the outcome of previous games. (Independent Practice)