Tài liệu Programming for Non-Programmers ppt

491 404 0
Tài liệu Programming for Non-Programmers ppt

Đ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

Programming for Non-Programmers Release 2.6.2 Steven F. Lott January 23, 2012 CONTENTS I How To Write Your Own Software Using Python 1 1 Preface 5 1.1 Why Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2 What Is This Book About? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2 Getting Started 11 2.1 About Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 About Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.3 Let There Be Python: Downloading and Installing . . . . . . . . . . . . . . . . . . . . . . . . 24 2.4 Two Minimally-Geeky Problems : Examples of Things Best Done by Customized Software . 30 2.5 Why Python is So Cool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3 Using Python 39 3.1 Instant Gratification : The Simplest Possible Conversation . . . . . . . . . . . . . . . . . . . 39 3.2 IDLE Time : Using Tools To Be More Productive . . . . . . . . . . . . . . . . . . . . . . . . 48 4 Arithmetic and Expressions 55 4.1 Simple Arithmetic : Numbers and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.2 Better Arithmetic Through Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.3 Extra Functions: math and random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.4 Special Ops : Binary Data and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 4.5 More Advanced Expression Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5 Programming Essentials 95 5.1 Seeing Results : The print Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.2 Turning Python Loose With a Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5.3 Expressions, Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 5.4 Assignment Bonus Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5.5 Can We Get Your Input? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 6 Some Self-Control 117 6.1 Truth and Logic : Boolean Data and Operators . . . . . . . . . . . . . . . . . . . . . . . . . 117 6.2 Making Decisions : The Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 123 6.3 Advanced Logic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 6.4 Processing Only When Necessary : The if Statement . . . . . . . . . . . . . . . . . . . . . . 130 6.5 While We Have More To Do : The for Statement . . . . . . . . . . . . . . . . . . . . . . . . 137 6.6 While We Have More To Do : The while Statement . . . . . . . . . . . . . . . . . . . . . . . 144 6.7 Becoming More Controlling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 i 6.8 Comments and Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 7 Organizing Programs with Function Definitions 165 7.1 Adding New Verbs : The def Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 7.2 Flexibility and Clarity : Optional Parameters, Keyword Arguments . . . . . . . . . . . . . . 177 7.3 A Few More Function Definition Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 8 Getting Our Bearings 197 8.1 Where We’ve Been; Where We’re Going . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 9 Basic Sequential Collections of Data 201 9.1 Collecting Items in Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 9.2 Sequences of Characters : str and Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 9.3 Doubles, Triples, Quadruples : The tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 9.4 Flexible Sequences : The list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9.5 Common List Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 10 Additional Processing Control Patterns 263 10.1 The Unexpected : The try and except statements . . . . . . . . . . . . . . . . . . . . . . . 263 10.2 Looping Back : Iterators, the for statement and Generators . . . . . . . . . . . . . . . . . . 277 11 More Data Collections 293 11.1 Collecting Items : The set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 11.2 Mappings : The dict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 11.3 Defining More Flexible Functions with Mappings . . . . . . . . . . . . . . . . . . . . . . . . . 322 11.4 Another Mapping : The defaultdict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 12 Working with Files 335 12.1 External Data and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 12.2 Files, Contexts and Patterns of Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 12.3 File-Related Library Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 13 Data + Processing = Objects 365 13.1 Objects: A Retrospective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 13.2 Defining New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 14 Modules : The unit of software packaging and assembly 387 14.1 Module Definitions – Adding New Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 14.2 Fixed-Point Numbers : Doing High Finance with decimal . . . . . . . . . . . . . . . . . . . 402 14.3 Time and Date Processing : The time and datetime Modules . . . . . . . . . . . . . . . . . 407 14.4 Text Processing and Pattern Matching : The re Module . . . . . . . . . . . . . . . . . . . . 422 15 Fit and Finish: Complete Programs 433 15.1 Wrapping and Packaging Our Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 15.2 Architectural Patterns – A Family Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 15.3 Professionalism : Additional Tips and Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 16 Appendices 455 16.1 Debugging Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 16.2 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 16.3 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 17 Indices and Tables 479 18 Other Back Matter 481 ii Bibliography 483 Python Module Index 485 iii iv Part I How To Write Your Own Software Using Python 1 Programming for Non-Programmers, Release 2.6.2 Legal Notice This work is licensed under a Creative Commons License. You are free to copy, distribute, display, and perform the work under the following conditions: • Attribution. You must give the original author, Steven F. Lott, credit. • Noncommercial. You may not use this work for commercial purposes. • No Derivative Works. You may not alter, transform, or build upon this work. For any reuse or distribution, you must make clear to others the license terms of this work. The Walrus and the Carpenter – Lewis Carroll “The time has come,” the Walrus said, “To talk of many things: Of shoes – and ships – and sealing-wax – Of cabbages – and kings – And why the sea is boiling hot – and whether pigs have wings.” 3 Programming for Non-Programmers, Release 2.6.2 4 [...]... directory names: bin is short for binary, as in binary executable 2.2.5 So How Do They Create Binary Executables? This is a digression for the curious It may help you understand how the team that wrote the Python program did it It can help you demystify programming It may not help you learn the Python language, so feel free to skip it 2.2 About Programming 21 Programming for Non-Programmers, Release 2.6.2... The language is central However, techniques for debugging are almost as important as the language itself 2.2 About Programming 19 Programming for Non-Programmers, Release 2.6.2 Other skills in include testing and problem analysis Testing is a rich subject; it would double the size of this book to talk about appropriate testing techniques The analytical skills for inception and elaboration don’t require... Of Programming Designing a program is mostly an intellectual exercise It can be done with pencils and paper or sophisticated software modeling tools For the most part, pencil-and-paper design is still the best and most common way to design software Often, the fancy modeling tools are used to fill in details and create a document ready for publication 20 Chapter 2 Getting Started Programming for Non-Programmers, ... For people very new to programming, this raises further questions like “what is a programming language?” and “why can’t it just use English?” and “what if I’m not good with languages?” We’ll return to these topics in Concepts FAQ’s For now, we’ll emphasize the point that the Python language is more precise than English, but still very easy to read and write 11 Programming for Non-Programmers, Release... ask for the properties of a document icon If it is a short cut, you can use the Find Target button to locate the real application file With MacOS, you can use control-click to get information on a particular icon The properties name the application that is associated with this document In Windows, you can see the Type of File and Opens With information about the file Using MacOS, you can ask for information... successfully 8 Chapter 1 Preface Programming for Non-Programmers, Release 2.6.2 We will use the following type styles for references to a specific Class, method(), attribute, which includes both class variables or instance variables Sidebars When we do have a digression, it will appear in a sidebar, like this Tip: Tips There will be design tips, and warnings, in the material for each exercise These reflect... programmers 1.4 Conventions Used in This Book 9 Programming for Non-Programmers, Release 2.6.2 10 Chapter 1 Preface CHAPTER TWO GETTING STARTED Tools and Toys This part provides some necessary background to help non -programming newbies get ready to write their own programs If you have good computer skills, this section may be all review If you are new to programming, our objective is to build up your... condition which is so trivial that we write the first statement and we’re done with the programming effort 2.2.4 Programming Exercises 1 Operating System and Platform To successfully download and install software on your computer, you’ll need to know your operating system information It can also help to know your processor chip information • Windows You get to the control panels with Start Settings Control Panel... wrote a tone poem for chamber orchestra” and “The chamber orchestra’s tone poem was written by Xander” have the same meaning, but express it different ways They have the same semantics, but different syntax For example, in one sentence the verb is “wrote”, in the other sentence it is “was written by” : different forms of the verb to write The first form is written in active voice, and second form is called... The answer is to click Run First, you’ll be asked if you want to install for all users or just yourself You require administrator privileges to install for all users If you’re using a corporate PC, for example, you might not have administrator privileges If you have the privileges, then install for all users Otherwise, install for yourself Click Next to continue The next step is to select a destination . internalize each 1.3. Audience 7 Programming for Non-Programmers, Release 2.6.2 language concept. There is no substitute for hands-on use of Python. You’ll. successfully 8 Chapter 1. Preface Programming for Non-Programmers, Release 2.6.2 We will use the following type styles for references to a specific Class,

Ngày đăng: 22/02/2014, 05:20

Mục lục

  • I How To Write Your Own Software Using Python

    • Preface

      • Why Read This Book?

      • What Is This Book About?

      • Audience

      • Conventions Used in This Book

      • Getting Started

        • About Python

        • About Programming

        • Let There Be Python: Downloading and Installing

        • Two Minimally-Geeky Problems : Examples of Things Best Done by Customized Software

        • Why Python is So Cool

        • Using Python

          • Instant Gratification : The Simplest Possible Conversation

          • IDLE Time : Using Tools To Be More Productive

          • Arithmetic and Expressions

            • Simple Arithmetic : Numbers and Operators

            • Better Arithmetic Through Functions

            • Extra Functions: math and random

            • Special Ops : Binary Data and Operators

            • More Advanced Expression Topics

            • Programming Essentials

              • Seeing Results : The print Statement

              • Turning Python Loose With a Script

              • Expressions, Constants and Variables

              • Assignment Bonus Features

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

  • Đang cập nhật ...

Tài liệu liên quan