![Page 1: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/1.jpg)
PLATINUM)SPONSORS)
GOLD)SPONSORS)
SILVER)SPONSORS)
Saturday, May 4, 13
![Page 2: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/2.jpg)
Juan GomezCo-Founder of PyhtonKC
Twitter: @_juandg
Teach your kids how to program with Python and the Raspberry Pi
Saturday, May 4, 13
![Page 3: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/3.jpg)
May the 4th be with you!Saturday, May 4, 13
![Page 4: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/4.jpg)
So what is this talk really about?
Saturday, May 4, 13
![Page 5: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/5.jpg)
Brief History of the RaspberryPiMore at: https://www.youtube.com/watch?v=z-j4USHTLWM
Saturday, May 4, 13
![Page 6: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/6.jpg)
Saturday, May 4, 13
![Page 7: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/7.jpg)
What you’ll need:
• MicroUSB Power Supply• SD Card (>= 4GB)• USB Keyboard• USB Mouse• Ethernet Cable• HDMI Cable• Monitor• Case?• HDMI to VGA Adapter?• USB WiFi Adapter?• USB to TTL Cable?• RCA Cable?
Saturday, May 4, 13
![Page 8: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/8.jpg)
How do you teach kids?
Saturday, May 4, 13
![Page 9: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/9.jpg)
Make it fun!http://www.adafruit.com/products/975
Saturday, May 4, 13
![Page 10: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/10.jpg)
Some tips
• Seriously, Make it fun!
• Start with Math
• Be Patient
• Explain the Basics
• Use real life examples
• Kids are slow typists!
Saturday, May 4, 13
![Page 11: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/11.jpg)
How do I set it up?
Saturday, May 4, 13
![Page 12: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/12.jpg)
Saturday, May 4, 13
![Page 13: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/13.jpg)
Disclaimer
• This an intro to Python for YOU, not your kids.
• The kids version is at: https://github.com/mechanicalgirl/young-coders-tutorial
Saturday, May 4, 13
![Page 14: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/14.jpg)
A Python Code Sample
x = 34 - 23 # A comment.
y = “Hello” # Another one. z = 3.45
if z == 3.45 or y == “Hello”:
x = x + 1
y = y + “ World” # String concat.
print x
print y
Saturday, May 4, 13
![Page 15: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/15.jpg)
Enough to Understand the Code
• First assignment to a variable creates it • Assignment is = and comparison is ==• For numbers + - * / % are as expected
• Special use:• + for string concatenation• % for string formatting (as in C’s printf)
• Logical operators are words (and, or, not) not symbols (&&, ||, !).
• The basic printing command is print
Saturday, May 4, 13
![Page 16: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/16.jpg)
Comments
•Start comments with #, rest of line is ignored•Can include a “documentation string” as the first line of a new function or class you define•Development environments, debugger, and other tools use it: it’s good style to include one
def my_function(x, y): “““This is the docstring. This function does blah blah blah.”””# The code would go here...
Saturday, May 4, 13
![Page 17: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/17.jpg)
Python and Types
•Everything is an object!
•“Dynamic Typing”-> Data types determined automatically.
•“Strong Typing” -> Enforces them after it figures them out.
x = “the answer is ” # Decides x is string. y = 23 # Decides y is integer. print x + y # Python will complain about this.
Saturday, May 4, 13
![Page 18: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/18.jpg)
Basic Datatypes
• Integers (default for numbers)•z = 5 / 2 # Answer 2, integer division
• Floats•x = 3.456
• Strings• Can use “” or ‘’ to specify with “abc” == ‘abc’• Unmatched can occur within the string: “matt’s”• Use triple double-quotes for multi-line strings or strings that
contain both ‘ and “ inside of them: “““a‘b“c”””
Saturday, May 4, 13
![Page 19: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/19.jpg)
Whitespace
Whitespace is meaningful in Python: especially indentation and placement of newlines•Use a newline to end a line of code
Use \ when must go to next line prematurely
•No braces {} to mark blocks of code, use consistent indentation instead
• First line with less indentation is outside of the block• First line with more indentation starts a nested block
•Colons start of a new block in many constructs, e.g. function definitions, then clauses
Saturday, May 4, 13
![Page 20: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/20.jpg)
Assignment
•You can assign to multiple names at the same time >>> x, y = 2, 3
>>> x
2>>> y
3
This makes it easy to swap values>>> x, y = y, x
•Assignments can be chained>>> a = b = x = 2
Saturday, May 4, 13
![Page 21: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/21.jpg)
A Python Code Sample x = 34 - 23 # A comment.
y = “Hello” # Another one. z = 3.45
if z == 3.45 or y == “Hello”:
x = x + 1
y = y + “ World” # String concat.
print x
print y
Saturday, May 4, 13
![Page 22: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/22.jpg)
Side by Side with Java
Java (C#) Pythonpublic class Employee{ private String myEmployeeName; private int myTaxDeductions = 1; private String myMaritalStatus = "single";
public Employee(String EmployeName) { this(EmployeName, 1); }
public Employee(String EmployeName, int taxDeductions) { this(EmployeName, taxDeductions, "single"); } public Employee(String EmployeName, int taxDeductions, String maritalStatus) { this.myEmployeeName = EmployeName; this.myTaxDeductions = taxDeductions; this.myMaritalStatus = maritalStatus; }}
class Employee():
def __init__(self,
employeeName
, taxDeductions=1
, maritalStatus="single"
):
self.employeeName = employeeName
self.taxDeductions = taxDeductions
self.maritalStatus = maritalStatus
Saturday, May 4, 13
![Page 23: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/23.jpg)
Life is Short(You Need Python)
- Bruce Eckel (Thinking in C++)
Saturday, May 4, 13
![Page 24: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/24.jpg)
Useful books:
Python for Kidshttp://oreil.ly/10boyUq
The Quick Python Book, 2nd Edhttp://amzn.to/lXKzH5
Google's Python Classhttps://developers.google.com/edu/python/
Saturday, May 4, 13
![Page 25: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/25.jpg)
Let’s start by writing text based games
Saturday, May 4, 13
![Page 26: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/26.jpg)
Saturday, May 4, 13
![Page 27: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/27.jpg)
A Skeleton• Let’s start with the most basic pygame program
template.pytemplate.py123456789
1011121314151617
from pygame import *from pygame.sprite import *from random import *
init()
screen = display.set_mode((640, 480))display.set_caption('Window name!')
while True: e = event.poll() if e.type == QUIT: quit() break
screen.fill(Color("white")) display.update()
Saturday, May 4, 13
![Page 28: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/28.jpg)
Surface• Most of the game elements you see are represented as Surface
• display.set_mode((x, y)) creates your canvas – it returns a Surface object
Useful surface methods:• fill("color") fills the surface object it's been called from
• blit(surface, area) paints the source surface onto the rectangle bounded by the area tuple–Example: screen.blit(ball, (50,50))
Saturday, May 4, 13
![Page 29: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/29.jpg)
Rect• Objects that store rectangular coordinates
• Call .get_rect()on a surface to get its bounding box
Rectangle methods/variables:• .center holds the object's center as a tuple
• .colliderect(target) returns True if the parameter overlaps with the object
• .collidepoint(target) returns True if the target point overlaps with the object
Saturday, May 4, 13
![Page 30: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/30.jpg)
Media• Loading an image:–img = image.load("file.gif").convert()
• Getting a bounding rectangle:–img_rect = img.get_rect()
• Loading and playing a sound file:–mixer.Sound("file.wav").play()
Saturday, May 4, 13
![Page 31: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/31.jpg)
Sprite• Simple base class visible game objects inherit from.
Ball.pyBall.py123456789
1011
from pygame import *from pygame.sprite import *
class Ball(Sprite): def __init__(self): Sprite.__init__(self) self.image = image.load("ball.png").convert() self.rect = self.image.get_rect()
def update(self): self.rect.center = mouse.get_pos()
Saturday, May 4, 13
![Page 32: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/32.jpg)
Using Sprites• They're just objects: initialize them–ball = Ball()
• Create a group of sprites in main–sprites = RenderPlain(sprite1, sprite2)
• Groups know how to draw and update–sprites.update()
–sprites.draw(surface)
Saturday, May 4, 13
![Page 33: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/33.jpg)
Events• User input such as clicking, moving mouse or key presses
• Add more branches to test the result of event.poll()
• Events to test for:–QUIT–MOUSEBUTTONDOWN–JOYBUTTONDOWN
• Testing for the letter ‘d’ being pressed using KEYDOWNif e.type == KEYDOWN:
if e.key == K_d: …
Saturday, May 4, 13
![Page 34: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/34.jpg)
Adding Text• f = font.Font(font, size) goes before your game loop
– Example: f = font.Font(None, 25) – Usually, None is a good enough font!
• text = Font.render(text, antialias, color)– Example: text = f.render("Hello!", True, Color("green"))
– Returns a surface
• Must be blit, just like any other surface– Example: screen.blit(t, (320, 0))
Saturday, May 4, 13
![Page 35: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/35.jpg)
Let’s dissect a game!
Saturday, May 4, 13
![Page 36: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/36.jpg)
Join PythonKC -> http://www.meetup.com/pythonkc/
Saturday, May 4, 13
![Page 37: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/37.jpg)
Juan GomezCo-Founder of PyhtonKC
Twitter: @_juandg
Thanks!
Saturday, May 4, 13
![Page 38: Teach your kids how to program with Python and the Raspberry Pi](https://reader033.vdocuments.net/reader033/viewer/2022052618/554fa0fcb4c90586258b4998/html5/thumbnails/38.jpg)
PLATINUM)SPONSORS)
GOLD)SPONSORS)
SILVER)SPONSORS)
Saturday, May 4, 13