Modeling Code#
Objectives#
To interact with bits of Python code as commands that transform inputs to outputs.
To develop a conceptual model of basic elements of programming (variables, data structures, loops).
To collaborate in understanding code.
What is a model?
A model is a way of representing something. In writing code, we are often engaged in the act of modeling, in the sense that we are creating representations of aspects of the world for use by a computer. In our next activity, you’ll work through an example of modeling information about GW courses and textbooks.
In this activity, the goal is to model – by drawing on paper – what you think is happening inside the computer when you run a given bit of Python code. We obviously haven’t explained what the code means, but don’t worry – we will cover that later. For now, we just want to get used to running Python code, and to practice paying attention to the effects that the code produces on the computer.
A line of Python is an instruction or command issued to the computer, directing it to take some action. In what follows, see if you can model the actions by drawing on paper what you think might be happening when you run the code.
Instructions#
This notebook contains a few cells of Python code. These are labeled Action and numbered 1 through 5. Each team will be assigned three actions.
Your task, as a team, is as follows:
Run the cell of code corresponding to the action.
Examine the output.
Model the action that the code performs, using pen, paper, and (if you like) sticky notes.
Below each cell is a hidden cell containing some hints. Click the green
Hint
panel to reveal them.Some actions are also followed by a cell linking to an animation that presents one possible way of modeling the action. But try to come up with your own model before watching the animation.
Once all teams have worked through their actions, we’ll share our models for discussion in the larger group.
Running code in the notebook
To run each cell, place your cursor in the cell and press Control
+ Enter
(or Command
+ Return
on a Mac). The output of running the code will appear below the cell.
Action 1: Assigning and modifying a variable#
num = 3
num = num + 1
print(num)
Action 2a: Creating a variable (number)#
num_participants = 20
print(num_participants)
Hint
The name (left side of the equals sign) acts like a label for the value (right side of the equal sign – here, the number 20).
The number 20 is written without quotation marks. What happens if you add them, like so?
"20"
Animation
Watch an animation of this concept!
Action 2b: Creating a variable (string)#
my_workshop = "Python Camp"
print(my_workshop)
Hint
Try changing the text between quotation marks to see how the output varies.
The name (left side of the equals sign) acts like a label for the value (right side of the equal sign – here, the content between quotation marks).
What do you think
print
does when it is given the namemy_workshop
?
Animation
Watch an animation of this concept!
Action 3a: Working with numbers#
num_students = 50
book_price = 59.99
total_cost = num_students * book_price
print(total_cost)
Hint
Variables in programming are similar to variables in algebra.
Whereas algebraic variables represent unknown values in equations to be solved, in programming, we use variables for many different reasons.
One important use for variables is simply to make our code more readable: e.g., to assign names to quantities.
What happens if you put the numbers in the code above inside quotation marks?
Action 3b: Working with strings#
book_title = "Python Distilled"
book_author = "David Beazley"
print(book_title + " by " + book_author)
Hint
Anything in Python between quotation marks is called a string.
When working with strings, it’s important to pay close attention to what falls inside the quotation marks and what sits outside the quotation marks.
The Jupyter Notebook interface helpfully highlights strings in red to distinguish them from the surrounding code.
Action 4a: Creating a list#
book_prices = [129.99, 99.95, 13.99, 50.00, 250.00]
print(book_prices[0])
print(book_prices[4])
print(book_prices[-1])
Hint
Lists allow us to store values in a certain order in the computer’s memory.
We can access the values in the stored list by their position or index.
Try changing the numbers in square brackets to see what happens to the output.
In math, we use negative numbers to count down backwards from zero (
-1, -2, -3...
). Can you tell what the-1
inbook_prices[-1]
is doing here?
Animation
Watch an animation of this concept!
Action 4b: Slicing a string#
course_info = "CHEM 1012"
dept_code = course_info[0:4]
dept_code
Hint
A string is a sequence of characters (just as written words in English can be described as sequences of letters).
Each character in the sequence has a position, called an index.
The first index in the sequence is numbered
0
, not1
.Note the numbers separated by the colon (
:
) and enclosed in square brackets ([]
). Try changing these numbers and see how it effects the output.
Animation
Watch an animation of this concept!
Action 5: Looping over a list#
book_prices = [129.99, 99.95, 13.99, 50.00, 250.00]
for price in book_prices:
sales_tax = price * .1
print(price + sales_tax)
Hint
We often use lists in Python to hold data that we want to process in sequence – such as applying sales tax to every price in a list of prices.
Note that
book_prices
here is the same variable used in the previous action.A
for
loop allows us to perform the same action for every value in a list of values.Try adding or removing numbers from the
book_prices
list to see what happens to the output.
Animation
Watch an animation of this concept!