2010 12 11 python workshop

Upload: helene-martin

Post on 09-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 2010 12 11 Python Workshop

    1/30

    Python

    Hlne Martin

    Puget Sound CSTA

    December 11th, 2010

    Special thanks to Marty Stepp for some formatting and content

  • 8/8/2019 2010 12 11 Python Workshop

    2/30

    Hlne Martin

    Bachelors in CS from UW

    Career and Technical Ed through South Seattle

    Teach at comprehensive public high school

    Five periods of computer science

    - 2x Exploring Computer Science- 2x AP Computer Science- 1x AP Computer Science (Experienced)

    http://garfieldcs.com

    http://garfieldcs.com/http://garfieldcs.com/http://garfieldcs.com/
  • 8/8/2019 2010 12 11 Python Workshop

    3/30

    Why Teach Python?

    Light-weight syntax

    Multi-paradigm (procedural, OO, functional)

    Lots of great libraries- turtle graphics- pygame- scipy- myro (workshop last spring)

    InterpretedPermissive

    Used increasingly in college courses, web applications

  • 8/8/2019 2010 12 11 Python Workshop

    4/30

    Why Not Teach Python?

    Permissive

    Dynamically typed (values have types, but not

    variables)

    Too many ways to skin a single cat!

    Not used much in corporate world

    - Though YouTube, Google, Yahoo!, NASA make some use of it- Primarily for internal tools (think Perl)

  • 8/8/2019 2010 12 11 Python Workshop

    5/30

    Creative Computing

    Semester-long high school course

    Only pre-requisite is algebra 1

    Topic progression:

    - Basic syntax with turtle graphics (iteration, variables,conditionals)

    - Functions (abstraction)

    - Compound data: strings and lists- Programming robots- Object-oriented basics- Games with pyGame

  • 8/8/2019 2010 12 11 Python Workshop

    6/30

    Getting Python

    Free at http://python.org

    Generally pre-installed on OS X or Linux

    Includes IDLE, a basic IDE

    http://python.org/http://python.org/
  • 8/8/2019 2010 12 11 Python Workshop

    7/30

    Interacting With IDLE

    Programs saved with .py extensions

    Commands interpreted straight in shell

    Students can get confused about the two!!

  • 8/8/2019 2010 12 11 Python Workshop

    8/30

    Procedural Turtle Graphics

    Long tradition started by Logo

    Drawing shapes gives instant feedback

    Students get used to syntax, algorithms

    Note: students often savefiles as turtle.py - remind

    them not to do that!

  • 8/8/2019 2010 12 11 Python Workshop

    9/30

    Available functions

    http://docs.python.org/library/turtle.html

    - forward() | fd()- backward() | bk() | back()- right() | rt()- left() | lt()- goto() | setpos() | setposition()- stamp()- speed()- pendown() | pd() | down()- penup()| pu() | up()

    - pensize() | width()- color()- begin_fill()- end_fill()- clear()

    - write()- showturtle() | st()- hideturtle() | ht()- shape()- register_shape() | addshape()

    http://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.end_fillhttp://docs.python.org/library/turtle.html#turtle.begin_fillhttp://docs.python.org/library/turtle.html#turtle.colorhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.pdhttp://docs.python.org/library/turtle.html#turtle.speedhttp://docs.python.org/library/turtle.html#turtle.speedhttp://docs.python.org/library/turtle.html#turtle.speedhttp://docs.python.org/library/turtle.html#turtle.speedhttp://docs.python.org/library/turtle.html#turtle.stamphttp://docs.python.org/library/turtle.html#turtle.stamphttp://docs.python.org/library/turtle.html#turtle.stamphttp://docs.python.org/library/turtle.html#turtle.lefthttp://docs.python.org/library/turtle.html#turtle.righthttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.begin_fillhttp://docs.python.org/library/turtle.html#turtle.begin_fillhttp://docs.python.org/library/turtle.html#turtle.begin_fillhttp://docs.python.org/library/turtle.html#turtle.addshapehttp://docs.python.org/library/turtle.html#turtle.addshapehttp://docs.python.org/library/turtle.html#turtle.register_shapehttp://docs.python.org/library/turtle.html#turtle.register_shapehttp://docs.python.org/c-api/buffer.html#shapehttp://docs.python.org/c-api/buffer.html#shapehttp://docs.python.org/library/turtle.html#turtle.hthttp://docs.python.org/library/turtle.html#turtle.hthttp://docs.python.org/library/turtle.html#turtle.hideturtlehttp://docs.python.org/library/turtle.html#turtle.hideturtlehttp://docs.python.org/library/turtle.html#turtle.sthttp://docs.python.org/library/turtle.html#turtle.sthttp://docs.python.org/library/turtle.html#turtle.showturtlehttp://docs.python.org/library/turtle.html#turtle.showturtlehttp://docs.python.org/library/turtle.html#turtle.writehttp://docs.python.org/library/turtle.html#turtle.writehttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.clearhttp://docs.python.org/library/turtle.html#turtle.end_fillhttp://docs.python.org/library/turtle.html#turtle.end_fillhttp://docs.python.org/library/turtle.html#turtle.begin_fillhttp://docs.python.org/library/turtle.html#turtle.begin_fillhttp://docs.python.org/library/turtle.html#turtle.colorhttp://docs.python.org/library/turtle.html#turtle.colorhttp://docs.python.org/library/turtle.html#turtle.widthhttp://docs.python.org/library/turtle.html#turtle.widthhttp://docs.python.org/library/turtle.html#turtle.pensizehttp://docs.python.org/library/turtle.html#turtle.pensizehttp://docs.python.org/library/turtle.html#turtle.pensizehttp://docs.python.org/library/turtle.html#turtle.pensizehttp://docs.python.org/library/turtle.html#turtle.uphttp://docs.python.org/library/turtle.html#turtle.uphttp://docs.python.org/library/turtle.html#turtle.puhttp://docs.python.org/library/turtle.html#turtle.puhttp://docs.python.org/library/turtle.html#turtle.penuphttp://docs.python.org/library/turtle.html#turtle.penuphttp://docs.python.org/library/turtle.html#turtle.downhttp://docs.python.org/library/turtle.html#turtle.downhttp://docs.python.org/library/turtle.html#turtle.pdhttp://docs.python.org/library/turtle.html#turtle.pdhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.pendownhttp://docs.python.org/library/turtle.html#turtle.speedhttp://docs.python.org/library/turtle.html#turtle.speedhttp://docs.python.org/library/turtle.html#turtle.stamphttp://docs.python.org/library/turtle.html#turtle.stamphttp://docs.python.org/library/turtle.html#turtle.setpositionhttp://docs.python.org/library/turtle.html#turtle.setpositionhttp://docs.python.org/library/turtle.html#turtle.setposhttp://docs.python.org/library/turtle.html#turtle.setposhttp://docs.python.org/library/turtle.html#turtle.gotohttp://docs.python.org/library/turtle.html#turtle.gotohttp://docs.python.org/library/turtle.html#turtle.lthttp://docs.python.org/library/turtle.html#turtle.lthttp://docs.python.org/library/turtle.html#turtle.lefthttp://docs.python.org/library/turtle.html#turtle.lefthttp://docs.python.org/library/turtle.html#turtle.rthttp://docs.python.org/library/turtle.html#turtle.rthttp://docs.python.org/library/turtle.html#turtle.righthttp://docs.python.org/library/turtle.html#turtle.righthttp://docs.python.org/library/turtle.html#turtle.backhttp://docs.python.org/library/turtle.html#turtle.backhttp://docs.python.org/library/turtle.html#turtle.bkhttp://docs.python.org/library/turtle.html#turtle.bkhttp://docs.python.org/library/turtle.html#turtle.backwardhttp://docs.python.org/library/turtle.html#turtle.backwardhttp://docs.python.org/library/turtle.html#turtle.fdhttp://docs.python.org/library/turtle.html#turtle.fdhttp://docs.python.org/library/turtle.html#turtle.forwardhttp://docs.python.org/library/turtle.html#turtle.forwardhttp://docs.python.org/library/turtle.htmlhttp://docs.python.org/library/turtle.htmlhttp://docs.python.org/library/turtle.html
  • 8/8/2019 2010 12 11 Python Workshop

    10/30

    Geometry exercises

    Reasoning about repetition

    Breaking down a problem into steps

    http://neoparaiso.com/logo/ejercicios-de-

    geometria.html

    http://neoparaiso.com/logo/ejercicios-de-geometria.htmlhttp://neoparaiso.com/logo/ejercicios-de-geometria.htmlhttp://neoparaiso.com/logo/ejercicios-de-geometria.htmlhttp://neoparaiso.com/logo/ejercicios-de-geometria.html
  • 8/8/2019 2010 12 11 Python Workshop

    11/30

    Iteration

    Definite loop

    Indefinite loop

    for loop structure

    1

    234

    for i in range():

    print("Hi") # not repeated

    while loop strcture

    1234

    while():

    print("Hi") # not repeated

  • 8/8/2019 2010 12 11 Python Workshop

    12/30

  • 8/8/2019 2010 12 11 Python Workshop

    13/30

    Functions

    Definition

    Calling

    - Students often forget to use parentheses when calling

    The def keyword is used for defining functions

    1

    2345

    def hello(, ...):

    print("Hello, world!")

    hello()hello()

  • 8/8/2019 2010 12 11 Python Workshop

    14/30

    Print Function

    Simple! (no System.out.yuck)

    Can be called with parameters of different types

    Concatenation shows ugliness of dynamic types

    Print can dis la different t es

    1

    23

    4

    print("Hello, world!")

    print()print(25)

    print("You have " + str(dollars) + " dollars")

  • 8/8/2019 2010 12 11 Python Workshop

    15/30

    Python Operations

    Arithmetic:

    - ** (exponent)- * (multiplication) / (division) % (modulus)- + (addition) - (subtraction)

    Boolean

    - == != < >=- not

    - and or

    Common Types

    - int, float, bool, str

  • 8/8/2019 2010 12 11 Python Workshop

    16/30

    Conditionals

    Co ditional structure

    123456

    if(): elif():

    else:

  • 8/8/2019 2010 12 11 Python Workshop

    17/30

    User input

    Us r input

    123456

    >>> name = raw_input("Your name?")Your name? Bonnie

    >>> name'Bonnie'

    User input - numbers123456

    >>> age = int(raw_input("Your age?"))Your age? 15

    >>> age15

  • 8/8/2019 2010 12 11 Python Workshop

    18/30

    External Libraries

    Import modules to get access to more functions

    Usi g random numbers

    1234

    from random import *

    die1 = randint(1, 6)die2 = randint(1, 6)

  • 8/8/2019 2010 12 11 Python Workshop

    19/30

  • 8/8/2019 2010 12 11 Python Workshop

    20/30

  • 8/8/2019 2010 12 11 Python Workshop

    21/30

    Iterating over strings

    Can access length using len() function

    Can use for loop to examine each character

    Looping over a string

    123456

    78910

    >>> for c in "PSCSTA":print c

    PS

    CSTA

  • 8/8/2019 2010 12 11 Python Workshop

    22/30

    String methods

    str.capitalize()

    str.endswith(suffix)

    str.find(sub)

    str.isdigit()

    str.join(iterable)

    str.lower()

    str.replace(old, new)str.split([sep])str.startswith(prefix)

    str.strip()

    str.title()

    str.upper()

    http://docs.python.org/library/stdtypes.html#string-methods

    http://docs.python.org/library/stdtypes.html#string-methodshttp://docs.python.org/library/stdtypes.html#string-methodshttp://docs.python.org/library/stdtypes.html#string-methodshttp://docs.python.org/library/stdtypes.html#string-methods
  • 8/8/2019 2010 12 11 Python Workshop

    23/30

    Lists

    Lists

    1234

    5678910

    111213

    14

    1516171819

    >>> nums = [2, 4, 6, 8]>>> for num in nums:

    print num2468>>> nums.reverse()>>> nums[8, 6, 4, 2]>>> nums.append(10)>>> nums[8, 6, 4, 2, 10]>>> nums.sort()>>> nums[2, 4, 6, 8, 10]>>> nums[len(nums) - 2]8

  • 8/8/2019 2010 12 11 Python Workshop

    24/30

    Your turn

    Write a pig latin programPhrase to translate? What a lovely day it is

    hatWAY AAY ovelyLAY ayDAY tIAY sIAY

    Simple: move the first letter to the back and add

    AY. Capitalize the last three letters

    More complex: if the word starts with a vowel,

    just add ay. Move first consonant cluster.- trash -> ashTRAY- ashtray -> ashtrayAY- Write a boolean function is_vowel

  • 8/8/2019 2010 12 11 Python Workshop

    25/30

    File reading

    File reading

    1234

    5

    >>> f = open("my_file")

    >>> for line in f:...

    Longe t line in a file

    12

    34567

    def longest_line(f):maxline = ""for line in f:

    if len(line) > len(maxline):maxline = line

    return maxline

  • 8/8/2019 2010 12 11 Python Workshop

    26/30

    File projects

  • 8/8/2019 2010 12 11 Python Workshop

    27/30

    Dictionaries

    Write a program to count the frequency of words in the Washington state

    programming framework.

    >>> import operator>>> sorted(words.iteritems(), key=operator.itemgetter(1))

    L ngest line in a file

    123

    45

    6789

    >>> birth_years = {"Bob":1983, "Mary":1955, "Joe":2000}>>> birth_years["Bob"]1983>>> birth_years.keys()['Bob', 'Mary', 'Joe']>>> birth_years.values()[1983, 1955, 2000]>>> birth_years.has_key("Andy")False

  • 8/8/2019 2010 12 11 Python Workshop

    28/30

    Classes

    No encapsulation

    Explicit use of self

    __what?!__

    point.py

    123

    4

    56789

    1011121314

    from math import *

    class Point:def __init__(self, x, y):

    self.x = xself.y = y

    def distance(self, other):dx = self.x - other.xdy = self.y - other.yreturn sqrt(dx * dx + dy * dy)

    def __str__(self):return "(" + str(self.x) + ", " + str(self.y) + ")"

  • 8/8/2019 2010 12 11 Python Workshop

    29/30

    Python Resources

    http://python.org/

    http://diveintopython.org

    http://openbookproject.net//thinkCSpy/

    http://openbookproject.net//thinkCSpy/http://openbookproject.net//thinkCSpy/http://diveintopython.org/http://diveintopython.org/http://python.org/http://python.org/
  • 8/8/2019 2010 12 11 Python Workshop

    30/30

    Scribblers