Week 2: Python Meeting Turtle

Review

  • X/Y axes & coordinates
  • Pen up, pen down
  • Begin fill, End fill

Discussion

  • Import turtle
    • Allows us to use the turtle library, with its various functions to help us create shapes.
  • Functions
    • A block of organized, reusable code that is used to perform a certain action
    • Built-in functions: written within the Python library
      • Sometimes calling a function is all that is needed.
        • i.e. turtle.penup()
      • Sometimes functions need additional information passed in by the user (input)
        • i.e. turtle.color(“color”)
    • User-defined functions (later): written by the programmer
  • Comments
    • #this is a single-line comment



    • t
      h
      i
      s
      i
      s
      a
      M
      u
      l
      t
      i

      l
      i
      n
      e
      C
      o
      m
      m
      e
      n
      t


    • Code that is not run by the program
  • Use comments according to your own programming style. Comments are less for the user, and more about providing helpful information to you as the programmer. For example,
    • Comments may be used above a block of code to remind yourself what that code does. Now when you review your program, you will not need to read every line to remember its purpose.
    • If there is a line of code that you want to keep, but you would like to test the program, you may comment out that line. When you want to bring the line back to be run by the program, remove the #.
  • Errors
    • Python does its best to tell you what is wrong with your code by providing error statements.
    • It is the programmer’s job to debug (find the problem and fix it)

Common Commands

import turtleturtle.circle(size)
x=turtle.Turtles()/import turtle as xturtle.right(degrees) /
turtle.left(degrees)
turtle.shape(“shape”)turtle.forward(length) /
turtle.backwards(legth)
turtlepenup() / turtle.pendown()turtle.speed(speed)
turtle.pencolor(“color”) /
turtle.fillcolor(“color”) /
turtle.pencolor(r,g,b)
turtle.width(width)
turtle.goto(x,y)turtle.begin_fill() /
turtle.end_fill()

Initialize Program

import turtle
import random
t = turtle.Turtle()
t.shape(‘turtle”)
t.speed(speed)

Practice

Create a snowflake arm

Snowflake arm