Preface

This book will introduce you to the Python programming language. It’s aimed at beginning programmers, but even if you’ve written programs before and just want to add Python to your list of languages, Introducing Python will get you started.

It’s an unhurried introduction, taking small steps from the basics to more involved and varied topics. I mix cookbook and tutorial styles to explain new terms and ideas, but not too many at once. Real Python code is included early and often.

Even though this is an introduction, I include some topics that might seem advanced, such as NoSQL databases and message-passing libraries. I chose these because they can solve some problems better than standard solutions. You’ll download and install external Python packages, which is good to know when the “batteries included” with Python don’t fit your application. And it’s fun to try something new.

I also include some examples of what not to do, especially if you’ve programmed in other languages and try to adapt those styles to Python. And I won’t pretend that Python is perfect; I’ll show you what to avoid.

Note

Sometimes, I’ll include a note such as this when something might be confusing or there’s a more appropriate Pythonic way to do it.

Audience

This book is for anybody interested in learning what seems to be emerging as the world’s most popular computing language, whether or not you have learned any programming before.

Outline

The first seven chapters explain Python’s basics, and you should read them in order. The later chapters show how Python is used in specific application areas such as the Web, databases, networks, and so on; read them in any order you like. The first three appendices showcase Python in the arts, business, and science. Then, you see how to install Python 3 if you don’t have it. Next are answers to the end-of-chapter exercises, and then finally, a few cheat sheets of useful things.

Chapter 1

Programs are like directions for making socks or grilling potatoes. Some real Python programs give a little demonstration of the language’s look, capabilities, and uses in the real world. Python fares well when compared with other languages, but it has some imperfections. An older version of Python (Python 2) is giving way to a newer one (Python 3). If you have Python 2, install Python 3 on your computer. Use the interactive interpreter to try examples from this book yourself.

Chapter 2

This chapter shows Python’s simplest data types: booleans, integers, floating-point numbers, and text strings. You also learn the basic math and text operations.

Chapter 3

We step up to Python’s higher-level built-in data structures: lists, tuples, dictionaries, and sets. You use these as you would Legos to build much more complicated structures. You learn how to step through them by using iterators and comprehensions.

Chapter 4

In Chapter 4, you weave the data structures of the previous chapters with code structures to compare, choose, or repeat. You see how to package code in functions and handle errors with exceptions.

Chapter 5

This chapter demonstrates how to scale out to larger code structures: modules, packages, and programs. You see where to put code and data, get data in and out, handle options, tour the Python Standard Library, and take a glance at what lies beyond.

Chapter 6

If you’ve done object-oriented programming in other languages, Python is a bit more relaxed. Chapter 6 explains when to use objects and classes, and when it’s better to use modules or even lists and dictionaries.

Chapter 7

Learn to manage data like a pro. This chapter is all about text and binary data, the joy of Unicode characters, and I/O.

Chapter 8

Data needs to go somewhere. In this chapter, you begin with basic flat files, directories, and filesystems. Then, you see how to handle common file formats such as CSV, JSON, and XML. You also explore how to store and retrieve with relational databases, and even some recent NoSQL data stores.

Chapter 9

The Web gets its own chapter, which covers clients, servers, scraping, APIs, and frameworks. In Chapter 9, you work up a real website with request parameters and templates.

Chapter 10

This is the hard-core system chapter. In this one, you learn to manage programs, processes, and threads; deal with dates and times; and automate some system administration tasks.

Chapter 11

Networking is the subject here: services, protocols, and APIs. Examples range from low-level TCP sockets, to messaging libraries and queuing systems, to cloud deployment.

Chapter 12

This chapter contains tips for Python developers, including installing, using IDEs, testing, debugging, logging, source control, and documentation. Chapter 12 also helps you to find and install useful third-party packages, package your own code for reuse, and learn where to get more information. Good luck.

Appendix A

The first appendix delves into what people are doing with Python in the arts: graphics, music, animation, and games.

Appendix B

Python has specific applications for business: data visualization (plots, graphs, and maps), security, and regulation.

Appendix C

Python has a strong presence in science: math and statistics, physical science, bioscience, and medicine. Appendix C features NumPy, SciPy, and Pandas.

Appendix D

If you don’t already have Python 3 on your computer, this appendix shows you how to install it, no matter if you’re running Windows, Mac OS/X, Linux, or Unix.

Appendix E

This has the answers to the end-of-chapter exercises. Don’t peek here until you’ve tried the exercises yourself.

Appendix F

This appendix contains cheat sheets to use as a quick reference.

Python Versions

Computer languages change over time as developers add features and fix mistakes. The examples in this book were written and tested while running Python version 3.3. Version 3.4 was released as this book was being edited, and I’ll talk about a few of its additions. If you want to know what was added to Python and when, try the What’s New in Python page. It’s a technical reference; a bit heavy when you’re just starting with Python, but may be useful in the future if you ever have to get programs to work on computers with different Python versions.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variables, functions, and data types.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

The substantial code examples in this book—although not the exercises, which are challenges for the reader—are available online for you to download. This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Introducing Python by Bill Lubanovic (O’Reilly). Copyright 2015 Bill Lubanovic, 978-1-449-35936-2.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at [email protected].

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/introducing_python.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Thanks go to the many people who read and commented on my draft. I’d like to particularly mention the careful reviews by Eli Bessert, Henry Canival, Jeremy Elliott, Monte Milanuk, Loïc Pefferkorn, and Steven Wayne.

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

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