Xem mẫu

tutor Alan Gauld 27 May 2007 tutor Table of Contents Learning to program Learning to program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 What do I need to be a programmer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 What is programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Simple sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 More Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Looping the loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 A Little Bit of Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Text Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Event Driven Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Introduction to GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Introduction to Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 A Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Python in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Working with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Working with the Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Inter-Process Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Inter-Process Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 May 27, 2007 Learning to program Contents Introduction Concepts What do I need? 27/05/2007 Learning to Program by Alan Gauld Stop Press! Last Update: 27th May 2007 The Web Site What is `Programming`? Getting Started The Basics Simple Sequences The Raw Materials More Sequences Loops Add a little style Talking to the user Branching Modules & Functions Handling Files Handling Text Error Handling Advanced Topics Welcome to the Learning to Program web tutor! I`ve been very busy in my day-job over the last year so very little progress has been made, however I am working on the first web programming topic. Meantime there have been several tweaks to the existing topics, including many minor bug fixes and some changes to the colour scheme to reducethe contrast which some folks felt was a bit glaring. The original version of the tutor using TCl and QBASIC is still available here. The Book Latest news on my book is that it has moved to print-on-demand. Basically this means that copies are only printed when firm orders are received from retailers. Logically this means you are less likely to see it in the shops but you can still place an order and it will be delivered, it just takes a little longer is all. The book is now significantly different to the new web site although the basics remain the same. It is stuck at Python version 1.5 and doesn`t have the new material, although it has extra examples of its own plus several extra chapters compared to the web site. These include an additional case study for an OO games framework. (A fully commented version of the framework is available on the old Useless Python website as hmgui.zip) You can order the book on Amazon by clicking here. Most of the other online bookshops have it in stock too. There is also a Japanese version available from the Japanese branch of my publishers. I now have a web site set up listing errata and providing opportunities to send feedback and questions. Please pay a visit if you`ve bought the book! Page 1 Learning to program What`s in a name? 27/05/2007 The Future Regular Expressions Object Oriented Programming Event Driven Programming GUI Programming Recursion - or doing it to yourself Functional Programming A Case Study Applications Python in Practice As I said above I am still adding new topics added as part of a new section looking at practical applications of programming. This is in response to many requests from readers who say - "OK, Now I know how to program, but what exactly can I do with my new found skills?" These topics do not feature any VBScript or JavaScript. There is a Polish translation underway and the Czech version has been updated to reflect the latest version. As ever, Petr has been my most exacting reviewer and I thank him for his many corrections. Any remaining errors are entirely my fault! The book is stagnant I`m afraid, but once I get the new topics written I might sound the publishers out about a second edition updating it to the latest version of Python, whatever that will be by then, and adding the new material. But don`t hold your breath waiting on that one... Non English Editions now available! Martin Pozzi was the first to translate the tutor (into Spanish) and he has now been followed by several others. The following versions are available: Language Translator Czech version Petr P Working with Databases Using the Operating System German Korean Italian Portuguese Spanish Bruno Schaefer johnsonj Luca Fini Wilson Edgar Martin Pozzi Inter-process communications Network programming Writing web clients Writing Web Applications The tutorials above are all usable but at different stages of completeness compared to the web site. That`s because they reflect the state of the web site at the time of translation. The Czech one in particular is very professionally presented including a very attractive appearance. I was so impressed with the style that I have used Petr`s stylesheets as the basis for the English version. Hopefully I`ll get round to changing the other language editions too. Page 2 Learning to program 27/05/2007 Parallel processing Appendices References, Books and Projects I think all of the translators deserve a vote of thanks for their efforts in putting this together. It`s always better to read a translation of an old tutorial than to struggle reading a new one in a foreign language! If anyone else wishes to do a translation, or update any of the existing ones, just send me an email. As mentioned above there is a Japanese version of my book available too. New Stuff Download the whole thing in TGZ format Or in ZIP format. Or in PDF format. Send Feedback Not much because of the PC problems mentioned above! Download Archive Format Change I`ll be posting both zip and tgz versions of the site from time to time. I don`t promise that they will be perfectly up to date but I`ll try not to let them get too far adrift. If you want the latest then check the online version. Linux users can download the tar/gzip version. Windows users should be able to use that version too since winzip and the freeware archiver Quick Zip by Joseph Leung can both cope with tgz format. I`ll also be using tgz format for the non English versions, just look at the bottom of the contents frame for a link. Finally I have made a basic PDF version available too, it doesn`t have any fancy navigation features like bookmarks but if you like PDF format it`s here. And Palm Users Too... One Palm user has created a Palm doc file of the old site. I haven`t had a chance to try it yet but offer it here for those who want it. If anyone has problems with this one then you`re on your own, it`s unsupported. But hopefully it will be useful to someone. If you don`t have a Palm doc reader you can find a choice of freeware ones at this site. Page 3 ... - tailieumienphi.vn
nguon tai.lieu . vn