Introduction to Python

From ThinkHausWiki
Jump to: navigation, search

This class has ended. If you're interested in it let us know - with enough people, we could put it on again.

Learn how to program in Python! Peter and Randy will bring you through the basics of the language over five sessions. Announcements, questions, and collaboration can happen on the course mailing list.

The cost of the course will be pay-what-you-can, with a recommended $20 per session ($100 for the complete course). Come for as many sessions as you like, pay whatever you think they're worth.

As you work through the course follow along with the additional support material.




Before classes start, each participant should install Python 3.3 on their laptop, along with a decent editor, like Notepad++ (install). For those who don't do so, we'll have portable versions (WinPython and Notepad++ Portable) so they can get running quickly.

In class:

Session 1

  • Mention a little about the Python language and interpreter
    • Language designed for simplicity and readability; try to code simply
    • It runs source files directly, no compilation step
    • But actually it does compile; you'll see a folder, __pycache__, with compiled bytecode, but you can mostly ignore them
    • Can also run the interpreter interactively for quick feedback
  • First program: Hello World
    • Makes sure everyone has Python running properly
    • Demonstrates print function (basic output)
    • Quick mention of strings (more detail later)
  • Variables
    • Holds any kind of value
    • Can vary (hence the name)
    • Any expression can use variables, and can be assigned to other variables
  • Basic math
    • Example program: set variables, calculate something from them (eg. quadratic formula), change variables, recalculate
    • May have to make brief mention of imports so as to access the math package

Session 2

Additional materials

  • Strings
    • Concatenation and how other types can not be mixed in
    • format method, and how it's often better than concatenation
    • Generating format strings from format strings (more to show its capabilities, not to give detailed instruction on that)
    • find, replace, indexing, slicing
  • input function (basic input)
    • Example program: Hello [name]
    • Rework previous math program to take user input instead of hardcoded variables (will have to introduce float function)
  • Flow control (for, while, if)
    • rework previous problems to handle specific names/bad input
    • Indentation delimits blocks (4 spaces standard)
  • Exception handling
    • simpler in some cases than basic flow control ("easier to ask forgiveness than permission")
    • simplify some of the handling done in the preceding section
  • Container types: list, tuple, dict, set
    • Idea of an iterable
    • and the use of them as iterables in for loops
    • Comprehensions

Session 3

  • Functions
    • Using builtin functions
    • Defining our own using def (maybe lambda)
    • Default arguments, keyword arguments, *args, **kwargs
    • Recursion (mention, simple examples)
    • Functions can perform actions and return values (not the same as math functions)
  • File IO
    • Starting with working directory (leads into os.path)
    • open function (manual close, no with)
    • Log data output from previous programs (could use shell redirect, but limited)
    • Can deal in binary data
    • Input from permanent source (don't always have to prompt user)
    • Simulate something in XY, generate a csv file
    • Cover output, then input
    • readlines, use file object as an iterable
    • read to a point, read whole file, seek position
    • Files can be thought of as strings, but they aren't (has a position)
  • Packages
    • Importing builtin packages
    • Many builtins, find in docs
    • Highlight cool packages
      • math
      • random
      • os (eg os.path)
      • csv
      • sys (eg argv, exit, platform)
      • http.server (it's cool!)
      • urllib
      • datetime
    • Third party packages
      • Need an extra install
      • numpy / scipy (numeric / scientific)
      • pygame
      • GUI libraries (GTK, Qt, Kivy)
      • requests
  • Weather scraper group project
    • Break down scraper into your own modules
    • Refactor for reuse, cleanup and packages
    • Possibly take-home project

Session 4

  • Classes and object oriented programming
    • Contains data about an object and special functions (called methods) to use that data
    • Wraps up collections of functions that all operate on same data
      • Try to build off of earlier examples that had multitudinous functions (eg. matrix operations)
    • Special methods can override built-in operators
    • Everything is an object! They all have data and methods and we can mimic/override all of it.
    • Inheritance allows you to extend other types (eg. inheriting from list for a Population type)
      • super() can access superclass methods inside other methods
    • Very, very, very brief mention of multiple inheritance, followed by "don't do it please"
      • Except maybe mixins
    • Manipulating classes (as opposed to manipulating instances) (classes are objects too!)
    • Class-level definitions versus instance values
  • Introspection
    • Programatically inspect objects
    • dir, hasattr, getattr
  • Decorators

Session 5

  • Errata
  • Larger practice programs
  • Buffer zone


Only in the conceptual stage so far. Let's see what people want after the Beginner section.

Intermediate topics:

  • Duck typing
  • Introspection - dir, hasattr, getattr
  • List comprehension
  • Generator expression
  • Decorators
  • ABCs
  • with
  • yield
  • Best practices - docstrings, PEP 8, testing

Example programs

  • Compound interest calculator
  • Number guessing game
  • Long division algorithm
  • Fibonacci sequence
  • Factorial
  • Long division algorithm
  • Checking if a string is a palindrome
  • Making a number a palindrome
  • Madlibs
  • RLE string compressor
  • 3n+1 problem
  • Count the occurance of each letter in a string (dicts)
  • Game of blackjack
  • Game of nim
  • Lemonade
  • Drunken walk (and view with gnuplot)
  • Simulation - ?
  • Document redactor
  • Web scraping (eg. weather data to excel file)
  • Prime number testing
    • Number factoring
  • Auto twitter thing
  • Magic eight ball
  • Randy is great program
Personal tools