Introduction to Python
Tutorial & Slideshow!

 

General Python Scripts

 

Note: Windows users will have to install at least Python 3.1.3 before any of these scripts will run. Python comes pre-installed on most Linux distributions, but you may have to update to the latest-and-greatest version if it's been awhile since your last OS upgrade.

 

Introduction to Python

This first download is a collection of very simple scripts which are aimed at teaching the very basics of scripting with Python. If you know absolutely nothing about Python, this is where you should start.

Relevant Keywords: import, def, if, elif, else, >=, input, not, for, in, range, len, while, __name__, sleep, raw_input, print, array, array.array, tostring, os, os.mkdir, os.listdir, os.path.isfile, os.path.isdir, os.remove, os.rmdir, os.environ.get, os.path.walk, os.curdir, string, string.replace, and string.split.

Reading Files With fileinput

This Python script demonstrates how to use the fileinput module to read each line of a given file.

Relevant Keywords: fileinput, and fileinput.input.

Creating a dictionary object

This Python script demonstrates how to create and access a dictionary object.

Relevant Keywords: del, keys, and has_key.

Zipping and Unzipping Files With zipfile

This collection of Python scripts demonstrate how to use the zipfile module to compress, or uncompress files, or examine the contents of a zipped archive.

Relevant Keywords: zipfile, zipfile.ZipFile, namelist, infolist, filename, date_time, file_size, compress_type, read, repr, len, time, time.localtime, writestr, close, os.path.splitext, os.listdir, and os.path.basename.

Opening Sockets

This collection of Python scripts demonstrate how to create and manage sockets using the socket module. One script creates a client that sends out a UDP packet every 2 seconds while the other script creates a socket, which will act as a simple dedicated server by listening to port 5000.

Relevant Keywords: from, socket, import, gethostbyname, AF_INET, SOCK_DGRAM, localhost, bind, recvfrom, time, sleep, gmtime, strftime, 127.0.0.1, strftime, and sendto.

Spawning Threads

This collection of Python scripts demonstrate how to use the threading module to create a thread that sleeps for some time, prints a message, and then exits.

Relevant Keywords: threading, sleep, def, __init__, self, and start.

Introduction to Creating GUIs With Tkinter

This collection of Python scripts demonstrate how to use the Tkinter module to create Graphical User Interfaces for your Python scripts.

Relevant Keywords: Tkinter, Tk, title. geometry, root, mainloop, Frame, grid, StringVar, text, variable, value, command, row, column, columnspan, sticky, Text, Toplevel, self, width, height, wrap, get, insert, delete, END, Label, Radiobutton, underline, menu, add_cascade, add_command, resizable, destroy, root. root.quit, tkMessageBox.showinfo. message, frame.protocol, and WM_DELETE_WINDOW.

Search & Replace

This collection of Python scripts demonstrate how to perform a search-and-replace on a plain text file.

Relevant Keywords: sys, sys.argv, sys.stdout, open, write, close, os.path.isfile, os. os.remove, and os.rename.

Creating 2D Games with Python & Pygame

 

Note: In addition to installing Python 3.1.3, users wishing to running Python scripts that use the Pygame module will need to download and install Pygame 1.9.1. Just make sure you get the installer that matches up with Python 3.1.3. I downloaded the one titled, "pygame-1.9.1.win32-py3.1.msi".

 

Loading an Image

This Python/Pygame script demonstrates how to load and display an image file.

Relevant Keywords: pygame.image.load, get_rect, bottom, left, blit, and flip.

Using the Image's Rect data

This Python/Pygame script demonstrates how to move or place a simple image by accessing its Rect (rectangle) data.

Relevant Keywords: pygame.image.load, get_rect, left, right, top bottom, blit, and flip.

Using Fonts

This Python/Pygame script demonstrates how to create and render on-screen text using fonts.

Relevant Keywords: pygame.font.init, pygame.font.Font, render, None, and blit.

Polling the keyboard

This Python/Pygame script demonstrates how to move an image using keyboard input.

Relevant Keywords: pygame.event.pump, pygame.key.get_pressed, pygame.event.peek, QUIT, K_ESCAPE, K_LEFT, K_RIGHT, K_UP, and K_DOWN.

Deriving a custom sprite from Pygame 's Sprite class

This Python / Pygame script demonstrates how to create a  custom sprite class for a 2D game by deriving a new class from Pygame 's built-in Sprite class.

Relevant Keywords: pygame.sprite.Sprite, self, __init__, move_ip, pygame.transform.flip, pygame.time.Clock, clock.tick, pygame.sprite.RenderPlain, pygame.image.load, convert, get_at, set_colorkey, left, right, and pygame.sprite.Sprite.__init__.

Simple Star Wars Game

This Python/Pygame script demonstrates how to create a simple Space Invaders clone with a Star Wars theme.

Relevant Keywords: random.seed, pygame.mixer, pygame.mixer.get_init, pygame.mixer.Sound(, pygame.sprite.Sprite, self, __init__, move_ip, pygame.transform.flip, pygame.time.Clock, clock.tick,  pygame.sprite.RenderClear, pygame.image.load, convert, get_at, set_colorkey, eft, right, top bottom, centerx, centery, add, random.randint, and pygame.sprite.Sprite.__init__.

Extending & Embedding Python with C/C++

 

Note: You obviously need Python 2.5 to recompile this samples, but the samples that mention Boost.Python, also require the Boost.Python v2 library, which is part of Boost 1.33.1, to compile. These samples come with a "readme.txt" file which will explain further how to set up Boost.Python.

 

Introduction to Embedding Python Into C/C++ Applications

This download contains four separate samples which help to introduce the basics of how to embed Python into C/C++ applications.

Relevant Keywords: Py_Initialize, Py_Finalize, PyImport_ImportModule, PyRun_SimpleString, PyModule_GetDict, PyRun_String, PyObject, PyMethodDef, PyArg_Parse, PyObject_SetAttrString, PyObject_GetAttrString, PyDict_SetItemString, PyEval_GetBuiltins, PyDict_New, PyCallable_Check, PyErr_SetString, Py_INCREF, Py_DECREF, Py_XINCREF, Py_XDECREF, PyImport_AddModule, Py_InitModule, PyModule_GetDict, Py_BuildValue, file_input, and __builtins__.

Integrating C/C++ and Python together through Extending & Embedding

This sample demonstrates how to integrate C/C++ and Python together by both extending and embedding Python into a C/C++ application. This is done by using the C API of Python to set up special functions which will allow C/C++ and Python to communicate back and forth.

Relevant Keywords: Py_Initialize, Py_Finalize, PyObject, PyMethodDef, PyArg_ParseTuple, PyCallable_Check, PyErr_SetString, Py_INCREF, Py_DECREF, Py_XINCREF, Py_XDECREF, PyImport_AddModule, Py_InitModule, PyModule_GetDict, Py_BuildValue, and METH_VARARGS.

Multithreading & Embedded Python

This sample is nearly identical to one above except in this particular sample, the Python script spawns a worker thread so it can run alongside the main application. This is important if you want your C++ application to run Python scripts, but you don't want it blocking on an unfinished script. This is especially true when you want that script to run continuously in the background.

Relevant Keywords: Py_InitModule, Py_Initialize, Py_Finalize, PyObject, PyMethodDef, PyArg_ParseTuple, PyCallable_Check, PyErr_SetString, Py_INCREF, Py_DECREF, Py_XINCREF, Py_XDECREF, PyImport_AddModule, Py_InitModule, PyModule_GetDict, Py_BuildValue, threading, import, threading.Thread, target, start, time, time.sleep, for, range, and METH_VARARGS.

Extending Python Using C/C++

This sample demonstrates how to use the Boost.Python library to extend Python by creating a new Python module written in C/C++. This will allow Python scripts to access and make use of C/C++ functions and classes like it would with any other Python function or class.

Relevant Keywords: BOOST_PYTHON_MODULE, def, class_, .def, and import.

Creating C++ objects from Python derived classes

This sample demonstrates how a C++ class, exposed to Python via the Boost.Python library, can be used to derive a new Python class. It then brings the concept full-circle by demonstrating how instances of that Python derived class can be created and used back in the C++ application.

Relevant Keywords: boost::noncopyable, BOOST_PYTHON_MODULE, class_, call_method, PyImport_AppendInittab, Py_Initialize, Py_Finalize, PyObject, and PyRun_String.