Posts by Al.

Free Physical Books with Amazon Review

UPDATE: I've had to stop this offer since I've been flooded with emails! Thank you so much!

I'm mailing out a free copy of my books to anyone who writes an Amazon review of my books (available online for free at inventwithpython.com). This is no-strings-attached: even writing a 1-star review will get you a book. Due to shipping costs, this offer is only available to US residents for now.

(UPDATE: And to clarify, the book would be sent out after your review is posted. The ebook versions are freely available and identical in content to the physical books. Due to the low response rate when I've previously sent out review copies first, I can't really afford to send out books before the reviews are written. Sorry.)

The process is:

  1. Have an Amazon account, or someone who can post your review on their Amazon account.
  2. Send me an email to [email protected] to tell me you intend to write a review and of which books.
  3. Read the book! Free ebook versions can be read at inventwithpython.com. Take your time, there's no deadline to read the books, but within a month or two would be nice. Or, if you've already read the books, just go ahead and...
  4. Write a review on Amazon at least 200 words. Give it an honest rating.
  5. Email me again with a link to the review and your mailing address.
  6. I'll mail you the physical book, no matter how high or low your rating and review were.

I can send out a max of one copy of each book you write a review for, but you can write reviews for as many of the books as you want. Thank you very much in advance!

Invent Your Own Computer Games with Python, 2nd Edition [PDF]

Making Games with Python & Pygame [PDF]

Hacking Secret Ciphers with Python [PDF]

“Let’s Create Software” Tutorial: Bus Routes Overlaid on Google Maps

The "Let's Create Software" series covers the creation of small, complete software projects from start to finish. The entire process and code is explained to give beginner and intermediate programmers an idea of what it is like to not just program, but create an entire piece of software from scratch.

There's a large PDF system map of all the Muni bus routes in San Francisco (as well as maps of the individual Muni routes). If I need to find directions, Google Maps' mass transit instructions are fine. But sometimes I'd like to just browse all the routes throughout the city. However the large PDF is slow to render on my laptop (moreso on my phone). Scrolling the PDF also results in a lengthy wait for redrawing.

So I had the idea to use Google Maps' API to draw out a map of all the Muni routes. This would have the benefit of the system map PDF but also be easier to load and navigate. I have no experience using the Google Maps API and am vaguely aware that there's public data about Muni bus routes, but I'll learn along the way.

The completed SF Muni Google Map.

To follow along with this tutorial, it helps to have a bit of Python or JavaScript knowledge, or know a little bit about programming. I've tried to keep this as straightforward as possible, even for non-programmers to roughly follow.

This project will involve writing a few "throw-away" scripts to parse and extract data, and my preferred language is Python. While I do all the scripts in Python 3 (note: Python 3 has some backwards incompatibilities with Python 2. My scripts might not run on Python 2), you can use whatever language you like. The final software will be done with JavaScript since it is a web page that uses the Google Maps API. (The final result is here: http://inventwithpython.com/sfmuni/

In general, by "throw-away" script I mean a script that is written in order to be run once to perform a task. Software such as Firefox or Angry Birds I want to keep around to run again and again, but a throw-away script I just need to automate a certain task. I'll still keep this script; I won't actually delete this script once I've run it, but I most likely won't ever need it again.

A note to beginning programmers who might feel like this project might be too advanced for them: even though I have years of experience programming, I still made many mistakes and went down a few blind alleys while putting this code together. Although it looks like I just sat down and typed out this code, just remember that this tutorial is the polished outcome of quite a bit of tenacious work. Don't be discouraged or intimidated, just keep at it!

More... »

Why Is HTML Not A Programming Language?

HTML is not a programming language. While some may point out that HTML is a "markup language", this doesn't clarify how that is different from a programming language. I'd like to provide a simple reasoning that is easy to understand by the layperson. (This isn't meant to be a thorough argument, but rather a brief explanation that goes just a bit more in detail than "HTML isn't a programming language but instead a markup language.")

Here is a point I'd like to make: HTML is no more a programming language than Microsoft Word is. With Word (or any other word processor software), you can write out text and format it with different fonts, sizes, and colors. Images and tables and bullet points can be added as well. This is all done through the word processor's graphical user interface.

An HTML file is similar, except it does not have a GUI. Instead, it is entirely written out in plaintext, so all the formatting instructions must be typed out as HTML tags (for example, <b> or <center>.) This "language" of tags is how we can format the appearance of a webpage.

Programming languages, on the other hand, can process data and make decisions. You can store data such as text strings and integers, and then manipulate these values to perform calculations. For example, doing some math or sorting text alphabetically are kinds of data processing that you cannot do in HTML.

Programming languages can also make decisions about what instructions they should execute. Depending on if a certain condition is true or false, a set of instructions may be executed or skipped by a program. Here is a Python example of such code:

if password == 'rosebud':
    print('Access granted.')
else:
    print('Access denied.')

Programming languages also have ways of executing instructions over and over again in loops. Loops, if-else statements, and other such instructions are called flow control statements. All programming languages have these flow control statements, but HTML (and Microsoft Word) do not.

JavaScript is a programming language. It has all these features of flow control and data processing. While JavaScript is used in many web pages, it is a distinctly separate thing from HTML. You can write HTML without JavaScript, and you can write JavaScript code without HTML.

Because HTML lacks these features, it cannot be called a programming language. One does not "program in HTML" nor could one "write HTML code". You should never list HTML on your resume under "programming languages".

The difference is not just a snobbish opinion of elitist software developers. While this post is by no means the definitive and complete reasoning of what a "programming language" is, it is good to know the general difference between a programming language and HTML.

An Interview With Al Sweigart, Author of “Hacking Secret Ciphers with Python”

I was interviewed on the aGupieWare blog about my latest book, "Hacking Secret Ciphers with Python".

“Invent with Scratch” Screencast Series

I've created a series of video screencast tutorials for Scratch. Scratch is a block-based programming environment from MIT. It is a programming education toy that is made for kids between the ages of 8 and 16. The screencasts can be found at:

"Invent with Scratch" Screencast at http://inventwithscratch.com

Scratch itself is hosted at http://scratch.mit.edu

I highly recommend Scratch as a teaching tool for younger kids who may not be ready for Python programming or are frustrated by their slow typing. Scratch is a drag-and-drop environment with code "blocks" that snap together.

Downloading Imgur Posts Linked From Reddit with Python

UPDATE - I have updated this article to use BeautifulSoup to parse the HTML rather than regular expressions. This makes it much easier.

Reddit is a popular site that allows users to post and vote on interesting web links. It is divided into several topical subreddits. Many Redditors use Imgur to host their images (and I highly recommend it: Imgur is free and easy to use). This tutorial tells you how to write a Python script that can scan Reddit and download images from Imgur submissions you find. This tutorial is for beginner-level programmers with a small amount of Python experience.

You can download the source code directly or view the GitHub repo.

This post will cover:

  • Basic web scraping concepts.
  • Command line options.
  • Accessing Reddit with the PRAW module.
  • Using regular expressions to find text patterns in a web page.
  • Downloading files with the Requests module.
  • Detecting which files are on our computer with the os and glob modules.
  • Opening files using Python's with statement.

More... »