Data Types, Turtle Drawing, and Functions

Welcome to the first day of class!

Reading

Chapters 2, 4, and 6.

Key ideas

From chapter 2:

  • Simple python types: int(eger), float, str(ing)
  • Determing type: type
  • Declaring and using variables
  • Assignment token: =
  • Arithmetic operators: +, -, *, /, //, %, **
  • User input: input

From chapter 4:

  • Turtle Module Methods
    • Creation: turtle
    • Movement: forward, backward, goto
    • x, y orientation: right, left
    • Pen control: up, down, pensize
    • Drawing Color: color, fillcolor, begin_fill, end_fill
    • Status: heading, position
    • Turtle type: shape, e.g. arrow, classic, turtle or circle
    • Turtle Imprints: stamp, dot
  • Looping Construct: for
  • onclick(), onrelease(), ondrag() from turtle online documentation

From chapter 6:

  • Be able to write a function.
  • Be able to call a function.
  • Understand function parameters.
  • Understand the difference between a fruitful function and a non-fruitful function.
  • Understand the difference between a local variable and a global variable.

Active learning

Activity 1

Evaluate the following numerical expressions:

5 ** 2
9 * 5
15 / 12
12 / 15
15 // 12
12 // 15
5 % 2
9 % 5
15 % 12
12 % 15
6 % 6
0 % 7

Activity 2

Download madlib.py and modify it to create your own story. Your modified Mad Lib should use (1) at least one input that is treated as a string, (2) at least one input that is treated as an integer and (3) at least one input that is treated as a floating point number.

Activity 3

Look at racing-turtles.py and make sure that you understand it fully. Then,

  1. Modify the program so that a third racing turtle starts at coordinate (-200, -100).
  2. Write down three other improvements to the program.
  3. Find someone to discuss your proposed improvements with.
  4. Implement at least one of your proposed improvements.

Activity 4

Look at etch-a-sketch.py and make sure that you understand it fully.

  1. Write down three improvements you could make to the program.
  2. Find someone to discuss your proposed improvements with.
  3. Implement at least one of your proposed improvements.

Activity 5

Which of the following best reflects the order in which these lines of code are processed in Python?

1  def pow(b, p):
2      y = b ** p
3      return y
4
5  def square(x):
6      a = pow(x, 2)
7      return a
8
9  n = 5
10 result = square(n)
11 print(result)

What does the program print?

Activity 6

Improve house.py using functions.