Webucator's Blog


Python Generators

Awhile ago, I traded emails with Christian Schramm about an article he had written called A Gentle Introduction to Generators in Python, which had been published on Tech.pro. Unfortunately, that site has since been taken down, at least temporarily: Continue Reading »


Python Color Constants Module

For Pygame and other graphics work, it’s helpful to have color constants that hold the color RGB values. I couldn’t find anything like this, so I created a color_constants module that:

  1. Contains constants for 551 named colors* (e.g, as named tuples:
    Color = namedtuple('RGB','red, green, blue')
  2. Extends the Color class to include a method for getting the hex formatted color:
    class RGB(Color):
        def hex_format(self):
            return '#{:02X}{:02X}{:02X}'.format(self.red,self.green,self.blue)
  3. Stores these constants in an OrderedDict.

The colors are shown in the table below and the full code is shown below that. Continue Reading »


Installing the Windows 64-bit Version of Pygame

UPDATE: Apparently, this is no longer necessary. As of February, 2017, you can just run pip install pygame and it will install the right version of pygame for your environment. (Thanks, Chek Wei).

I’m not a game developer, but I’ve been playing with Pygame lately and have been impressed. I plan to use it in one of our Python courses to teach object-oriented programming. I use 64-bit Windows 8, so I have the 64-bit version of Python 3.4. Most of our students are unlikely to be game developers, but because games have obvious visible objects, they provide a nice framework for teaching OOP.

Unfortunately, Pygame doesn’t have an official 64-bit installer, so they recommend you use the 32-bit version of Python. So, I installed the 32-bit version of Python alongside the 64-bit version I already had, but that was a pain as I had to constantly make sure I was running the right one. It also made the 32-bit version the default.

Luckily, Christoph Gohlke of the University of California, Irvine has made a bunch of 64-bit binaries available for Python extension packages, including one for pygame. Continue Reading »


Python: pow(x, y, z) less efficient than x**y % z

The Python documentation on pow() states that pow(x,y,z) is computed more efficiently than pow(x,y) % z, but our tests don’t show that to be the case. In fact, we found x**y % z to be the most efficient way to complete the operation. Note that this is academic as all the methods we tested for raising one number to the power of another and then doing a modulus operation are lightning fast. You should use whichever method suits your coding style the most, but for those who are curious, here’s how we tested it: Continue Reading »


Python: isdigit() vs. isdecimal()

The difference between str.isdigit() and str.isdecimal() in Python is, for most of us, academic and can be used interchangeably. According to the Python documentation, the decimal category “includes digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.” Continue Reading »