Introduction

Welcome to Python 3.1!

I've been working with Python for about ten years now, and every new version has caused me to fall in love with the language all over again. Version 3.1 is no different. If you are new to Python, rest easy — I'll guide you every step of the way. If, on the other hand, you are an old Python hand exploring the new version, the book is structured so that you can learn the new information you need, without wasting time on already-known information.

I wanted to write this book because I love Python. I love it! And I want to share my love with you. And, maybe you'll grow to love it as I do.

Who This Book Is For

If you're computer-literate, and want to learn a fun programming language to better control your computer, this book is for you.

If you are a system administrator who wants to learn a great language to help you better manage and configure systems and networks, this book is for you.

If you already know Python, but are wondering what cool new features are available in version 3.1, this book is for you.

In summary, this book is for anyone interested in exploring Python programming with the newest and most full-featured, easy-to-use version, 3.1.

What This Book Covers

This book is designed to cover Python 3.1. Python 3.1, released in 2009, is the latest major revision of the Python programming language. Since Python is a cross-platform language, the content and examples in the book are applicable in any platform (unless specified otherwise). When there is a choice to be made as to platform independence, the examples will be as cross-platform as possible.

In addition, since Python 3.1 is relatively new, not all supporting libraries have been updated to work in Python 3.x. In those instances where this is the case and it is felt that the theory still needs to be expounded upon, Python 2.6 will be used in lieu of version 3.1.

How This Book Is Structured

As might be expected from a "Beginning" book, the book begins with an introduction to the language. From there, you'll move through the core of the language, then move on to more advanced and specialized topics. The book is divided up into four parts.

Part I — Dipping Your Toe into Python

The first part will allow you to, as the title suggests, dip your toe in.

Programming Basics and Strings

First you'll be introduced to Python. This chapter will explore what Python is, and why it is so useful and powerful. Also explored will be Python's history from its early development to the newest version, which is the focus of this book. You'll also learn about the scope of Python's reach, and all the different areas of application development in which Python plays a part. Finally, you'll learn to work with your first data type — strings.

Numbers and Operators

This chapter will guide you through the basics of working with numbers and operators. You will learn the different types of numbers, how to perform simple — and complex — equations, and work with the various operators. You will also learn about order of precedence and formatting numbers.

Variables — Names for Values

Ultimately, programming languages help you to manage different types of information — in other words, data. An understanding of data types and how they are represented in Python is essential to programming in Python. This chapter will help you to understand the best ways to represent different data types in Python.

Part II — Python Language and the Standard Library

Of course, the core piece of knowledge you need to use a language is to know the language itself, and familiarize yourself with its syntax and modules. This part will start small, with data types and variables, and gradually introduce additional concepts until you have all the information you need to develop fully functional Python programs.

You'll want to read through these chapters sequentially — each chapter builds on the information presented in the previous chapter.

Making Decisions

Ultimately, there will come a point when your program must make a decision — do I take this path or that path? And what happens when I take that path? In this chapter, you will learn how to compare data, such as deciding if one value is greater than another, and use repetition to repeat repetitive tasks.

Functions

This chapter will help you to expand on your Python knowledge by introducing you to functional programming. Functions allow you to take advantage of powerful concepts like parameter passing and code reuse. You'll learn how to use functions to make your code more efficient and flexible.

Classes and Objects

Here you will be shown what objects are and learn to create classes. You will learn how to define them, create objects in your classes, write methods, and discuss the scope of your objects.

Organizing Programs

When your programs get larger, you'll want to divide them up into separate components. This chapter will discuss Python modules. You'll also explore packages, which are nothing but collections of modules.

Files and Directories

An important part of everyday programming is learning to work with files and directories. This chapter focuses on creating, modifying, and working with files in general. In addition, you will learn how to obtain data from files and how to interact with the various directories.

Other Features of the Language

Here you will learn about some of the other features the language has to offer, including how to make decisions with lists, string substitutions with dictionaries, and some of the featured modules.

Building a Module

Modules help you save time by allowing you to reuse snippets of code. It also ensures fewer errors, as the module you use will have been tested and used many times before. Here, we will learn to create our own modules, as well as import and work with pre-existing modules — something that makes Python particularly powerful.

Text Processing

There are so many things you can do with text in programming and in essence, text is the key to effectively communicating with your user. After all, without it, the only thing you are left with is images. In this chapter you learn to process text in a variety of ways, including: working with regular expressions, searching for files, and searching for files of a particular type.

Part III — Putting Python to Work

So, now that you know what Python is, and how to work with the language, what's next, you ask? This final part explores many of the programming topics you'll likely encounter or want to explore. These can be looked at sequentially, or in any order you like ... these chapters are independent of each other.

Testing

There is only one way to ensure your program works before it is in the hands of the user, and that is by testing your program. Here, you will learn not only the concepts behind properly testing your programs, but the tools and frameworks available to you.

Writing a GUI with Python

Thus far in the book, all the programs work through the command line. In this chapter, you'll be introduced to the concept of GUI programming. You'll also walk through creating a few GUI programs with Tkinter, the most popular GUI toolkit used by Python programmers.

Accessing Databases

Databases store information that your program can use for an infinite amount of reasons. It also acts as a place for you to store information, and later retrieve that information for a given task. In this chapter you learn about the different types of databases and how to work with them.

Using Python for XML

XML is a powerful tool for processing data on the Internet. Here, you will learn the basics of XML including the difference between schema and DTD, basic syntax, how to create and validate your own XML, and more advanced topics such as using lxml.

Network Programming

Now that the Internet has wormed its way into our everyday lives, and has become more of a necessity than a privilege, learning to programmatically send e-mails and allow users to communicate across the web is essential. In this chapter, you will learn how to do just that.

Extension Programming with C

This chapter delves into programming with the C language, including working with C frameworks and modules, the basics of C, and passing parameters from Python to C, and then returning value back to Python.

Numerical Programming

Numbers were touched on briefly in the beginning of this book; now it is time to delve more deeply below the surface. Here you will learn all there is to know about integers and floating point numbers, as well as complex numbers, arrays, and working with built-in math functions and modules.

An Introduction to Django

Django is a web application framework written in Python, which utilizes the model-view-architecture pattern. Originally created for managing news websites, Django has become popular for its ease of use, allowing programmers to create complex websites in a simple fashion, including database-focused sites. Here we will learn the basics of Django.

Web Applications and Web Services

Here you will learn the foundations of working with web applications and web services. You will learn about the REST architecture, as well as how to work with HTTP Requests and Responses.

Integrating Java with Python

In this chapter you learn the basics of Java, building a strong foundation before you delve blending the two languages together. You will learn the various modules that allow you to work with Java in Python and how to create simple, yet effective applications.

Part IV: Appendices

In the back of the book, there are some useful appendices to further your knowledge and fun with Python:

  • Answers to the Exercises

  • Online Resources — where do you go from here?

  • What's New in Python 3.1

  • Glossary of terms

What You Need to Use This Book

There are some minimal requirements to use the material in this book. The following are recommendations, as Python itself runs on many different platforms. However, the first chapters assume that you have access to a GUI such as is available in Windows, Mac OS X, or the X Window system on UNIX and Linux. Naturally, some chapters, such as the GUI chapter, require the GUI as well, and chapters involving networking will make much more sense if a network connection is in place.

Following are the suggested minimum requirements:

  • A PC running Linux, a BSD UNIX, or Windows running at 500MHz or faster, or a G3 or later Macintosh running Mac OS X version 10.2 or later

  • 256MB of memory (at a minimum)

  • A graphical user interface native to the platform you are on

  • Necessary access to the computer you are on so that you may install required software

  • Network access to a TCP/IP network such as the Internet or a campus network

  • Internet access to download required software

Conventions

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.

Examples that you can download and try out for yourself generally appear in a box like this:

Note

Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

Notes, tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

  • We highlight new terms and important words when we introduce them.

  • We show keyboard strokes like this: Ctrl+A.

  • We show file names, URLs, and code within the text like so: persistence.properties.

  • We present code in two different ways:

    We use a monofont type with no highlighting for most code examples.
    We use bold highlighting to emphasize code that's particularly important
    in the present context.

Source Code

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download at http://www.wrox.com. Once at the site, simply locate the book's title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book.

Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-0-470-41463-7.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to http://www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book's errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don't spot "your" error on the Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

  1. Go to p2p.wrox.com and click the Register link.

  2. Read the terms of use and click Agree.

  3. Complete the required information to join as well as any optional information you wish to provide and click Submit.

  4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.136.63