Choose Your Own Pyventure/old toc page

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Purpose[edit | edit source]

This book is the curriculum book for the Twin Cities ExCo (Experimental College) class Bits and Bites: Programming First Steps

Do you think that programmers are born with keyboards in their hands? Programmers are made, not born—you too can code with the best of them. If you're interested in breaking down the barriers and mystique around programming, join us! Learn to code in a chill, non-judgmental environment.

Your facilitators, Gregg and Amanda, come from non-traditional programming backgrounds, and used to be N00bs. We have no patience for alpha geeks, macho baloney, and geek superiority.

Our big project is a web application that allows you to play a "Choose Your Own Adventure" that you write yourself! (example: http://cyoa.lind-beil.net/).

All instruction is done in the Python language, a free, open-source, cross-platform, powerful yet easy to learn language. We'll help get you going, introducing new concepts weekly. There will be hands-on assignments, lots of time for questions, and a loosely structured feel. Hacking is about liberation and democratizing power.

Prerequisites: Access to a computer where you can run or install programs. Online only is fine, but learning is better in meat space (where you'll need access to a laptop, or really strong arms to haul your desktop).

Programmers with experience are also welcome as learners or mentors.

Please let us know about any requirements around mobility, neurodiversity, or child-care needs, and we will do our best to meet them.

Why Another Python Book?[edit | edit source]

We were inspired by:

Kirrily Roberts' OSCON Presentation and Dreamwidth's Python vs. Ruby Deathmatch.

About the Authors[edit | edit source]

The Audience[edit | edit source]

Installing Python[edit | edit source]

Windows[edit | edit source]

1a. Python, from the Python website

You will want the newest 2.x series (probably 2.6.x) release, NOT a 3.x.x release.[1] Python 3 has some differences (primarily around strings) that this class doesn't address.

If you're more adventurous, feel free to try one of the installations towards the bottom of the page:

  • ActiveState ActivePython (not open source)
  • Enthought Python Distribution (a commercial distribution for scientific computing)
  • Portable Python (Python and add-on packages configured to run off a portable dice) (Recommended if you can't install python system-wide, and need to run it off a USB stick, SD card, or the like)

Those distributions have additional modules (bundles of code) we're not going to use. If you get serious with Python, installing one of these bundles can be much easier than installing pieces piecemeal. Install it using the usual windows methods.

1b. Test your Python installation.

   start > run > cmd  [OK]
  

This will open a Windows cmd window. In it, type python:

   C:\Documents and Settings\Gregg>python
   Python 2.4.3 - [some other info, perhaps]
   >>> 'hello'
   'hello'

If you see something like this, then you're good!

2a. Install A Text Editor.

Word processors, including Microsoft Word and friends, are terrible for writing code, since they conflate layout formatting and text.Simpler is better. That said, Notepad is terrible also, because it automagically [2] appends on '.txt' onto filenames, and other niceties.

Key features of a good programming editor:

  • syntax highlighting
  • fixed-width font
  • multiple tabbed interface.

A good, free (as in beer, and open-source) one that we like a lot is SciTE [1]. This program also has a "no install" version found at from SourceForge The portable versions doesn't quite have all the features of the installed version, but is quite capable. A good sign in programs is when they can exist in a form that doesn't require an installer. This implies the developers don't want to interfere with a running system, or damage anything, and make it easy to get rid of the program if you don't like it.

2b. Test your installation

Double click on SciTE, or choose it from the Programs menu, or click on the executable, in the usual Windows ways. You should get a Notepad looking workspace.

Copy in paste this in:

 # here is some python code
 1 # an int
 b = 'some string'  # string
 if 2 > 1:  print "sure looks bigger"

Then, in the menu: Language > Python. The code should change change colors, and the various parts (variables, integers, strings, comments) will be nice colors.

Mac OS X[edit | edit source]

Good news Mac users! Python comes preinstalled as part of the Mac OS. Check it out:

  1. In Finder, navigate to Applications -> Utilities -> Terminal and start up the Terminal
  2. Type python and hit enter
  3. You will see something like this:
 Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
 GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
 Type "help", "copyright", "credits" or "license" for more information. (
 >>> print "hello"
 'hello'

You can do a lot in the Python command prompt but writing more complex problems will be easier with a text editor. TextWrangler is a good text editor for Mac users. Download it from the Barebones Software Site

Both[edit | edit source]

(Optional) Install ipython

Ipython is a python package that gives a much nicer command-line environment, which includes syntax highlighting, history, and a variety of debugging tools and improvements. Download it from the Ipython site.

Related Tools[edit | edit source]

PyScripter is a free IDE (available for Windows. If you have previous programming experience - this is similar to the Borland Delphi IDE. You can download PyScripter from PyScipter project site.

Lessons[edit | edit source]

Lesson 0[edit | edit source]

print "Hello, world!"

After running this, you should see:

 "Hello, world!"

Play Around With Turtle

The Python turtle module, is a simple reimplementation of a LOGO-like language.

From your python prompt:

# import everything from the turtle module
# import:  make them available for use
# everything:  (mostly) everything (there are some exceptions)
# the turtle module:  a collection of functions (actions), constants,
#    and other useful stuff that is grouped into one 'space' (a namespace)
#    named turtle, for easy of memory, and general good sense
>>> from turtle import *  
>>> circle(80)   # this will draw a circle with a diameter of 80
>>> reset()  # reset the screen
>>> forward(10)  # make the turtle go forward 10

All the commands are listed at the Turtle reference documentation

Lesson 1[edit | edit source]

no line numbers, but can be copy and pasted

## anything from '#' is a comment, and gets ignored.  
## all my editorial comments will start with '##' -- GRL
 
## some text describng what this is
# a simple choose your own adventure
 
## 'print' prints to the screen.  
print "Welcome to MYSTERIOUS MANSION."
 
print "You are at a mysterious door.  The door is clearly marked -- 'Open Me And Die!'."  
 
## in python, strings can be single or double-quoted
print 'Do you want to open the door?'
 
## raw_input gets input from the user
## Here, we take the input, and *assign* it to a variable called 'ans'
ans = raw_input("please type 'yes' or 'no' ")
 
## conditionals
## see if the user's answer is interesting or not
if ans=="yes":
    print "That was foolish!  You are now dead."
## elif means "else-if"
elif ans == "no":
    print "That was wise!  You are alive, but thoroughly bored."
## else is a 'catch-all' for "any condition not all ready covered"
else:
    print "I don't know what to do, based on what you said, which was, |", ans, "|"
 
print "Thank you for playing!"
## anything from '#' is a comment, and gets ignored.  
## all my editorial comments will start with '##' -- GRL

## some text describng what this is
# a simple choose your own adventure

## 'print' prints to the screen.  
print "Welcome to MYSTERIOUS MANSION."

print "You are at a mysterious door.  The door is clearly marked -- 'Open Me And Die!'."

## in python, strings can be single or double-quoted
print 'Do you want to open the door?'

## raw_input gets input from the user
## Here, we take the input, and *assign* it to a variable called 'ans'
ans = raw_input("please type 'yes' or 'no' ")

## conditionals
## see if the user's answer is interesting or not
if ans=="yes":
    print "That was foolish!  You are now dead."
## elif means "else-if"
elif ans == "no":
    print "That was wise!  You are alive, but thoroughly bored."
## else is a 'catch-all' for "any condition not all ready covered"
else:
    print "I don't know what to do, based on what you said, which was, |", ans, "|"

print "Thank you for playing!"

Other Pages[edit | edit source]

Additional Resources[edit | edit source]

Exercises (under construction)[edit | edit source]

References[edit | edit source]

  1. As of 9/9/09: choose the Python 2.6.2 Windows installer (Windows binary -- does not include source)
  2. automagically: hacker. automatically, and as if by magic. As in Fantasia, this can be a mixed blessing.



Please add {{alphabetical}} only to book title pages.