
- •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

As you see its very similar to Python except you put the variable at the end. Also BASIC uses INPUT for both numbers and strings. There are usually a few extra features in BASIC's INPUT statement. You should look at the documentation for your particular version.
Reading input in Tcl
Tcl has its own input mechanism, which is based around files (which may include the standard input and output 'files') and a command called gets. This reads input from the specified file which in our case will be stdin.
[ Note: This program will not work from the standard tclsh80 or wish80 prompt. Instead you will need to type it into a file (say input.tcl) and run it from the command prompt like so:
C:\PROJECTS\Tcl>tclsh80 input.tcl
]
The Tcl version of our program looks like this:
puts -nonewline stdout "What multiplier do you want? " flush stdout
set mult [gets stdin]
for {set j 1} {$j <= 12} {incr j} {
puts [format " %d x %d = %d" $j $mult [expr $mult * $j] ]
}
The -nonewline option to puts simply prevents the cursor from moving to the next line after displaying the prompt message. flush forces stdout to write its contents immediately to ensure that it appears on screen. The for loop is almost identical to the version we saw in the loops section.
A word about stdin and stdout
NOTE: stdin is a bit of jargon for the standard input device (usually the keyboard). It is made to look like a file (we'll get to those shortly) for consistency with file handling code.
In Python it lives in the sys module and is called sys.stdin and raw_input() uses it automatically. Tcl can read from any file using gets (short for getstring). You can do the same in Python, try this:
import sys
print "Type a value: ", # comma prevents newline value = sys.stdin.readline() # use stdin explicitly print value
It is almost identical to:
print raw_input(Type a value: )
The advantage of the explicit vesion is that you can do fancy things like make stdin point to a real file so the program reads its input from the file rather than the terminal - this can be useful for long testing sessions wherby instead of sitting typing each input as requested we simply let the program read its input from a file. [ This has the added advantage of ensuring that we can run the test repeatedly, sure that the input will be exactly the same eaach time, and so hopefully will the output. This technique of repeating previous tests to ensure that nothing got broken is called regression testing by programmers. ]
Finally there is also a sys.stdout 'file' that can likewise be redirected, this time to a file. print is equivalent to:
sys.stdout.write("Hello world\n") # \n= newline
47

Obviously if stdout did not refer to the screen then the output would be written to a file. This is how the operating system commands work when we use redirection at the command prompt:
C:> dir
C:> dir > dir.txt
The first command prints a directory listing to the screen. The second prints it to a file. By using the '>' sign we tell the program to redirect stdout to the file dir.txt.
48