Beruflich Dokumente
Kultur Dokumente
Hélène Martin
Puget Sound CSTA
December 11th, 2010
Bachelor’s 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
Why Teach Python?
Light-weight syntax
Multi-paradigm (procedural, OO, functional)
Lots of great libraries
- turtle graphics
- pygame
- scipy
- myro (workshop last spring)
Interpreted
Permissive
Used increasingly in college courses, web applications
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)
Creative Computing
Free at http://python.org
Generally pre-installed on OS X or Linux
Includes IDLE, a basic IDE
Interacting With IDLE
http://docs.python.org/library/turtle.html
- forward() | fd() - write()
- backward() | bk() | back() - showturtle() | st()
- right() | rt() - hideturtle() | ht()
- left() | lt() - shape()
- goto() | setpos() | setposition() - register_shape() | addshape()
- stamp()
- speed()
- pendown() | pd() | down()
- penup() | pu() | up()
- pensize() | width()
- color()
- begin_fill()
- end_fill()
- clear()
Geometry exercises
Definite loop
for loop structure
1 for i in range(<times>):
2 <statements>
3 <statements>
4 print("Hi") # not repeated
Indefinite loop
while loop strcture
1 while(<condition>):
2 <statements>
3 <statements>
4 print("Hi") # not repeated
White Space
Indentation matters
1 for i in range(3):
2 print("Hello") # repeated
3 print(25) # repeated
4 print("Hi") # not repeated
Functions
Definition
The def keyword is used for defining functions
1 def hello(<param1>, <param2>...):
2 print("Hello, world!")
3
4 hello()
5 hello()
Calling
- Students often forget to use parentheses when calling
Print Function
Arithmetic:
- ** (exponent)
- * (multiplication) / (division) % (modulus)
- + (addition) - (subtraction)
Boolean
- == != < <= > >=
- not
- and or
Common Types
- int, float, bool, str
Conditionals
Conditional structure
1 if(<condition>):
2 <statements>
3 elif(<condition>):
4 <statements>
5 else:
6 <statements>
User input
User input
1 >>> name = raw_input("Your name?")
2 Your name? Bonnie
3
4 >>> name
5 'Bonnie'
6
http://docs.python.org/library/stdtypes.html#string-methods
Lists
Lists
1 >>> nums = [2, 4, 6, 8]
2 >>> for num in nums:
3 print num
4 2
5 4
6 6
7 8
8 >>> nums.reverse()
9 >>> nums
10 [8, 6, 4, 2]
11 >>> nums.append(10)
12 >>> nums
13 [8, 6, 4, 2, 10]
14 >>> nums.sort()
15 >>> nums
16
17 [2, 4, 6, 8, 10]
18 >>> nums[len(nums) - 2]
19 8
Your turn
File reading
1 >>> f = open("my_file")
2
3 >>> for line in f:
4 ...
5
1 def longest_line(f):
2 maxline = ""
3 for line in f:
4 if len(line) > len(maxline):
5 maxline = line
6 return maxline
7
File projects
Dictionaries
point.py
• No encapsulation
• Explicit use of self
• __what?!__
Python Resources
http://python.org/
http://diveintopython.org
http://openbookproject.net//thinkCSpy/
Scribblers