- 1 Introduction
- 2 Python for Scientific Computing
- 3 Python for Games
- 4 Installation Walkthrough
- 5 PyCharm
- 6 Misc
- 7 IDLE
- 8 IronPython
- 9 Examples
- 10 File
- 11 Python Wackiness
- 12 Running Processes within Python
- 13 32 bit or 64 bit?
- 14 Some Oddities
- 15 Packages
- 16 Twisted
- 17 Compiling Python
- Don't use Dive Into Python to teach Python. Dive Into Python must die. Instead, use Mark Lutz's books. If you're a complete beginner, try Learn Python The Hard Way.
- Python Programming Language Official Website, with documentation for Python 3.1 and Python 2.7
- What does an asterisk in a function argument (parameter) do? See arbitrary argument lists.
- See set.
Python for Scientific Computing
- Enthought Python Distribution provides scientists with a comprehensive set of tools to perform rigorous data analysis and visualization.
- IPython for Interactive Computing. A video tutorial on IPython by Fernando Pérez.
- Python Tools for Visual Studio
Python for Games
- Invent Your Own Computer Games with Python, a free book by Albert Sweigart.
- Pygame, for prototyping games in Python. The only published book for this seems to be Beginning Game Development with Python and Pygame by Will McGugan.
- Surprisingly, Pygame doesn't have built-in support for vectors. Instead, you can use the 2D Vector class from the Pygame Wiki.
- piman Sprite Tutorial
- Instructions for installing Pygame on OS X.
While not a requirement, cURL (Windows download) simplifies the downloading processing by allowing you to use the command line. Installing the version without SSL is easiest; otherwise OpenSSL (Windows download) is required.
- First, a Python public service announcement.
- Install the appropriate version of Python (usually Python 2.7), since quite a few important frameworks still do not support Python 3 (namely, Twisted).
- Previously, you could install setuptools. This would give you
easy_install. The preferred way seems to be distribute now. Note that
easy_toolsis installed under the Python
- Next, install virtualenv and pip. By the way, installing
virtualenvis supported with PyCharm! Also,
pipcan generally be used to install things, even if the instructions say to use
easy_install. For example:
pip install zope.interface.
- This is all still very confusing, so see the The Hitchhiker’s Guide to Packaging.
curl -O http://python-distribute.org/distribute_setup.py python distribute_setup.py
- JetBrains PyCharm has become a favorite editor of mine for working with Python.
- PyCharm Resources, which includes a quick reference guide.
- Twisted, an event-driven networking engine written in Python.
netcatis handy for testing (Windows download). A good list of examples to get started. Documentation. Caveat: currently does not support Python 3.
- Python for Windows extensions, required by many frameworks when running Python under Windows.
- Code Like a Pythonista: Idiomatic Python
- Older documentation for Python 2.4.
- UltraEdit Python word file for 2.6 and 3.0. Install this to:
- Dive Into Python 3 by Mark Pilgrim.
- Google Python Style Guide
- ED-SIG: Python in Education
- Python Decorator Library, containing functions like memoize.
- Time Complexity of CPython
- Why Python is a great language for teaching beginners in introductory programming classes
- You can write compilers with it! See PLY (Python Lex-Yacc).
The IDLE shell has the ability to recall the command history. Use
ALT + p for the previous command and
ALT + n for the next command. Annoyingly, up and down do not work because these keys are reserved for cursor motion.
- IronPython if using Visual Studio, and now supporting 2010 and .NET 4.0. Use this document to integrate it with Visual Studio. You will need to run IronPythonTools.vsix.
Unfortunately, IronPython doesn't even seem to be able run "Hello World" at this time, so give up on it for now; this system is garbage. Here's the full error log on IronPython. You can run the program through the IronPython Interactive (
ALT + SHIFT + F5) though, which is a bit odd.
- Python format string syntax.
def fact(n): if n == 0: return 1 else: return n * fact(n - 1)
Iterating over Standard Input
This is a common technique in algorithm competitions:
for line in iter(input, "-"): print line
The above code will keep processing lines until a dash (-) is received.
In algorithm competitions, perform is a huge factor, as it can make the difference between meeting and exceeding the problem time limit. The
timeit module can help with this, though its usage is not at all intuitive:
import timeit t = timeit.Timer("f(3)", "from __main__ import f") t.timeit()
The use of
from __main__ import f is important! If no argument is passed to
timeit, then the number of iterations is a million.
Python itself has some oddities when you start delving under the hood. Decorators? repr? Python functions with attributes?
- The different import mechanisms get confusing to me.
Running Processes within Python
- No one knows how to use the
-c cmdargument in Python. Because it isn't nearly as nice as
perl -e, so you'll have to do a bunch of escaping. Example of a simple print:
python -c "print \"hello\"".
- In Windows, you may want to run a process in the background and allow Python to exit without waiting for your process to finish. To do this, you should see this (also couples as a nice injection attack here):
DETACHED_PROCESS = 0x00000008 line = sys.stdin.readline() pid = subprocess.Popen([line], shell=True, creationflags=DETACHED_PROCESS).pid
You can also use the Windows-specific startfile, which is far less arcane.
32 bit or 64 bit?
- I couldn't find an easy way to tell what version of Python (32-bit or 64-bit) that I was actually running until I found this thread.
takes no keyword arguments. This might actually be a function call to a
pydfile, rather than a regular Python script.
- Python provides
heapq, without instructions on how to use it. The mystery of how
__cmp__is supposed to work.
Some commonly used Python packages on Windows:
Twisted is an event-driven networking engine written in Python and licensed under the open source.
pip has not been configured, then Twisted can still be installed under Windows:
- Download the 32-bit version of Twisted (Twisted for Python 2.7).
- You will also need to install setuptools if
easy_installdoes not exist.
easy_installshould be found under
c:\Python27\Scripts\easy_install.exe. (I'm curious as to how I obtained
easy_installin the first place, since I don't have
setuptools. Aha -- it's because I've installed distribute).
- Twisted requires the use of zope.interface, which can be downloaded as an
- You can install the
configure; make. But when installing to the home directory:
make altinstall prefix=/home/tbarik exec_prefix=/home/barik.
./configure CFLAGS="-I/home/tbarik/include" LDFLAGS="-L/home/tbarik/lib"