ebook img

Learning to Program using Python PDF

215 Pages·2011·0.761 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learning to Program using Python

Learning to Program Using Python Cody Jackson 2 Copyright©2009-2011CodyJackson. Thisworkislicensedunderthe Creative Commons Attribution-ShareAlike 3.0 Unported License. To viewacopyofthislicense,visithttp://creativecommons.org/licenses/by- sa/3.0/orsendalettertoCreativeCommons,444CastroStreet,Suite 900, Mountain View, California, 94041, USA. The source code within this text is licensed under the GNU General PublicLicense(GPL).Thefollowinglicenseinformationcoversallcode contained herein, except those that explicitly state otherwise: Copyright © 2006-2011 Cody Jackson. This program is free software: you can redistribute it and/or modify it un- der the terms of the GNU General Public License as pub- lishedbytheFreeSoftwareFoundation, eitherversion3of the License, or (at your option) any later version. Thisprogramisdistributedinthehopethatitwillbeuse- ful,butWITHOUTANYWARRANTY;withouteventhe implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. YoushouldhavereceivedacopyoftheGNUGeneralPub- licLicensealongwiththisprogram. Ifnot,seehttp://www.gnu.org/licenses/. More information about this book, as well as source code files and to contacttheauthor,canbefoundonlineathttp://python-ebook.blogspot.com. For those curious, this book was initially drafted on a Mac using Scrivener (http://www.literatureandlatte.com/). It was imported to LYX (http://www.lyx.org) for editing, typesetting, page layout, and other “book writing” tasks. LATEX (http://www.latex-project.org/) was used to create the PDF and HTML versions of this book. Contents I The Core Language 8 1 Introduction 9 1.1 Why Python? . . . . . . . . . . . . . . . . . . . . . . . 10 1.2 Why Another Tutorial? . . . . . . . . . . . . . . . . . . 11 1.3 Getting Python . . . . . . . . . . . . . . . . . . . . . . 12 1.4 Conventions Used in this Book . . . . . . . . . . . . . . 13 2 How is Python Different? 14 2.1 Python Concepts . . . . . . . . . . . . . . . . . . . . . . 14 2.1.1 Dynamic vs Static Types . . . . . . . . . . . . . 14 2.1.2 Interpreted vs. Compiled . . . . . . . . . . . . . 15 2.1.3 Prototyping . . . . . . . . . . . . . . . . . . . . . 16 2.1.4 Procedural vs. Object-Oriented Programming . . 17 3 Comparison of Programming Languages 18 3.1 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.2 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3.3 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.4 C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.5 Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4 The Python Interpreter 29 4.1 Launching the Python interpreter . . . . . . . . . . . . . 29 4.1.1 Windows . . . . . . . . . . . . . . . . . . . . . . 30 4.1.2 Mac . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.2 Python Versions . . . . . . . . . . . . . . . . . . . . . . 31 3 CONTENTS 4 4.3 Using the Python Command Prompt . . . . . . . . . . . 32 4.4 Commenting Python . . . . . . . . . . . . . . . . . . . . 33 4.5 Launching Python programs . . . . . . . . . . . . . . . . 33 4.6 Integrated Development Environments . . . . . . . . . . 34 5 Types and Operators 36 5.1 Python Syntax . . . . . . . . . . . . . . . . . . . . . . . 36 5.1.1 Indentation . . . . . . . . . . . . . . . . . . . . . 36 5.1.2 Multiple Line Spanning . . . . . . . . . . . . . . 37 5.2 Python Object Types . . . . . . . . . . . . . . . . . . . 38 5.3 Python Numbers . . . . . . . . . . . . . . . . . . . . . . 39 6 Strings 42 6.1 Basic string operations . . . . . . . . . . . . . . . . . . . 43 6.2 Indexing and slicing strings . . . . . . . . . . . . . . . . 45 6.3 String Formatting . . . . . . . . . . . . . . . . . . . . . 46 6.4 Combining and Separating Strings . . . . . . . . . . . . 48 6.5 Regular Expressions . . . . . . . . . . . . . . . . . . . . 50 7 Lists 51 7.1 List usage . . . . . . . . . . . . . . . . . . . . . . . . . . 52 7.2 Adding List Elements . . . . . . . . . . . . . . . . . . . 53 7.3 Mutability . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7.4 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 8 Dictionaries 58 8.1 Making a dictionary . . . . . . . . . . . . . . . . . . . . 59 8.2 Basic operations . . . . . . . . . . . . . . . . . . . . . . 60 8.3 Dictionary details . . . . . . . . . . . . . . . . . . . . . . 62 8.4 Operation . . . . . . . . . . . . . . . . . . . . . . . . . . 62 9 Tuples 63 9.1 Why Use Tuples? . . . . . . . . . . . . . . . . . . . . . . 64 9.2 Sequence Unpacking . . . . . . . . . . . . . . . . . . . . 65 9.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 CONTENTS 5 10 Files 68 10.1 File Operations . . . . . . . . . . . . . . . . . . . . . . . 69 10.2 Files and Streams. . . . . . . . . . . . . . . . . . . . . . 69 10.3 Creating a File . . . . . . . . . . . . . . . . . . . . . . . 70 10.4 Reading From a File . . . . . . . . . . . . . . . . . . . . 71 10.5 Iterating Through Files . . . . . . . . . . . . . . . . . . 74 10.6 Seeking . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 10.7 Serialization . . . . . . . . . . . . . . . . . . . . . . . . . 76 11 Statements 78 11.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . 79 11.2 Expressions/Calls . . . . . . . . . . . . . . . . . . . . . . 80 11.3 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 11.4 if Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 11.5 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . 84 11.6 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 85 11.7 pass Statement . . . . . . . . . . . . . . . . . . . . . . . 87 11.8 break and continue Statements . . . . . . . . . . . . . . 88 11.9 try, except, finally and raise Statements . . . . . . . . . 88 11.10import and from Statements. . . . . . . . . . . . . . . . 88 11.11def and return Statements. . . . . . . . . . . . . . . . . 88 11.12Class Statements . . . . . . . . . . . . . . . . . . . . . . 89 12 Documenting Your Code 90 13 Making a Program 97 13.1 Making Python Do Something . . . . . . . . . . . . . . 98 13.2 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 13.3 Default Arguments . . . . . . . . . . . . . . . . . . . . . 102 14 Exceptions 104 14.1 Exception Class Hierarchy . . . . . . . . . . . . . . . . 107 14.2 User-Defined Exceptions . . . . . . . . . . . . . . . . . 109 15 Object Oriented Programming 111 15.1 Learning Python Classes . . . . . . . . . . . . . . . . . . 111 15.2 How Are Classes Better? . . . . . . . . . . . . . . . . . . 112 15.3 Improving Your Class Standing . . . . . . . . . . . . . . 112 CONTENTS 6 15.4 So What Does a Class Look Like? . . . . . . . . . . . . 114 15.5 “New-style” classes . . . . . . . . . . . . . . . . . . . . . 116 15.6 A Note About Style . . . . . . . . . . . . . . . . . . . . 117 16 More OOP 118 16.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . 118 16.2 Operator Overloads . . . . . . . . . . . . . . . . . . . . 120 16.3 Class Methods . . . . . . . . . . . . . . . . . . . . . . . 122 16.4 Have you seen my class? . . . . . . . . . . . . . . . . . . 123 17 Databases 125 17.1 How to Use a Database . . . . . . . . . . . . . . . . . . 126 17.2 Working With a Database . . . . . . . . . . . . . . . . 126 17.3 Using SQL to Query a Database . . . . . . . . . . . . . 127 17.4 Python and SQLite . . . . . . . . . . . . . . . . . . . . . 129 17.5 Creating an SQLite DB . . . . . . . . . . . . . . . . . . 129 17.6 Pulling Data from a DB . . . . . . . . . . . . . . . . . . 131 17.7 SQLite Database Files . . . . . . . . . . . . . . . . . . . 134 18 Distributing Your Program 136 19 Python 3 138 II Graphical User Interfaces 140 20 Overview of Graphical User Interfaces 141 20.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 141 20.2 Popular GUI Frameworks . . . . . . . . . . . . . . . . . 142 20.3 Before You Start . . . . . . . . . . . . . . . . . . . . . . 144 21 A Simple Graphical Dice Roller 146 22 What Can wxPython Do? 152 A String Methods 154 B List Methods 160 CONTENTS 7 C Dictionary operations 162 D Operators 166 E Sample programs 168 E.1 Dice rolling simulator . . . . . . . . . . . . . . . . . . . 168 E.2 Temperature conversion . . . . . . . . . . . . . . . . . . 172 E.3 Game character attribute generator. . . . . . . . . . . . 173 E.4 Text-based character creation . . . . . . . . . . . . . . . 176 F GNU General Public License 197 Part I The Core Language 8 Chapter 1 Introduction I originally wanted to learn Python because I wanted to make a com- puter game. I had taken several programming classes in college (C, C++, and Java) but nothing really serious. I’m not a Computer Sci- ence major and I don’t program on a professional level. Ididn’treallylikethelow-levelworkinvolvedwithC/C++. Things like pointers, memory management, and other concepts were difficult for me to grasp, much less effectively use. Java, as my first pro- gramming class in school, didn’t make any sense. I had never used an object-oriented language before and object-oriented programming (OOP) concepts gave me fits. It probably didn’t help that my Java class wasn’t actually real Java; it was actually Microsoft’s “custom” version: J++. So not only was I learning a language that had little practical use (J++ added and cut many features found in real Java), but the programs didn’t work correctly. Ultimately the class was can- celed near the end of the semester and everyone received full credit. Theseproblems,andissueslearningotherprogramminglanguages, left a bad taste in my mouth for programming. I never thought I learned a language well enough to feel comfortable using it, much less actually enjoy programming. But then I heard about Python on a computer forum, and noticed several other mentions of the language at other sites around the Internet. People were talking about how great the language was for personal projects and how versatile it is. I decided to give programming one more try and see if Python was the 9

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.