Docsity

Lecture Notes for CPSC 121 Fall 2011: Variables and Turtle Graphics, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes for CPSC 121 Fall 2011: Variables and Turtle Graphics and more Study notes Computer Science in PDF only on Docsity!

Today ...

  • Variables
  • Basic turtle graphics

Homework

  • HW 2 out (due next Wed.)

Reading Assignment

  • Ch. 3
  • Pg. 170 (on turtle)

Variables

A variable is a name (label) that “refers” to (i.e., “references”) a value

Assignment statements create new variables and give them values

course = "CPSC121" year = 2011 pi = 3.

Each variable refers to its assigned value

course ’CPSC121’

We’ll often denote the value of a variable using an arrow

course −→ "CPSC121"

  • The value is stored in a slot in memory
  • The variable acts as a “name tag” (think “sticky note”) for the value
  • The variable allows us to get the value out of memory

We can use variables in place of their values

type(pi) <class ’float’>

Variables can be reassigned (example of “dynamic typing”)

pi = course # note: assigned a string! type(pi) <class ’str’>

Reference Diagrams

Statments within a sequence of statements are evaluated in order ...

  • the first statement is evaluated first
  • then the second one is evaluated
  • then the third one is evaluated
  • and so on

Assignment statements can make this a bit tricky

For example:

x = 100 y = 200 z = x x = 300 After these statements are executed, was is the value of x?

Reference (aka state and memory) diagrams can help!

  • After the first three statements

x, z −→ 100 y −→ 200

  • After the last statement (x = 300)

z −→ 100 y −→ 200 x −→ 300

Excercise: After these statements are executed ...

x = 15 y = x x = 20 z = y y = x + z z = 5

What are the values of x, y, and z?

Some general hints about assignment

  • Assigning a new value to a variable doesn’t change other variables (e.g., x=15, y=x, x=20 doesn’t change y from 15 to 20 ).
  • Once a statement is executed, a subsequent assignment doesn’t trigger re- computation (e.g., y=x+z, z=5 doesn’t change y from 35 to 25 ).

Exercise

  1. In the python shell enter the following:

import turtle pen = turtle.Turtle() pen.goto(0, 100) pen.goto(100, 100) pen.goto(100, 0) pen.goto(0, 0)

  1. Draw a similar square, but with the lower left-hand corner at (50, 50)
    • Be sure to lift up the pen and move it before drawing the new square
  2. Now use the turtle object to make a cube out of your two squares
    • If you make a mistake:
      • Use pen.clear() to clear the canvas
      • Or undo the previous command ...

        pen.undo()

Statements

A statement is a chunk of code the interpreter can execute

So far we’ve looked at 3 simple kinds

  • function call (using print and type)
  • assignment statement (e.g., course = "CPSC121")
  • simple math expressions (e.g., 3 + 4)

After entering a statement into the intepreter

  • the statement is read
  • the statement is executed (evaluated)
  • the result is displayed (if there is one)
  • e.g., assignment doesn’t produce a result (except assigning the variable)
  • often called the “read-eval-print” loop