1. Trang chủ
  2. » Công Nghệ Thông Tin

LEARNING TO PROGRAM WITH PYTHON

283 3,6K 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 283
Dung lượng 3,28 MB

Nội dung

LEARNING TO PROGRAM WITH PYTHON Richard L. Halterman Copyright © 2011 Richard L. Halterman. All rights reserved. i Contents 1 The Context of Software Development 1 1.1 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Learning Programming with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Writing a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.5 A Longer Python program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2 Values and Variables 11 2.1 Integer Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.4 Floating-point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5 Control Codes within Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.6 User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.7 The eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.8 Controlling the print Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3 Expressions and Arithmetic 35 3.1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.2 Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.4 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 ©2011 Richard L. Halterman Draft date: November 13, 2011 CONTENTS ii 3.4.1 Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.4.2 Run-time Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.4.3 Logic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.5 Arithmetic Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.6 More Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.7 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4 Conditional Execution 57 4.1 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.3 The Simple if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.4 The if/else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.5 Compound Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.6 Nested Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4.7 Multi-way Decision Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 4.8 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.9 Errors in Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5 Iteration 81 5.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.2 Definite Loops vs. Indefinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.3 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.4 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.5 Abnormal Loop Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 5.5.1 The break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 5.5.2 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.6 Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.7 Iteration Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5.7.1 Computing Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.7.2 Drawing a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 5.7.3 Printing Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 ©2011 Richard L. Halterman Draft date: November 13, 2011 CONTENTS iii 5.7.4 Insisting on the Proper Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 6 Using Functions 115 6.1 Introduction to Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6.2 Standard Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 6.3 time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 6.4 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 6.5 Importing Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7 Writing Functions 133 7.1 Function Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 7.2 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 7.3 Main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 7.4 Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 7.5 Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 7.5.1 Better Organized Prime Generator . . . . . . . . . . . . . . . . . . . . . . . . . . 144 7.5.2 Command Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 7.5.3 Restricted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 7.5.4 Better Die Rolling Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 7.5.5 Tree Drawing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7.5.6 Floating-point Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 7.6 Custom Functions vs. Standard Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 7.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 8 More on Functions 161 8.1 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 8.2 Default Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 8.3 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 8.4 Making Functions Reusable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 8.5 Documenting Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 ©2011 Richard L. Halterman Draft date: November 13, 2011 CONTENTS iv 8.6 Functions as Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 8.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 9 Lists 181 9.1 Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 9.2 List Assignment and Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 9.3 List Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 9.4 Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 9.5 Lists and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 9.6 Prime Generation with a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 9.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 9.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 10 List Processing 207 10.1 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 10.2 Flexible Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 10.3 Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 10.3.1 Linear Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 10.3.2 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 10.4 List Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 10.5 Randomly Permuting a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 10.6 Reversing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 10.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 10.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 11 Objects 235 11.1 Using Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 11.2 String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 11.3 List Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 11.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 11.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 12 Custom Types 245 12.1 Geometric Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 12.2 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 ©2011 Richard L. Halterman Draft date: November 13, 2011 CONTENTS v 12.3 Custom Type Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 12.3.1 Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 12.3.2 Automated Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 12.4 Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 12.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 12.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 13 Handling Exceptions 267 13.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 13.2 Exception Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 13.3 Using Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 13.4 Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 13.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 13.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Index 273 ©2011 Richard L. Halterman Draft date: November 13, 2011 CONTENTS vi ©2011 Richard L. Halterman Draft date: November 13, 2011 vii Preface Legal Notices and Information This document is copyright ©2011 by Richard L. Halterman, all rights reserved. Permission is hereby granted to make hardcopies and freely distribute the material herein under the following conditions: • The copyright and this legal notice must appear in any copies of this document made in whole or in part. • None of material herein can be sold or otherwise distributed for commercial purposes without written permission of the copyright holder. • Instructors at any educational institution may freely use this document in their classes as a primary or optional textbook under the conditions specified above. A local electronic copy of this document may be made under the terms specified for hardcopies: • The copyright and these terms of use must appear in any electronic representation of this document made in whole or in part. • None of material herein can be sold or otherwise distributed in an electronic form for commercial purposes without written permission of the copyright holder. • Instructors at any educational institution may freely store this document in electronic form on a local server as a primary or optional textbook under the conditions specified above. Additionally, a hardcopy or a local electronic copy must contain the uniform resource locator (URL) providing a link to the original content so the reader can check for updated and corrected content. The current URL is http://python.cs.southern.edu/pythonbook/pythonbook.pdf ©2011 Richard L. Halterman Draft date: November 13, 2011 1 Chapter 1 The Context of Software Development A computer program, from one perspective, is a sequence of instructions that dictate the flow of electrical impulses within a computer system. These impulses affect the computer’s memory and interact with the display screen, keyboard, and mouse in such a way as to produce the “magic” that permits humans to perform useful tasks, solve high-level problems, and play games. One program allows a computer to assume the role of a financial calculator, while another transforms the machine into a worthy chess opponent. Note the two extremes here: • at the lower, more concrete level electrical impulses alter the internal state of the computer, while • at the higher, more abstract level computer users accomplish real-world work or derive actual plea- sure. So well is the higher-level illusion achieved that most computer users are oblivious to the lower-level activity (the machinery under the hood, so to speak). Surprisingly, perhaps, most programmers today write software at this higher, more abstract level also. An accomplished computer programmer can develop sophisticated software with little or no interest or knowledge of the actual computer system upon which it runs. Powerful software construction tools hide the lower-level details from programmers, allowing them to solve problems in higher-level terms. The concepts of computer programming are logical and mathematical in nature. In theory, computer programs can be developed without the use of a computer. Programmers can discuss the viability of a program and reason about its correctness and efficiency by examining abstract symbols that correspond to the features of real-world programming languages but appear in no real-world programming language. While such exercises can be very valuable, in practice computer programmers are not isolated from their machines. Software is written to be used on real computer systems. Computing professionals known as software engineers develop software to drive particular systems. These systems are defined by their underlying hardware and operating system. Developers use concrete tools like compilers, debuggers, and profilers. This chapter examines the context of software development, including computer systems and tools. ©2011 Richard L. Halterman Draft date: November 13, 2011 [...]... language Programmers have a variety of tools available to enhance the software development process Some common tools include: • Editors An editor allows the programmer to enter the program source code and save it to files Most programming editors increase programmer productivity by using colors to highlight language features The syntax of a language refers to the way pieces of the language are arranged to. .. Listing 1.2 (arrow.py) to be * *** ***** * * * If you try to enter each line one at a time into the IDLE interactive shell, the program s output will be intermingled with the statements you type In this case the best approach is to type the program into an editor, save the code you type to a file, and then execute the program Most of the time we use an editor to enter and run our Python programs The interactive... capabilities of today’s software Fortunately, programming languages provide a relatively simple structure with very strict rules for forming statements that can express a solution to any program that can be solved by a computer Consider the following program fragment written in the Python programming language: subtotal = 25 tax = 3 total = subtotal + tax These three lines do not make up a complete Python program; ... run with the IDLE interactive shell The editor allows us to save our programs and conveniently make changes to them later The editor understands the syntax of the Python language and uses different colors to highlight the various components that comprise a program Much of the work of program development occurs in the editor ©2011 Richard L Halterman Draft date: November 13, 2011 7 1.4 WRITING A PYTHON. .. PYTHON PROGRAM Figure 1.4: Launching the IDLE editor Figure 1.5: The simple Python program typed into the IDLE editor Figure 1.6: Saving a file created with the IDLE editor Listing 1.1 (simple.py) contains only one line of code: print("This is a simple Python program" ) This is a Python statement A statement is a command that the interpreter executes This statement prints the message This is a simple Python. .. for Python Despite the plethora of tools (and tool vendors’ claims), the programming process for all but trivial programs is not automatic Good tools are valuable and certainly increase the productivity of developers, but they cannot write software There are no substitutes for sound logical thinking, creativity, common sense, and, of course, programming experience 1.3 Learning Programming with Python. .. program This section introduces Python by providing a simple example program Listing 1.1 (simple.py) is one of the simplest Python programs that does something: Listing 1.1: simple.py 1 print("This is a simple Python program" ) Note: The small numbers that appear to the left of the box containing the Python code are not part of the program; the numbers are shown to allow us to easily reference a specific... transfer directly to other imperative programming languages such as Java, C#, and C++ We stick with the basics and explore more advanced features of Python only when necessary to handle the problem at hand 1.4 Writing a Python Program Python programs must be written with a particular structure The syntax must be correct, or the interpreter will generate error messages and not execute the program This section... from source code? 5 What tool does a programmer use to produce Python source code? 6 What is necessary to execute a Python program? 7 List several advantages developing software in a higher-level language has over developing software in machine language 8 How can an IDE improve a programmer’s productivity? 9 What the “official” Python IDE? 10 What is a statement in a Python program? ©2011 Richard L... program directly into IDLE and press enter to execute the program Figure 1.3 shows the result using the IDLE interactive shell Since it does not provide a way to save the code you enter, the interactive shell is not the best tool for writing larger programs The IDLE interactive shell is useful for experimenting with small snippets of Python code IDLE’s editor IDLE has a built in editor From the IDLE . best approach is to type the program into an editor, save the code you type to a file, and then execute the program. Most of the time we use an editor to enter and run our Python programs. The interactive. language. Programmers have a variety of tools available to enhance the software development process. Some common tools include: • Editors. An editor allows the programmer to enter the program source. common sense, and, of course, programming experience. 1.3 Learning Programming with Python Guido van Rossum created the Python programming language in the late 1980s. In contrast to other popular languages

Ngày đăng: 22/10/2014, 21:06

TỪ KHÓA LIÊN QUAN