Skip to main content

About this book

Gain a fundamental understanding of Python’s syntax and features with this up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time.

Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in Python 3, Beginning Python also covers advanced topics such as extending Python and packaging/distributing Python applications.

What You'll Learn

Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features

Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction

Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution

Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more

Who This Book Is For

Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language.

Table of Contents

Chapter 1. Instant Hacking: The Basics

It’s time to start hacking. In this chapter, you learn how to take control of your computer by speaking a language it understands: Python. Nothing here is particularly difficult, so if you know the basic principles of how your computer works, you should be able to follow the examples and try them out yourself. I’ll go through the basics, starting with the excruciatingly simple, but because Python is such a powerful language, you’ll soon be able to do pretty advanced things.
Magnus Lie Hetland

Chapter 2. Lists and Tuples

This chapter introduces a new concept: data structures. A data structure is a collection of data elements (such as numbers or characters, or even other data structures) that is structured in some way, such as by numbering the elements. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number—its position, or index. The first index is zero, the second index is one, and so forth. Some programming languages number their sequence elements starting with one, but the zero-indexing convention has a natural interpretation of an offset from the beginning of the sequence, with negative indexes wrapping around to the end. If you find the numbering a bit odd, I can assure you that you’ll most likely get used to it pretty fast.
Magnus Lie Hetland

Chapter 3. Working with Strings

You’ve seen strings before and know how to make them. You’ve also looked at how to access their individual characters by indexing and slicing. In this chapter, you see how to use them to format other values (for printing, for example) and take a quick look at the useful things you can do with string methods, such as splitting, joining, searching, and more.
Magnus Lie Hetland

Chapter 4. Dictionaries: When Indices Won’t Do

You’ve seen that lists are useful when you want to group values into a structure and refer to each value by number. In this chapter, you learn about a data structure in which you can refer to each value by name. This type of structure is called a mapping. The only built-in mapping type in Python is the dictionary. The values in a dictionary don’t have any particular order but are stored under a key, which may be a number, a string, or even a tuple.
Magnus Lie Hetland

Chapter 5. Conditionals, Loops, and Some Other Statements

By now, I’m sure you are getting a bit impatient. All right—all these data types are just dandy, but you can’t really do much with them, can you?
Magnus Lie Hetland

Chapter 6. Abstraction

In this chapter, you’ll learn how to group statements into functions, which enables you to tell the computer how to do something, and to tell it only once. You won’t need to give it the same detailed instructions over and over. The chapter provides a thorough introduction to parameters and scoping, and you’ll learn what recursion is and what it can do for your programs.
Magnus Lie Hetland

Chapter 7. More Abstraction

In the previous chapters, you looked at Python’s main built-in object types (numbers, strings, lists, tuples, and dictionaries); you peeked at the wealth of built-in functions and standard libraries; and you even created your own functions. Now, only one thing seems to be missing—making your own objects. And that’s what you do in this chapter.
Magnus Lie Hetland

Chapter 8. Exceptions

When writing computer programs, it is usually possible to discern between a normal course of events and something that’s exceptional (out of the ordinary). Such exceptional events might be errors (such as trying to divide a number by zero) or simply something you might not expect to happen very often. To handle such exceptional events, you might use conditionals everywhere the events might occur (for example, have your program check whether the denominator is zero for every division). However, this would not only be inefficient and inflexible but would also make the programs illegible. You might be tempted to ignore these exceptional events and just hope they won’t occur, but Python offers an exception-handling mechanism as a powerful alternative.
Magnus Lie Hetland

Chapter 9. Magic Methods, Properties, and Iterators

In Python, some names are spelled in a peculiar manner, with two leading and two trailing underscores. You have already encountered some of these (_ _future_ _, for example). This spelling signals that the name has a special significance—you should never invent such names for your own programs. One very prominent set of such names in the language consists of the magic (or special) method names. If your object implements one of these methods, that method will be called under specific circumstances (exactly which will depend on the name) by Python. There is rarely any need to call these methods directly.
Magnus Lie Hetland

Chapter 10. Batteries Included

You now know most of the basic Python language. While the core language is powerful in itself, Python gives you more tools to play with. A standard installation includes a set of modules called the standard library. You have already seen some of them (math and cmath, for example), but there are many more. This chapter shows you a bit about how modules work and how to explore them and learn what they have to offer. Then the chapter offers an overview of the standard library, focusing on a few selected useful modules.
Magnus Lie Hetland

Chapter 11. Files and Stuff

So far, we’ve mainly been working with data structures that reside in the interpreter itself. What little interaction our programs have had with the outside world has been through input and print. In this chapter, we go one step further and let our programs catch a glimpse of a larger world: the world of files and streams. The functions and objects described in this chapter will enable you to store data between program invocations and to process data from other programs.
Magnus Lie Hetland

Chapter 12. Graphical User Interfaces

In this rather short chapter, you’ll learn the basics of how to make graphical user interfaces (GUIs) for your Python programs—you know, windows with buttons and text fields and stuff like that. The de facto standard GUI toolkit for Python is Tkinter, which ships as part of the standard Python distribution. Several other toolkits are available, however. This has its advantages (greater freedom of choice) and drawbacks (others can’t use your programs unless they have the same GUI toolkit installed). Fortunately, there is no conflict between the various GUI toolkits available for Python, so you can install as many different GUI toolkits as you want.
Magnus Lie Hetland

Chapter 13. Database Support

Using simple, plain-text files can get you only so far. Yes, they can get you very far, but at some point, you may need some extra functionality. You may want some automated serialization, and you can turn to shelve (see Chapter 10) and pickle (a close relative of shelve). But you may want features that go beyond even this. For example, you might want to have automated support for concurrent access to your data, that is, to allow several users to read from and write to your disk-based data without causing any corrupted files or the like. Or you may want to be able to perform complex searches using many data fields or properties at the same time, rather than the simple single-key lookup of shelve. There are plenty of solutions to choose from, but if you want this to scale to large amounts of data and you want the solution to be easily understandable by other programmers, choosing a relatively standard form of database is probably a good idea.
Magnus Lie Hetland

Chapter 14. Network Programming

In this chapter, I give you a sample of the various ways in which Python can help you write programs that use a network, such as the Internet, as an important component. Python is a very powerful tool for network programming. Many libraries for common network protocols and for various layers of abstractions on top of them are available, so you can concentrate on the logic of your program, rather than on shuffling bits across wires. Also, it’s easy to write code for handling various protocol formats that may not have existing code, because Python is really good at tackling patterns in byte streams (you’ve already seen this in dealing with text files in various ways).
Magnus Lie Hetland

Chapter 15. Python and the Web

This chapter tackles some aspects of web programming with Python. This is a really vast area, but I’ve selected three main topics for your amusement: screen scraping, CGI, and mod_python.
Magnus Lie Hetland

Chapter 16. Testing, 1-2-3

How do you know that your program works? Can you rely on yourself to write flawless code all the time? Meaning no disrespect, I would guess that’s unlikely. It’s quite easy to write correct code in Python most of the time, certainly, but chances are your code will have bugs.
Magnus Lie Hetland

Chapter 17. Extending Python

You can implement anything in Python, really; it’s a powerful language, but sometimes it can get a bit too slow. For example, if you’re writing a scientific simulation of some form of nuclear reaction or you’re rendering the graphics for the next Star Wars movie, writing the high-performance code in Python will probably not be a good choice. Python is meant to be easy to work with and to help make the development fast. The flexibility needed for this comes with a hefty price in terms of efficiency. It’s certainly fast enough for most common programming tasks, but if you need real speed, languages such as C, C++, Java, or Julia can usually beat it by several orders of magnitude.
Magnus Lie Hetland

Chapter 18. Packaging Your Programs

Once your program is ready for release, you will probably want to package it properly before distributing it. If it consists of a single .py file, this might not be much of an issue. If you’re dealing with nonprogrammer users, however, even placing a simple Python library in the right place or fiddling with the PYTHONPATH may be more than they want to deal with. Users normally want to simply double-click an installation program, follow some installation wizard, and then have your program ready to run.
Magnus Lie Hetland

Chapter 19. Playful Programming

At this point, you should have a clearer picture of how Python works than when you started. Now the rubber hits the road, so to speak, and in the next ten chapters you put your newfound skills to work. Each chapter contains a single do-it-yourself project with a lot of room for experimentation, while at the same time giving you the necessary tools to implement a solution.
Magnus Lie Hetland

Chapter 20. Project 1: Instant Markup

In this project, you see how to use Python’s excellent text-processing capabilities, including the capability to use regular expressions to change a plain-text file into one marked up in a language such as HTML or XML. You need such skills if you want to use text written by people who don’t know these languages in a system that requires the contents to be marked up.
Magnus Lie Hetland

Chapter 21. Project 2: Painting a Pretty Picture

In this project, you’ll learn how you can create graphics in Python. More specifically, you’ll be creating a PDF file with graphics helping you visualize data that you read from a text file. While you could get such functionality from a regular spreadsheet, Python gives you much more power, as you’ll see when you get to the second implementation and automatically download your data from the Internet.
Magnus Lie Hetland

Chapter 22. Project 3: XML for All Occasions

I mentioned XML briefly in Project 1. Now it’s time to examine it in more detail. In this project, you see how XML can be used to represent many kinds of data and how XML files can be processed with the Simple API for XML, or SAX. The goal of this project is to generate a full web site from a single XML file that describes the various web pages and directories.
Magnus Lie Hetland

Chapter 23. Project 4: In the News

The Internet is replete with news sources in many forms, including newspapers, video channels, blogs and podcasts, to name a few. Some of these also provide services, such as RSS or Atom feeds, that let you retrieve the latest news using relatively simple code, without having to parse their web pages. In this project, we’ll be exploring a mechanism that predates the Web: the Network News Transfer Protocol (NNTP). We’ll go from a simple prototype without any form of abstraction (no functions, no classes) to a generic system in which some important abstractions have been added. We’ll be using the nntplib library, which lets you interact with NNTP servers, but adding other protocols and mechanisms should be straightforward.
Magnus Lie Hetland

Chapter 24. Project 5: A Virtual Tea Party

In this project, we’ll do some serious network programming. We’ll write a chat server—a program that lets several people connect via the Internet and chat with each other in real time. There are many ways to create such a beast in Python.
Magnus Lie Hetland

Chapter 25. Project 6: Remote Editing with CGI

This chapter’s project uses CGI, which is discussed in more detail in Chapter 15. The specific application is remote editing—editing a document on another machine via the Web. This can be useful in collaboration systems (groupware), for example, where several people may be working on the same document. It can also be useful for updating your web pages.
Magnus Lie Hetland

Chapter 26. Project 7: Your Own Bulletin Board

Many kinds of software enable you to communicate with other people over the Internet. You’ve seen a few already (for example, the Usenet groups in Chapter 23 and the chat server in Chapter 24). In this chapter, you will implement another such system: a web-based discussion forum. While the functionality is a far cry from complex social media platforms, it does implement the basics needed for comment systems, for example.
Magnus Lie Hetland

Chapter 27. Project 8: File Sharing with XML-RPC

This chapter’s project is a simple file sharing application. You may be familiar with the concept of file sharing from such applications as the (in)famous Napster (no longer downloadable in its original form), Gnutella (see for discussions about available clients), BitTorrent (available from, and many others. What we’ll be writing is in many ways similar to these, although quite a bit simpler.
Magnus Lie Hetland

Chapter 28. Project 9: File Sharing II—Now with GUI!

This is a relatively short project because much of the functionality you need has already been written—in Chapter 27. In this chapter, you see how easy it can be to add a GUI to an existing Python program.
Magnus Lie Hetland

Chapter 29. Project 10: Do-It-Yourself Arcade Game

Welcome to the final project. Now that you’ve sampled several of Python’s many capabilities, it’s time to go out with a bang. In this chapter, you’ll learn how to use Pygame, an extension that enables you to write full-fledged, full-screen arcade games in Python. Although easy to use, Pygame is quite powerful and consists of several components that are thoroughly described in the Pygame documentation (available on the Pygame web site, This project introduces you to some of the main Pygame concepts, but because this chapter is only meant as a starting point, I’ve skipped several interesting features, such as sound and video handling. I recommend that you investigate the other features yourself, once you’ve familiarized yourself with the basics. You might also want to take a look at Beginning Game Development with Python and Pygame by Will McGugan (Apress, 2007) or Program Arcade Games with Python and Pygame by Paul Craven (Apress, 2016).
Magnus Lie Hetland
Additional information