Python 3

Python 3

$10.00

There are many more people who want to study programming other than aspiring computer scientists with a passing grade in advanced calculus. This guide appeals to your intelligence and ability to solve practical problems, while gently teaching the most recent revision of the programming language Python.

Introducing Python
So, you want to learn programming. Welcome to one of the great adventures of the twenty-first century.
Programming requires little in the way of specialized equipment; the software tools can all be
downloaded for free off the Internet, and it can be practiced in the safety and comfort of your own home,
without having to ask anyone’s permission. This chapter will ease you in gently by introducing you to the
software you will need to create your programs: a command-line interface, which allows you to use
Python in interactive mode, and a text editor for writing scripts—nothing more complicated than that. I
will also show you where to go to find help and documentation, so you can decode the sometimes
impenetrable jargon that seems to surround this, the geekiest of all technical disciplines. To begin with,
you will need to make sure that you have a decently recent version of Python installed on your machine
or follow the steps later in this chapter to install it (see “Choosing the Right Python Version” for a
definition of decently recent). This chapter explains how to make sure that you have everything set up
correctly and that you have suitable references at hand before you start your journey.
Python is an excellent language with which to learn programming. There are many reasons for this,
but the simple explanation is that it’s easy to read and fast to write; it doesn’t take long to come up with
working code that does something meaningful. Python has a very human-friendly syntax, which makes
writing elegant code easy. The basic language is fairly simple and therefore easy to remember, and then
it has an extensive library of predefined functions that you can use to facilitate most common computer
tasks. Writing effective applications in Python can be as simple as playing with conceptual building
blocks. It works really well for writing a little two-line application to perform some routine system
administration task or to provide interactive functions on a web page, but it has enough power and
flexibility to comfortably create much larger and more complex applications with graphic interfaces
indistinguishable from the programs you are used to running from your computer’s main menu. If you
follow the suggestions laid out in this book about writing self-explanatory code, in several months, even
years, you will be able to come back to your programs and see immediately what they were supposed to
do and what your original intentions were; this makes maintaining programs much simpler too.
OK, let’s make sure your system is ready for you to start running Python programs.

Categories: ,

Description

There are many more people who want to study programming other than aspiring computer scientists with a passing grade in advanced calculus. This guide appeals to your intelligence and ability to solve practical problems, while gently teaching the most recent revision of the programming language Python.

You can learn solid software design skills and accomplish practical programming tasks, like extending applications and automating everyday processes, even if you have no programming experience at all. Authors Tim Hall and J–P Stacey use everyday language to decode programming jargon and teach Python 3 to the absolute beginner.

What you’ll learn

Cultivate a problem–solving approach and acquire software design skills.

Learn how to create your own software from simple “Hello World”-type programs to stand–alone windowed applications.

Document while programming, program while documenting.

Decode programmers’ jargon.

Master the Python 3 programming language.

Who this book is for

Non–programmers who want to learn Python programming without taking a detour via a computer science department.

 

iii

Contents at a Glance
■Contents……………………………………………………………………………………………………….v
■About the Author…………………………………………………………………………………………..xi
■About the Technical Reviewer………………………………………………………………………..xii
■Chapter 1: Introducing Python…………………………………………………………………………1
■Chapter 2: Designing Software…………………………………………………………………………9
■Chapter 3: Variables and Data Types……………………………………………………………….27
■Chapter 4: Making Choices…………………………………………………………………………….49
■Chapter 5: Using Lists……………………………………………………………………………………75
■Chapter 6: Functions…………………………………………………………………………………..101
■Chapter 7: Working with Text……………………………………………………………………….125
■Chapter 8: Executable Files, Organization, and Python on the Web……………………161
■Chapter 9: Classes………………………………………………………………………………………181
■Chapter 10: Exceptions………………………………………………………………………………..221
■Chapter 11: Reusing Code with Modules and Packages…………………………………..241
■Chapter 12: Simple Windowed Applications……………………………………………………261
■Index…………………………………………………………………………………………………………283

■ CONTENTS
v

Contents
■About the Author…………………………………………………………………………………………..xi
■About the Technical Reviewer………………………………………………………………………..xii
■Chapter 1: Introducing Python…………………………………………………………………………1
Running Python on Various Systems………………………………………………………………………1
Learning While Having Fun……………………………………………………………………………………4
Introducing the Interactive Shell…………………………………………………………………………….4
Choosing to Code with a Text Editor……………………………………………………………………….5
Choosing to Code with an Integrated Development Environment………………………………..5
Getting Started with Programming…………………………………………………………………………5
Creating Your Own Help Files………………………………………………………………………………..6
Jargon Busting…………………………………………………………………………………………………….6
Summary……………………………………………………………………………………………………………7
■Chapter 2: Designing Software…………………………………………………………………………9
Designing Your Own Software (Why Bother?)…………………………………………………………..9
Identifying the Problem……………………………………………………………………………………….10
Creating Your Wish List……………………………………………………………………………………….12
Devising a Solution…………………………………………………………………………………………….13
Breaking Down the Solution into Steps…………………………………………………………………15
Jargon Busting…………………………………………………………………………………………………..24
Summary………………………………………………………………………………………………………….25
■Chapter 3: Variables and Data Types……………………………………………………………….27
Choosing Good Identifiers……………………………………………………………………………………27
Creating Variables and Assigning Values……………………………………………………………….28
Recognizing Different Types of Variables………………………………………………………………29
Jargon Busting…………………………………………………………………………………………………..45
■ CONTENTS

vi

Summary………………………………………………………………………………………………………….47
■Chapter 4: Making Choices…………………………………………………………………………….49
Comparing Things………………………………………………………………………………………………49
Taking Control of the Process………………………………………………………………………………55
Dealing with Logical Errors………………………………………………………………………………….57
Using Conditional Code in the Application……………………………………………………………..61
Now Repeat That……………………………………………………………………………………………….65
Jargon Busting…………………………………………………………………………………………………..73
Summary………………………………………………………………………………………………………….74
■Chapter 5: Using Lists……………………………………………………………………………………75
Working with Python Data Structures……………………………………………………………………75
Tuples………………………………………………………………………………………………………………79
Lists…………………………………………………………………………………………………………………80
Sets………………………………………………………………………………………………………………….85
Dictionaries……………………………………………………………………………………………………….86
A Simple Role-Playing Combat Game……………………………………………………………………89
Jargon Busting…………………………………………………………………………………………………..99
Summary………………………………………………………………………………………………………..100
■Chapter 6: Functions…………………………………………………………………………………..101
Accessing Privileged Information……………………………………………………………………….101
Working with Variable Scope……………………………………………………………………………..105
Refactoring rpcombat.py to Reduce Repetition…………………………………………………….108
Jargon Busting…………………………………………………………………………………………………123
Summary………………………………………………………………………………………………………..124
■Chapter 7: Working with Text……………………………………………………………………….125
Strings and Things……………………………………………………………………………………………125
Matching Patterns Using Regular Expressions……………………………………………………..135
Using Files………………………………………………………………………………………………………141
Applications…………………………………………………………………………………………………….145
Jargon Busting…………………………………………………………………………………………………159
Summary………………………………………………………………………………………………………..160
■ CONTENTS
vii

■Chapter 8: Executable Files, Organization, and Python on the Web……………………161
Making Programs Executable as Stand-Alone Applications……………………………………161
Organizing Your Project…………………………………………………………………………………….164
Writing Stylish Code………………………………………………………………………………………….165
Importing Modules……………………………………………………………………………………………170
Using exec() and eval()………………………………………………………………………………………172
Putting Python on the Web………………………………………………………………………………..173
Jargon Busting…………………………………………………………………………………………………179
Summary………………………………………………………………………………………………………..179
■Chapter 9: Classes………………………………………………………………………………………181
Empowering objects…………………………………………………………………………………………182
When Should Classes Be Used?…………………………………………………………………………185
Customizing Classes…………………………………………………………………………………………191
Application………………………………………………………………………………………………………200
Jargon Busting…………………………………………………………………………………………………219
Summary………………………………………………………………………………………………………..220
■Chapter 10: Exceptions………………………………………………………………………………..221
When Something Goes Wrong……………………………………………………………………………221
Classes of Exceptions……………………………………………………………………………………….224
A Final Note on Pythonic Exception Handling……………………………………………………….238
Jargon Busting…………………………………………………………………………………………………239
Summary………………………………………………………………………………………………………..240
■Chapter 11: Reusing Code with Modules and Packages…………………………………..241
Understanding Python Modules………………………………………………………………………….241
Everyday Module Usage…………………………………………………………………………………….244
Advanced Module Behavior……………………………………………………………………………….249
Combining Modules into Packages……………………………………………………………………..252
The Universe of Python packages……………………………………………………………………….254
Jargon Busting…………………………………………………………………………………………………259
Summary………………………………………………………………………………………………………..260
■ CONTENTS

viii

■Chapter 12: Simple Windowed Applications……………………………………………………261
Using Tkinter……………………………………………………………………………………………………261
Saying “Hello” with PyGTK………………………………………………………………………………..265
Using Glade and tepache to Build Interfaces………………………………………………………..279
Jargon Busting…………………………………………………………………………………………………282
Summary………………………………………………………………………………………………………..282
■Index…………………………………………………………………………………………………………283

Reviews

There are no reviews yet.

Be the first to review “Python 3”

Your email address will not be published. Required fields are marked *

Skip to content