The document introduces Python and the author's motivation for writing a Python tutorial. It discusses that the author originally wanted to learn Python to create a computer game, and had struggled with other programming languages in the past. The author decided Python may be the right language and to help stay motivated, would recreate a childhood roleplaying game. The document provides an overview of why Python is a good programming language for both hobbyists and professionals. It also explains the author's goals for the tutorial which is to teach programming in a fun and friendly way rather than focusing on the language's history or esoteric uses.
The document introduces Python and the author's motivation for writing a Python tutorial. It discusses that the author originally wanted to learn Python to create a computer game, and had struggled with other programming languages in the past. The author decided Python may be the right language and to help stay motivated, would recreate a childhood roleplaying game. The document provides an overview of why Python is a good programming language for both hobbyists and professionals. It also explains the author's goals for the tutorial which is to teach programming in a fun and friendly way rather than focusing on the language's history or esoteric uses.
I originally wanted to learn Python because I wanted to make a com-
puter game. I had taken several programming classes in college (C, C++, and Java) but nothing really serious. I’m not a Computer Sci- ence major and I don’t program on a professional level. I didn’t really like the low-level work involved with C/C++. Things like pointers, memory management, and other concepts were difficult for me to grasp, much less effectively use. Java, as my first pro- gramming class in school, didn’t make any sense. I had never used an object-oriented language before and object-oriented programming (OOP) concepts gave me fits. It probably didn’t help that my Java class wasn’t actually real Java; it was actually Microsoft’s “custom” version: J++. So not only was I learning a language that had little practical use (J++ added and cut many features found in real Java), but the programs didn’t work correctly. Ultimately the class was can- celed near the end of the semester and everyone received full credit. These problems, and issues learning other programming languages, left a bad taste in my mouth for programming. I never thought I learned a language well enough to feel comfortable using it, much less actually enjoy programming. But then I heard about Python on a computer forum, and noticed several other mentions of the language at other sites around the Internet. People were talking about how great the language was for personal projects and how versatile it is. I decided to give programming one more try and see if Python was the
9 CHAPTER 1. INTRODUCTION 10
language for me.
To give me more incentive to learn the language, I decided to recre- ate a role playing game from my childhood as a computer game. Not only would I have a reason to learn the language but, hopefully, I would have something useful that I could give to others for their enjoyment.
1.1 Why Python?
Python is regarded as being a great hobbyist language, yet it is also an extremely powerful language. It has bindings for C/C++ and Java so it can be used to tie large projects together or for rapid prototyping. It has a built-in GUI (graphical user interface) library via Tkinter, which lets the programmer make simple graphical interfaces with little effort. However, other, more powerful and complete GUI builders are available, such as Qt and GTK+. IronPython, a Python version for Windows using the .NET framework, is also available for those using Microsoft’s Visual Studio products. Python can also be used in a real- time interpreter for testing code snippets before adding them into a normal "executable". Python is classified as a scripting language. Generally speaking, this just means that it’s not compiled to create the machine-readable code and that the code is “tied-into” another program as a control routine. Compiled languages, such as C++, require the programmer to run the source code through a compiler before the software is can be used by a computer. Depending on the program’s size, the compilation process can take minutes to hours. Using Python as a control routine means Python can act as a “glue” between different programs. For example, Python is often used as the scripting language for video games; while the heavy-duty work is performed by pre-compiled modules, Python can act in a call/re- sponse fashion, such as taking controller input and passing it to the appropriate module. Python is also considered a high-level language, meaning it takes care of a lot of the grunt work involved in programming. For example, Python has a built-in garbage collector so you, as a programmer, don’t really need to worry about memory management and memory leaks, a common occurrence when using older languages such as C. CHAPTER 1. INTRODUCTION 11
The main emphasis of Python is readable code and enhancing pro-
grammer productivity. This is accomplished by enforcing a strict way of structuring the code to ensure the reader can follow the logic flow and by having an “everything’s included” mentality; the programmer doesn’t have to worry about including a lot of different libraries or other source code to make his program work. One of the main arguments against Python is the use of whitespace. As shown in Chapter 3, many other languages require the programmer to use brackets, typically curly braces, i.e. “{}”, to identify different blocks of code. With Python, these code blocks are identified by dif- ferent amounts of indentation. People who have spent a lot of time with “traditional” languages feel the lack of brackets is a bad thing, while others prefer the white space. Ultimately, it comes down to personal preference. I happen to like the lack of brackets because it’s one less thing I have to troubleshoot when there is a coding problem. Imagine that one missing bracket in several dozen to hundreds lines of code is the reason your program won’t work. Now imagine having to go through your code line by line to find the missing bracket. (Yes, programming environments can help but it’s still one extra thing to consider).
1.2 Why Another Tutorial?
Even though there are several great tutorials at the Python web site (http://www.python.org), in addition to many books, my emphasis will be on the practical features of the language, i.e. I won’t go into the history of the language or the esoteric ways it can be used. Though it will help if you have programmed before, or at least can understand programming logic and program flow, I will try to make sure that things start out slow so you don’t get confused. The main purpose of this book is to teach people how to program; Python just happens to be the language I have chosen to use. As mentioned above, it is a very friendly language which lets you learn how to program without getting in your way. Most people, when they decide to learn programming, want to jump into C, C++, or Java. However, these languages have little “gotchas” that can make learning difficult and dissuade people from continuing with programming. My CHAPTER 1. INTRODUCTION 12
goal is to present programming in a fun, friendly manner so you will
have the desire to learn more.
1.3 Getting Python
As of this revision, Python 3.x has been out for several years. Most of my experience is with Python 2.4 and 2.5, though much of my knowledge was gained from reading books written for version 2.2. As you can see, it doesn’t necessarily mean your knowledge is obsolete when a new version comes out. Usually the newer versions simply add new features, often features that a beginner won’t have a need for. Python 3.x breaks compatibility with programs written in 2.x ver- sions. However, much of the knowledge you gain from learning a 2.x version will still carry over. It just means you have to be aware of the changes to the language when you start using version 3.0. Plus, the install base of 2.x is quite large and won’t be going away for quite some time. Due to the fact that most Linux distributions (and Mac OS X) still have older Python versions installed by default (some as old as v2.4), many of the code examples in this book are written for Python 2.x. Special note is made of significant changes between 2.x and 3.x in the chapter about 19, but learning either version won’t harm you. You can download Python from the Python web site (for Windows) or it may already be installed on your system if you’re using a Mac, Linux, or *BSD. However, the Unix-like operating systems, including OS X, may not have the latest version so you may wish to upgrade, at least to version 2.6. Version 2.6 is a modification of 2.5 that allows use of both 2.x code and certain 3.0 functions. Essentially it lets you code in “legacy” style while still being able to use the latest features as desired, and testing which legacy features will be broken when moving to 3.0. For those interested in using Python via a USB thumbdrive, you may be interested in Portable Python. This is a self-contained Python environment that you can either run from the thumbdrive or install to your computer. This is useful for people who can’t or don’t want to install Python but would still like to use it. I’ll assume you can figure out how to get the interactive interpreter running; if you need help, read the help pages on the web site. Gener- CHAPTER 1. INTRODUCTION 13
ally speaking though, you open up a command prompt (or terminal)
and type “python” at the prompt. This will open a Python session, allowing you to work with the Python interpreter in an interactive manner. In Windows, typically you just go to the Python file in All Programs and click it.
1.4 Conventions Used in this Book
The latest version of Python is 3.2 while the most current “legacy” version is 2.7. I will use the term 3.x to signify anything in the Python 3 family and 2.x for anything in the Python 2 family, unless explicitly stated. The term “*nix” is used to refer to any Unix-like language, in- cluding Linux and the various flavors of BSD (FreeBSD, OpenBSD, NetBSD). Though Mac OS X is built upon FreeBSD, it is different enough to not be lumped in the *nix label. Due to the word-wrap formatting for this book, some lines are automatically indented when they are really on the same line. It may make some of the examples confusing, especially because Python uses “white space” like tabs and spaces as significant areas. Thus, a word- wrapped line may appear to be an indented line when it really isn’t. Hopefully you will be able to figure out if a line is intentionally tabbed over or simply wrapped.