Simple Python Tricks #11

Tags

, ,

Last time we looked at subclassing the Python built-in classes tuple, list, and dict with a focus on the built-in __new__ and __init__ methods (and never forget to include __str__ and/or __repr__ in your user-defined classes).

This time Simple Tricks explores many of the other built-in methods that help you create richly featured Python-aware objects. Specifically, we’ll focus on subclassing tuple to create (immutable) vector objects. A basic understanding of object-oriented programming is assumed.

Continue reading

Simple Python Tricks #10

Tags

, , ,

Last time, Simple Tricks looked at some built-in Python functions. This time, we look at three built-in Python container classes, tuple, list, and dict, with a focus on designing useful sub-classes based on them.

We’ll explore the built-in __new__ and __init__ methods in detail along with some of the other built-in methods that help you to create rich new types. [The reader is assumed to be familiar with the basics of object-oriented programming.]

Continue reading

Simple Python Tricks #9

Tags

,

For many, fall means back to school, so for this blog I thought I’d return to Simple Tricks in Python. Fall also means Halloween for many, so hopefully these tricks will be treats, even if they do involve some very basic Python.

In this post, I explore some of Python’s more interesting and useful built-in functions, such as enumerate, sorted, reversed, map, and filter.

Continue reading

The Python Turtle

Tags

, ,

Some of you may have encountered the Logo programming language or one of its many offshoots. A memorable aspect of Logo was its use of turtle graphics — a form of vector graphics similar to pen plotters. Notably, turtle graphics features a relative drawing cursor.

Standard Python includes a turtle graphics module. It doesn’t have a huge production value (there’s no easy way to save an image, for instance), but it can be fun to play with.

Continue reading

Python Tredoku Solver

Tags

, , , , ,

I’ve never been particularly interested in puzzle games. Figuring out software has filled that niche for me (plenty puzzling enough). So, I’ve never done a Sudoku puzzle. Recently I read a post about Tredoku, which is a kind of three-dimensional Sudoku.

In that post was an unsolved Tredoku puzzle. I wasn’t tempted to try to solve it myself, but I did think it might be fun to see if I could write some Python to do it.

Continue reading

A Faster Fibonacci

Tags

, , ,

The famous Fibonacci sequence starts off [1, 1, 2, 3, 5, 8, 13, 21, …] and continues forever. Each number in the series, except the first two, is the sum of the previous two numbers. For example, 3+5=8.

The canonical algorithm to calculate the series uses recursion and is elegant enough to be a common example of a recursive function. But while elegant conceptually, the algorithm is deadly computationally. In this post I’ll look at several ways to dodge the bullet.

Continue reading

Bloom Filters in Python

Tags

, , ,

I recently learned about Bloom filters (and was then able to fully understand the joke in this xkcd comic). While I don’t have a good application for them myself, I found them interesting enough to play around with a little.

Python uses them under the hood in a way that has some potential for other applications. In this post I’ll explain Bloom filter basics and go over some simple implementations.

Continue reading

Simple Python Tricks #8

Tags

, , , ,

Simple Tricks started late last year with a “project” post that may have been a misfire, though it’s possible I’ll do some other simple projects in the future (though my question is whether they’re really “simple tricks” — I’m not sure the first post in the series qualified).

Regardless, since then we’ve looked at Python comprehensions (see here and here), file handling techniques (see here and here), and function parameters (see here and here). This time we look at printing output with an emphasis on formatted output.

Continue reading

Simple Python Tricks #6

Tags

,

The last two posts in this Simple Tricks series (Tricks #4 and Tricks #5) explored the basics of file handling. The two before that (Tricks #2 and Tricks #3) explored Python list comprehensions.

This time we’ll explore something extremely basic, passing parameters to functions. Python has interesting native capabilities that give programmers options in how they deal with function parameters.

Continue reading