Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An Introduction To Python and Python on the Raspberry Pi

An Introduction To Python and Python on the Raspberry Pi

Slides from my talk at the BrightonPi / BrightonPy “Raspberry Pi Jam” night.

Jamie Matthews

November 27, 2012
Tweet

More Decks by Jamie Matthews

Other Decks in Programming

Transcript

  1. An Introduction To Python and Python on the Raspberry Pi

    Jamie Matthews Raspberry Pi Jam - “Py vs Pi” 27th November 2012
  2. Agenda About me Why Python? A brief tour Python on

    the Raspberry Pi Python for hardware hacking Discussion?
  3. A Brief History of Python Created by Guido van Rossum

    Implementation started in 1989 Python 1.0 released January 1994 Python 2.0 released October 2000 Python 3.0 released December 2008 *
  4. Readability “Programs must be written for people to read, and

    only incidentally for machines to execute” -H. Abelson and G. Sussman (in "The Structure and Interpretation of Computer Programs)
  5. Standard Library subprocess csv json sqlite3 os datetime logging smtplib

    multiprocessing urllib2 posix signal pickle zlib hashlib
  6. Plays well with others Easy to write extensions in C

    for speed Easy to communicate with other processes Jython, IronPython, PyPy Embeddable as a scripting language
  7. Scripts “programs written for a software environment that automate the

    execution of tasks which could alternatively be executed one-by-one by a human operator.” - wikipedia
  8. Scripts “programs written for a software environment that automate the

    execution of tasks which could alternatively be executed one-by-one by a human operator.” - wikipedia
  9. Running code in a file 1. Create a file with

    a .py extension 2. Put some code in it 3. At a terminal prompt, type python yourfile.py
  10. Basic types Lists: [1, 2, 4, 8, 16] Tuples: (1,

    "Hello", 5.2) Dictionaries: {"greeting": "Hello", "location": "Brighton"}
  11. Control structures location = "Brighton" if location == "Brighton": print

    "Hello, Brighton!" else: print "How unfortunate"
  12. Brackety languages function greet(location) { if (location === "Brighton") {

    console.log("Hello, Brighton"); } else { console.log("How unfortunate"); } }
  13. Python uses whitespace def greet(location): if location == "Brighton": print

    "Hello, Brighton!" else: print "How unfortunate"
  14. Whitespace is significant def greet(location): !!!!if location == "Brighton": !!!!!!!!print

    "Hello, Brighton!" !!!!else: !!!!!!!!print "How unfortunate"
  15. Whitespace is significant def greet(location): !!!!if location == "Brighton": !!!!!!!!print

    "Hello, Brighton!" !!!!else: !!!!!!!!!print "How unfortunate"
  16. Whitespace is significant def greet(location): if location == "Brighton": print

    "Hello, Brighton!" else: print "How unfortunate" (actually, this works fine)
  17. Whitespace is significant def greet(location): !!!!if location == "Brighton": !!!!!!!!print

    "Hello, Brighton!" !!!!else: !!!!print "How unfortunate" IndentationError: expected an indented block
  18. Whitespace is boring 1. Use an editor that indents code

    automatically 2. Set it to indent with four spaces 3. Get on with your life Aside over
  19. Using code from elsewhere Every Python file is a namespace.

    You can import names from other files. You can also create packages (namespaces) consisting of multiple files and import names from them.
  20. def greet(location): if location == "Brighton": print "Hello, Brighton!" else:

    print "How unfortunate" from greeter import greet greeter.py anotherfile.py
  21. def greet(location): if location == "Brighton": print "Hello, Brighton!" else:

    print "How unfortunate" from greeter import greet greeter.py anotherfile.py
  22. def greet(location): if location == "Brighton": print "Hello, Brighton!" else:

    print "How unfortunate" from greeter import greet greet("Brighton") greeter.py anotherfile.py
  23. A (tiny) real example from urllib2 import urlopen response =

    urlopen('http://jsonip.com/') print response.read() >
  24. A (tiny) real example from urllib2 import urlopen response =

    urlopen('http://jsonip.com/') print response.read() {"ip":"87.xxx.xxx.xxx","about":"/about"} >
  25. A (tiny) real example from urllib2 import urlopen import json

    response = urlopen('http://jsonip.com/') data = json.loads(response.read()) print data['ip'] >
  26. A (tiny) real example from urllib2 import urlopen import json

    response = urlopen('http://jsonip.com/') data = json.loads(response.read()) print data['ip'] >
  27. A (tiny) real example from urllib2 import urlopen import json

    response = urlopen('http://jsonip.com/') data = json.loads(response.read()) print data['ip'] >
  28. A (tiny) real example from urllib2 import urlopen import json

    response = urlopen('http://jsonip.com/') data = json.loads(response.read()) print data['ip'] 87.xxx.xxx.xxx >
  29. Things you should know about virtualenv for dependency isolation pip

    for package management sudo apt-get install python-virtualenv mkdir mynewproject && cd mynewproject virtualenv env source env/bin/activate pip install somepackage vim mypythonfile.py python mypythonfile.py (Google them)
  30. Raspberry Pi is a real computer Roughly equivalent to an

    Amazon EC2 Micro Instance (512MB version) Raspbian comes with Python 2.7.3 and 3.2.3 Web sites, web crawlers, social media robots, notification hubs...
  31. PyQuery jQuery in Python (good for scraping) from pyquery import

    PyQuery as pq print pq('http://brightonpy.org').find('h1').text() # prints 'Brighton and Hove Python User Group'
  32. Flask A tiny web framework from flask import Flask app

    = Flask(__name__) @app.route("/") def hello(): return "Hello World!" app.run()
  33. Be careful “These interfaces are not plug and play and

    require care to avoid miswiring. The pins use a 3V3 logic level and are not tolerant of 5V levels” - RPi Wiki
  34. Arduino Another tiny computer Much, much lower powered than Raspberry

    Pi Much better for physical computing No internet connection, can’t run Python £10-30 (several models)
  35. Summary Python is easy, fun and powerful. The Raspberry Pi

    lets you run Python anywhere. Imagine the possibilities.TM