Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
21
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

Learning to Program

by

Alan Gauld

1

Introduction_____________________________________________7

Introduction - What, Why, Who etc.______________7

Why am I writing this? _____________________________________________7 What will I cover _________________________________________________7 Who should read it? _______________________________________________7 Why Python?_____________________________________________________7 Other resources ___________________________________________________8

Concepts ___________________________________________________9

What do I need?__________________________________________9

Generally________________________________________________________9 Python __________________________________________________________9 Tcl/Tk __________________________________________________________9 QBASIC _______________________________________________________10

What is Programming? _______________________________11

Back to BASICs _________________________________________________11 Let me say that again _____________________________________________11 A little history ___________________________________________________11 The common features of all programs ________________________________12 The structure of a program _________________________________________13

Getting Started __________________________________________15

A word about error messages_______________________15

The Basics_______________________________________________16

Simple Sequences _______________________________________16

Using Tcl_______________________________________________________18 And BASIC too... ________________________________________________19

2

The Raw Materials _____________________________________20

Introduction_____________________________________________________20 Data___________________________________________________________20 Variables _______________________________________________________20 Primitive Data Types _____________________________________________21 Character Strings_________________________________________________21 Integers ________________________________________________________23 Real Numbers ___________________________________________________24 Complex or Imaginary Numbers ____________________________________25 Boolean Values - True and False ____________________________________25 Collections _____________________________________________________26 Python Collections _______________________________________________26 Other Collection Types____________________________________________29 Files___________________________________________________________30 Dates and Times _________________________________________________30 Complex/User Defined ____________________________________________30 Python Specific Operators _________________________________________32

More information on the Address example ____________________________33

More Sequences and Other Things _______________35

The joy of being IDLE ____________________________________________35 A quick comment ________________________________________________35 Sequences using variables__________________________________________36 Order matters ___________________________________________________36 A Multiplication Table ____________________________________________37

Looping - Or the art of repeating oneself!______38

FOR Loops _____________________________________________________38 WHILE Loops___________________________________________________39 More Flexible Loops______________________________________________40 Looping the loop_________________________________________________41 Other loops _____________________________________________________41

Coding Style ______________________________________________42

Comments ______________________________________________________42 Indentation _____________________________________________________43 Variable Names__________________________________________________44 Modular Programming ____________________________________________44

3

Conversing with the user_____________________________46

>>> print raw_input("Type something: ") _____________________________46

A word about stdin and stdout _______________________________________47

Command Line Parameters_________________________49

Decisions, Decisions ____________________________________51

The if statement__________________________________________________51 Boolean Expressions______________________________________________52 Tcl branches ____________________________________________________52

Case statements__________________________________________53

Modular Programming _______________________________54

What's a Module? ________________________________________________54 Using Functions _________________________________________________54 Using Modules __________________________________________________55 Defining our own functions ________________________________________57 Tcl Functions ___________________________________________________59 Creating our own modules _________________________________________60

Handling Files and Text ______________________________62

Files - Input and Output ___________________________________________62 Counting Words _________________________________________________63 BASIC and Tcl __________________________________________________64

Handling Errors_________________________________________66

The Traditional Way ______________________________________________66 The Exceptional Way _____________________________________________66 Generating Errors ________________________________________________67 Tcl's Error Mechanism ____________________________________________68 BASIC Error Handling ____________________________________________68

4

Advanced Topics ____________________________________69

Recursion __________________________________________________69

What is it? ______________________________________________________69 Recursing over lists_______________________________________________69

Object Oriented Programming ____________________71

What is it? ______________________________________________________71 Data and Function - together________________________________________71 Defining Classes _________________________________________________72 Using Classes ___________________________________________________72 Same thing, Different thing_________________________________________73 Inheritance______________________________________________________73

Namespaces _______________________________________________77

Introduction_____________________________________________________77 Python's approach ________________________________________________77 And BASIC too__________________________________________________78 Tcl ____________________________________________________________78

Event Driven Programming_________________________79

Simulating an Event Loop _________________________________________79 A GUI program__________________________________________________80

GUI Programming with Tkinter __________________82

GUI principles___________________________________________________82 A Tour of Some Common Widgets __________________________________83 Exploring Layout ________________________________________________86 Controlling Appearance using Frames and the Packer ____________________87 Adding more widgets _____________________________________________88 Binding events - from widgets to code ________________________________89 A Short Message_________________________________________________89 The Tcl view ____________________________________________________90 Wrapping Applications as Objects ___________________________________90 An alternative - wxPython _________________________________________92

Functional Programming ____________________________94

What is Functional Programming? ___________________________________94 How does Python do it? ___________________________________________94 Other constructs _________________________________________________96 Conclusions_____________________________________________________98

5

Conclusions ___________________________________________100

A Case Study ____________________________________________100

Counting lines, words and characters ________________________________100 Counting sentences instead of lines _________________________________101 Turning it into a module __________________________________________102 Classes and objects ______________________________________________106 Text Document _________________________________________________108 HTML Document _______________________________________________109 Adding a GUI __________________________________________________110

References______________________________________________115

Books to read____________________________________________115

General Programming____________________________________________115 Object Oriented Programming _____________________________________116

Web sites to visit________________________________________117

Languages _____________________________________________________117 Programming in General__________________________________________117 Object Oriented Programming _____________________________________117

Projects to try ___________________________________________118

Topics for further study _____________________________118

6

Introduction

Introduction - What, Why, Who etc.

Why am I writing this?

The reason I am creating this tutorial is that there seems to be very little for the absolute beginner to programming on the Web. Yet the Internet and the Web encourage interest in computers and that interest naturally leads to a desire to "take control", which means learning to program!

Why me? Well I am a professional programmer who came to programming from an electronic engineering background. I have used (and continue to use) several computer languages and don't have any personal interest in promoting any particular tool or language. Oh, and nobody else seemed to be doing it!

What will I cover

As much as I can. I will cover the basic theory of computer programming - what it is, some of its history and the basic techniques needed to solve problems. I will not be teaching esoteric techniques or the details of any particular programming language, in fact I'll be using several different languages, since I believe its important to realize that different languages do different things well. That said, the majority of the course will be in the language called Python.

Who should read it?

Put another way: what do I expect the reader to know already?

I expect the reader of this tutorial to be an experienced user of a computer system, probably MS DOS, Windows or Unix although others should be able to cope too. I also expect them to understand basic mathematical concepts such as geometric coordinates, sets, and basic algebra. These are all important in todays programming environments, and many programming concepts are based on these ideas.

I certainly will not be covering issues like how to create or copy text files, how to install software, or the organization of files on a computer storage system. Frankly if you need to know those things you probably are not at the stage of being able to program, regardless of your desire to do so. Find a tutorial for your computer first, then when you're confident with the above concepts revisit.

Why Python?

Python happens to be a nice language to learn. Its syntax is simple and it has some very powerful features built into the language. It supports lots of programming styles from the very simple through to state of the art Object Oriented techniques. It runs on lots of platforms - Unix/Linux, MS Windows, Macintosh etc. It also has a very friendly and helpful user community. All of these are important features for a beginner's language.

Python however is not just a beginner's language. As your experience grows you can keep on using Python either as an end in itself or as a rapid prototyping language. There are a few things that Python is not well suited to, but these are comparatively few and far between.

I will also use BASIC for some of the very early examples then introduce Tcl as an alternative. Why? Well, if we accept that most Web surfers who are also beginners are using PCs with Microsoft Windows installed, there is a version of BASIC(QBASIC) already available on the CD ROM (either NT or Win 95/98). Tcl comes with versions of Python up to V1.5.2 (you effectively get two languages for the price of one - which in this case is nothing!) After version 2.0 you only get a minimal Tcl install so to do the examples you will need to download the official Tcl installer from Scriptics.

7

Other resources

There are other Web sites trying to do this in other languages. There are also lots of tutorials for those who already know how to program but want to learn a new language. This section contains links to some of those that I think are worthwhile!

The official Python language website with online documentation, latest downloads etc.

The official Perl web site - Perl is a natural competitor to Python in capability but is, I think, harder to learn.

The Tcl site. Tcl is the Tool Control Language and has some interesting features. It has also been responsible for two of the most important innovations in automation for computers: expect and Tk. Note that the Tcl site keeps changing name, if the link doesn't work just search for Tcl/Tk on any search engine.

Java and Javascript. are the 2 most common languages for the Web and have superficial similarities. (Actually Perl is used heavily on the Web too, but only at the server end)

There is another good 'get you started' Python specific tutorial by Josh Cogliari plus a very short and fast taster on the Python web site under Documentation|Introductions. They both feature teaching by example with only brief descriptions of the concepts. If you only want to get up and running in Python then they might be worth a visit and come back here when you want a deeper insight to programming in general.

If you don't much like my style a web site with similar aims is the How to think like a Computer Scientist produced by Jeff Elkner who uses Python in his high School classes. It seems a little bit less comprehensive than mine, but maybe I'm just biased :-)

8