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:

  1. Run the cell of code corresponding to the action.

  2. Examine the output.

  3. Model the action that the code performs, using pen, paper, and (if you like) sticky notes.

  4. Below each cell is a hidden cell containing some hints. Click the green Hint panel to reveal them.

  5. 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)

Animation

Watch an animation of this concept!

Action 2b: Creating a variable (string)#

my_workshop = "Python Camp"
print(my_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)

Action 3b: Working with strings#

book_title = "Python Distilled"
book_author = "David Beazley"
print(book_title + " by " + book_author)

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])

Animation

Watch an animation of this concept!

Action 4b: Slicing a string#

course_info = "CHEM 1012"
dept_code = course_info[0:4]
dept_code

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)

Animation

Watch an animation of this concept!