- •Introduction
- •Introduction - What, Why, Who etc.
- •Why am I writing this?
- •What will I cover
- •Who should read it?
- •Why Python?
- •Other resources
- •Concepts
- •What do I need?
- •Generally
- •Python
- •QBASIC
- •What is Programming?
- •Back to BASICs
- •Let me say that again
- •A little history
- •The common features of all programs
- •Let's clear up some terminology
- •The structure of a program
- •Batch programs
- •Event driven programs
- •Getting Started
- •A word about error messages
- •The Basics
- •Simple Sequences
- •>>> print 'Hello there!'
- •>>>print 6 + 5
- •>>>print 'The total is: ', 23+45
- •>>>import sys
- •>>>sys.exit()
- •Using Tcl
- •And BASIC too...
- •The Raw Materials
- •Introduction
- •Data
- •Variables
- •Primitive Data Types
- •Character Strings
- •String Operators
- •String operators
- •BASIC String Variables
- •Tcl Strings
- •Integers
- •Arithmetic Operators
- •Arithmetic and Bitwise Operators
- •BASIC Integers
- •Tcl Numbers
- •Real Numbers
- •Complex or Imaginary Numbers
- •Boolean Values - True and False
- •Boolean (or Logical) Operators
- •Collections
- •Python Collections
- •List
- •List operations
- •Tcl Lists
- •Tuple
- •Dictionary or Hash
- •Other Collection Types
- •Array or Vector
- •Stack
- •Queue
- •Files
- •Dates and Times
- •Complex/User Defined
- •Accessing Complex Types
- •User Defined Operators
- •Python Specific Operators
- •More information on the Address example
- •More Sequences and Other Things
- •The joy of being IDLE
- •A quick comment
- •Sequences using variables
- •Order matters
- •A Multiplication Table
- •Looping - Or the art of repeating oneself!
- •FOR Loops
- •Here's the same loop in BASIC:
- •WHILE Loops
- •More Flexible Loops
- •Looping the loop
- •Other loops
- •Coding Style
- •Comments
- •Version history information
- •Commenting out redundant code
- •Documentation strings
- •Indentation
- •Variable Names
- •Modular Programming
- •Conversing with the user
- •>>> print raw_input("Type something: ")
- •BASIC INPUT
- •Reading input in Tcl
- •A word about stdin and stdout
- •Command Line Parameters
- •Tcl's Command line
- •And BASIC
- •Decisions, Decisions
- •The if statement
- •Boolean Expressions
- •Tcl branches
- •Case statements
- •Modular Programming
- •What's a Module?
- •Using Functions
- •BASIC: MID$(str$,n,m)
- •BASIC: ENVIRON$(str$)
- •Tcl: llength L
- •Python: pow(x,y)
- •Python: dir(m)
- •Using Modules
- •Other modules and what they contain
- •Tcl Functions
- •A Word of Caution
- •Creating our own modules
- •Python Modules
- •Modules in BASIC and Tcl
- •Handling Files and Text
- •Files - Input and Output
- •Counting Words
- •BASIC and Tcl
- •BASIC Version
- •Tcl Version
- •Handling Errors
- •The Traditional Way
- •The Exceptional Way
- •Generating Errors
- •Tcl's Error Mechanism
- •BASIC Error Handling
- •Advanced Topics
- •Recursion
- •Note: This is a fairly advanced topic and for most applications you don't need to know anything about it. Occasionally, it is so useful that it is invaluable, so I present it here for your study. Just don't panic if it doesn't make sense stright away.
- •What is it?
- •Recursing over lists
- •Object Oriented Programming
- •What is it?
- •Data and Function - together
- •Defining Classes
- •Using Classes
- •Same thing, Different thing
- •Inheritance
- •The BankAccount class
- •The InterestAccount class
- •The ChargingAccount class
- •Testing our system
- •Namespaces
- •Introduction
- •Python's approach
- •And BASIC too
- •Event Driven Programming
- •Simulating an Event Loop
- •A GUI program
- •GUI Programming with Tkinter
- •GUI principles
- •A Tour of Some Common Widgets
- •>>> F = Frame(top)
- •>>>F.pack()
- •>>>lHello = Label(F, text="Hello world")
- •>>>lHello.pack()
- •>>> lHello.configure(text="Goodbye")
- •>>> lHello['text'] = "Hello again"
- •>>> F.master.title("Hello")
- •>>> bQuit = Button(F, text="Quit", command=F.quit)
- •>>>bQuit.pack()
- •>>>top.mainloop()
- •Exploring Layout
- •Controlling Appearance using Frames and the Packer
- •Adding more widgets
- •Binding events - from widgets to code
- •A Short Message
- •The Tcl view
- •Wrapping Applications as Objects
- •An alternative - wxPython
- •Functional Programming
- •What is Functional Programming?
- •How does Python do it?
- •map(aFunction, aSequence)
- •filter(aFunction, aSequence)
- •reduce(aFunction, aSequence)
- •lambda
- •Other constructs
- •Short Circuit evaluation
- •Conclusions
- •Other resources
- •Conclusions
- •A Case Study
- •Counting lines, words and characters
- •Counting sentences instead of lines
- •Turning it into a module
- •getCharGroups()
- •getPunctuation()
- •The final grammar module
- •Classes and objects
- •Text Document
- •HTML Document
- •Adding a GUI
- •Refactoring the Document Class
- •Designing a GUI
- •References
- •Books to read
- •Python
- •BASIC
- •General Programming
- •Object Oriented Programming
- •Other books worth reading are:
- •Web sites to visit
- •Languages
- •Python
- •BASIC
- •Other languages of interest
- •Programming in General
- •Object Oriented Programming
- •Projects to try
- •Topics for further study
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