05_778648 ch01.qxp 8/7/06 10:46 PM Page 7
Chapter 1 Introducing Python
In This Chapter The history of Python What people use Python for Useful concepts for Python programming
elcome to Python! If you’re the type of person who wants to know Wwhat you’re getting into, this chapter is for you. We give you a quick history of Python and its community of developers. You find out what Python is and isn’t good for (the “is” section is much longer than the “isn’t” section) and the most important principles of good Python programming. If you’re new to programming, you’ll see how it’s very similar to a task you’re proba- bly familiar with.
The Right Tool for the Job Python is a general-purpose, high-level language that can be extended and embedded (included in applications as a tool for writing macros). That makes Python a smart choice for many programming problems, both small and large, and not so good for a couple of computing tasks.
GoodCOPYRIGHTED uses of Python MATERIAL Python is ideal for projects that require quick development. It supports multiple programming philosophies, so it’s good for programs that require flexibility. The many packages and modules already written for Python pro- vide versatility and save you time. 05_778648 ch01.qxp 8/7/06 10:46 PM Page 8
8 Part I: Getting Started
The story of Python Guido van Rossum created Python and is affec- manipulate. For example, Python strings tionately bestowed with the title “Benevolent can be searched, sliced, joined, split, set to Dictator For Life” by the Python community. upper- or lowercase, or have white space In the late 1980s, Guido liked features of several removed. High-level data types in Python, programming languages, but none of them such as lists and dicts (which can store had all the features he wanted. Specifically, other data types), encompass much more he wanted a language that had the following functionality than in other languages. features: Extensibility: An extensible programming Scripting language: A script is a program language can be added to. These languages that controls other programs. Scripting lan- are very powerful because additions make guages are good for quick development and them suitable for multiple applications and prototyping because they’re good at pass- operating systems. Extensions can add data ing messages from one component to types or concepts, modules, and plug-ins. another and at handling fiddly stuff like Python is extensible in several ways. A core memory management so that the program- group of programmers works on modifying mer doesn’t have to. Python has grown and improving the language, while hundreds beyond scripting languages, which are of other programmers write modules for spe- used mostly for small applications. The cific purposes. Python community prefers to call Python a Interpreted: Interpreted languages run dynamic programming language. directly from source code that humans gen- Indentation for statement grouping: Python erate (whereas programs written in com- specifies that several statements are part piled languages, like C++, must be translated of a single group by indenting them. The to machine code before they can run). indented group is called a code block. Other Interpreted languages run more slowly languages use different syntax or punctua- because the translation takes place on the tion for statement grouping. For example, the fly, but development and debugging is faster C programming language uses { to begin because you don’t have to wait for the com- an instruction and } to end it. Indentation is piler. Interpreted languages are easier to run considered good practice in other languages on multiple operating systems. In the case of also, but Python was one of the first to Python, it’s easy to write code that works on enforce indentation. Indentation makes code multiple operating systems — with no need easier to read, and code blocks set off with to make modifications. indentation have fewer begin/end words and People argue over whether Python is an punctuation to accidentally leave out (which interpreted or compiled language. Although means fewer bugs). Python works like an interpreted language High-level data types: Computers store in many ways, its code is compiled before everything in 1s and 0s, but humans need to execution (like Java), and many of its capa- work with data in more complex forms, bilities run at full machine speed because such as text. A language that supports such they’re written in C — leaving you free to complex data is said to have high-level data focus on making your application work. types. A high-level data type is easy to 05_778648 ch01.qxp 8/7/06 10:46 PM Page 9
Chapter 1: Introducing Python 9
Guido began writing Python during his Christmas February 1991 by posting to the Usenet system of vacation in 1989, and over the next year, newsgroups. In Guido’s words: “The rest is in the he added to the program based on feedback Misc/HISTORY file.” from colleagues. He released it to the public in
Fast development High-level features make Python a wise alternative for prototyping and fast development of complex applications: