Practical programming, 2nd edition

388 343 0
Practical programming, 2nd edition

Đ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

www.it-ebooks.info www.it-ebooks.info What Readers Are Saying About Practical Programming, Second Edition I wish I could go back in time and give this book to my 10-year-old self when I first learned programming! It’s so much more engaging, practical, and accessible than the dry introductory programming books that I tried (and often failed) to comprehend as a kid I love the authors’ hands-on approach of mixing explanations with code snippets that students can type into the Python prompt ➤ Philip Guo Creator of Online Python Tutor (www.pythontutor.com), Assistant Professor, Department of Computer Science, University of Rochester Practical Programming delivers just what it promises: a clear, readable, usable introduction to programming for beginners This isn’t just a guide to hacking together programs The book provides foundations to lifelong programming skills: a crisp, consistent, and visual model of memory and execution and a design recipe that will help readers produce quality software ➤ Steven Wolfman Senior Instructor, Department of Computer Science, University of British Columbia www.it-ebooks.info The second edition of this excellent text reflects the authors’ many years of experience teaching Python to beginning students Topics are presented so that each leads naturally to the next, and common novice errors and misconceptions are explicitly addressed The exercises at the end of each chapter invite interested students to explore computer science and programming language topics ➤ Kathleen Freeman Director of Undergraduate Studies, Department of Computer and Information Science, University of Oregon www.it-ebooks.info Practical Programming, 2nd Edition An Introduction to Computer Science Using Python Paul Gries Jennifer Campbell Jason Montojo The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC Every precaution was taken in the preparation of this book However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com The team that produced this book includes: Lynn Beighley (editor) Potomac Indexing, LLC (indexer) Molly McBeath (copyeditor) David J Kelly (typesetter) Janet Furlow (producer) Juliet Benda (rights) Ellie Callahan (support) Copyright © 2013 The Pragmatic Programmers, LLC All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher Printed in the United States of America ISBN-13: 978-1-93778-545-1 Encoded using the finest acid-free high-entropy binary digits Book version: P1.0—September 2013 www.it-ebooks.info Contents Acknowledgments Preface What’s Programming? 1.1 Programs and Programming 1.2 What’s a Programming Language? 1.3 What’s a Bug? 1.4 The Difference Between Brackets, Braces, and Parentheses 1.5 Installing Python Hello, Python 2.1 How Does a Computer Run a Python Program? 2.2 Expressions and Values: Arithmetic in Python 2.3 What Is a Type? 2.4 Variables and Computer Memory: Remembering Values 2.5 How Python Tells You Something Went Wrong 2.6 A Single Statement That Spans Multiple Lines 2.7 Describing Code 2.8 Making Code Readable 2.9 The Object of This Chapter 2.10 Exercises Designing and Using Functions 3.1 Functions That Python Provides 3.2 Memory Addresses: How Python Keeps Track of Values 3.3 Defining Our Own Functions 3.4 Using Local Variables for Temporary Storage 3.5 Tracing Function Calls in the Memory Model 3.6 Designing New Functions: A Recipe 3.7 Writing and Running a Program www.it-ebooks.info xi xiii 5 7 12 15 22 23 25 26 27 27 31 31 34 35 39 40 47 59 Contents 3.8 3.9 3.10 3.11 Omitting a Return Statement: None Dealing with Situations That Your Code Doesn’t Handle What Did You Call That? Exercises Working with Text 4.1 Creating Strings of Characters 4.2 Using Special Characters in Strings 4.3 Creating a Multiline String 4.4 Printing Information 4.5 Getting Information from the Keyboard 4.6 Quotes About Strings in This Text 4.7 Exercises 65 65 68 70 70 73 74 75 Making Choices 5.1 A Boolean Type 5.2 Choosing Which Statements to Execute 5.3 Nested If Statements 5.4 Remembering the Results of a Boolean Expression Evaluation 5.5 You Learned About Booleans: True or False? 5.6 Exercises 77 77 86 92 A Modular Approach to Program Organization 6.1 Importing Modules 6.2 Defining Your Own Modules 6.3 Testing Your Code Semiautomatically 6.4 Tips for Grouping Your Functions 6.5 Organizing Our Thoughts 6.6 Exercises Using Methods 7.1 Modules, Classes, and Methods 7.2 Calling Methods the Object-Oriented Way 7.3 Exploring String Methods 7.4 What Are Those Underscores? 7.5 A Methodical Review 7.6 Exercises 115 115 117 119 123 125 125 Storing Collections of Data Using Lists 8.1 Storing and Accessing Data in Lists 8.2 Modifying Lists 129 129 133 www.it-ebooks.info 60 61 62 63 • vi 92 94 94 99 100 104 109 112 113 114 Contents 8.3 8.4 8.5 8.6 8.7 8.8 8.9 Operations on Lists Slicing Lists Aliasing: What’s in a Name? List Methods Working with a List of Lists A Summary List Exercises Repeating Code Using Loops 9.1 Processing Items in a List 9.2 Processing Characters in Strings 9.3 Looping Over a Range of Numbers 9.4 Processing Lists Using Indices 9.5 Nesting Loops in Loops 9.6 Looping Until a Condition Is Reached 9.7 Repetition Based on User Input 9.8 Controlling Loops Using Break and Continue 9.9 Repeating What You’ve Learned 9.10 Exercises • vii 134 137 138 140 142 144 144 147 147 149 150 152 154 158 161 161 165 166 10 Reading and Writing Files 10.1 What Kinds of Files Are There? 10.2 Opening a File 10.3 Techniques for Reading Files 10.4 Files over the Internet 10.5 Writing Files 10.6 Writing Algorithms That Use the File-Reading Techniques 10.7 Multiline Records 10.8 Looking Ahead 10.9 Notes to File Away 10.10 Exercises 171 171 173 176 181 182 11 Storing Data Using Other Collection Types 11.1 Storing Data Using Sets 11.2 Storing Data Using Tuples 11.3 Storing Data Using Dictionaries 11.4 Inverting a Dictionary 11.5 Using the In Operator on Tuples, Sets, and Dictionaries 11.6 Comparing Collections www.it-ebooks.info 183 191 194 196 197 199 199 204 209 216 217 218 Contents 11.7 A Collection of New Information 11.8 Exercises 12 Designing Algorithms 12.1 Searching for the Smallest Values 12.2 Timing the Functions 12.3 At a Minimum, You Saw This 12.4 Exercises • viii 218 219 223 224 232 234 234 13 Searching and Sorting 13.1 Searching a List 13.2 Binary Search 13.3 Sorting 13.4 More Efficient Sorting Algorithms 13.5 Mergesort: A Faster Sorting Algorithm 13.6 Sorting Out What You Learned 13.7 Exercises 237 237 245 249 259 261 265 266 14 Object-Oriented Programming 14.1 Understanding a Problem Domain 14.2 Function “Isinstance,” Class Object, and Class Book 14.3 Writing a Method in Class Book 14.4 Plugging into Python Syntax: More Special Methods 14.5 A Little Bit of OO Theory 14.6 A Case Study: Molecules, Atoms, and PDB Files 14.7 Classifying What You’ve Learned 14.8 Exercises 269 270 271 274 280 282 288 292 293 15 Testing and Debugging 15.1 Why Do You Need to Test? 15.2 Case Study: Testing above_freezing 15.3 Case Study: Testing running_sum 15.4 Choosing Test Cases 15.5 Hunting Bugs 15.6 Bugs We’ve Put in Your Ear 15.7 Exercises 297 297 298 304 310 311 312 312 16 Creating Graphical User Interfaces 16.1 Using Module Tkinter 16.2 Building a Basic GUI 16.3 Models, Views, and Controllers, Oh My! 16.4 Customizing the Visual Style 317 317 319 323 327 www.it-ebooks.info Index frame type, 321 frame widget, 319 frames, defined, 41 function type, 104 function body, 37–38 function call, 31, 40, 62 function definition, 36, 62 function design recipe (FDR), 47 function header, 37 functions above_freezing, 299 abs, 31 AVG, 354 built-in, 31 click, 323 COUNT, 354 days_difference, 50 defining, 35 designing birthday-related, 49 designing new, 47 documentation for, 47 exercises, 63 float, 67 grouping, 112 help, 33, 100, 115 helper, 188 input, 73, 77, 161 int, 67 isinstance, 271 lambda, 324 len, 66 MAX, 354 memory addresses, 16, 34 merge, 262 MIN, 354 omitting return statements, 60 open, 173 pow, 34 print, 60, 69, 71, 147 Python, 31 range, 151 reversed, 177 round, 33 running programs, 59 running_sum, 305 sorted, 178 sqrt, 101 startswith, 121 str, 116, 271 terminology, 62 timing, 232 tracing function calls in memory model, 40 troubleshooting, 61 urlopen, 181 using local variables for temporary storage, 39 writing programs, 59 G global variables, 324 graphical user interface (GUI) about, 317, 336 building, 319 changing colors, 328 changing fonts, 327 controllers, 323 customizing visual style, 327 exercises, 336 getting information from users, 322 grouping widgets, 321 lambda functions, 324 models, 323 object-oriented, 335 tkinter module, 317 using mutable variables with widgets, 320 views, 323 widget layout, 330 widgets, 332 greater than (>) operator, 80, 299, 346 greater than or equal to (>=) operator, 80, 299, 346 GROOVE value, 322 GROUP BY clause, 355 grouping in databases, 355 functions, 112 widgets, 321 GUI, see graphical user interface (GUI) Guo, Philip, 17 H headers, skipping, 183 help function, 33, 100, 115 help documentation, 72, 117, 123 helper function, 188 I identifier, 16 IDLE, 9, 31, 59 if statement, 87, 92 if and only if (iff), 82 immutable list objects, 134 www.it-ebooks.info • 370 import statement, 100 import math statement, 101 importing modules, 100, 106 in operator about, 136 using on tuples, sets, and dictionaries, 217 inclusive or, 78 indentation in if statements, 86 index method, 237 indices processing lists using, 152 processing parallel lists using, 154 infinite loops, 160 inheritance, about, 284 init method, 277 inner joins, 350 input function, 73, 77, 161 insert method, 140 INSERT command, 343 insertion sort, 255 insort_left method, 249 insort_right method, 249 installing Python, 5, instance variables, 273 instances of classes, 271 int type, 10, 33 INTEGER data type, 342 integers, 10–11, 14 Internet, files over the, 181 interpreter, intersection method, 201 IntVar type, 320 invariant, of linear search, 239 inverting dictionaries, 216 isinstance function, 271 islower method, 120 isotope, 135 issubset method, 201 issuperset method, 201 isupper method, 120 iteration, 148 Index J Java, Boolean operators in, 80 Jmol, 288 joins, using to combine tables, 349 K keyboard, getting information from, 73 keys, 353 keyword arguments (kwargs), 73 keywords, Python, 38 kwargs, keyword arguments, 73 L label widget, 319 lambda functions, 324 layouts, 330 len function, 66 less than (

Ngày đăng: 28/08/2016, 12:43

Từ khóa liên quan

Mục lục

  • Cover

  • Table of Contents

  • Acknowledgments

  • Preface

    • Online Resources

    • 1. What's Programming?

      • Programs and Programming

      • What's a Programming Language?

      • What's a Bug?

      • The Difference Between Brackets, Braces, and Parentheses

      • Installing Python

      • 2. Hello, Python

        • How Does a Computer Run a Python Program?

        • Expressions and Values: Arithmetic in Python

        • What Is a Type?

        • Variables and Computer Memory: Remembering Values

        • How Python Tells You Something Went Wrong

        • A Single Statement That Spans Multiple Lines

        • Describing Code

        • Making Code Readable

        • The Object of This Chapter

        • Exercises

        • 3. Designing and Using Functions

          • Functions That Python Provides

Tài liệu cùng người dùng

Tài liệu liên quan