XXXXX
XXXXX
INTRODUCTION: Welcome to programming, through the Python language as a gateway. Where, as usual, the first thing is to get our feet wet; as, programming is in the end a practical activity; so we will duly start with some hello world based case studies.
Where, a note is in order:
WHY PYTHON AND JAVA: As an introductory, gateway language, Python can have the sort of "simplicity" that BASIC once had, which is a great advantage. This, we will make full advantage of in this preliminary unit. However, by thus hiding from view many of the key structural elements of a computer language, it is in some ways somewhat more prone to pitfalls than Java; a language that makes a lot of scaffolding quite explicit. Also, note a comment from GregF10;s article about Java on the Raspberry Pi, that "Java is faster than Python" and that "Java is safer than C", as is in the Where to go from here section of UNIT R, on Raspberry Pi. Accordingly, this workshop-oriented unit is a complement to UNIT F (where, UNIT B gives general background on Computers and UNIT M gives complementary material on Multimedia Authoring). It is not not a substitute for UNIT F, and onward it makes sense to develop the more formal units for the course as a whole mainly around Java. (However, given Jython, these two languages can in fact be closely integrated.)
After this foundational unit or tutorial/ workshop, you should be
comfortable enough to know how to set up, key in, run and compose a
simple program that uses the main program control structures, using Python.
This will then serve as a place to begin building proficiency and then expertise.
This just gets our toes in the water, giving us confidence to dive in and swim, so, beyond this unit, there will be much more work, which we encourage you to take up.
The philosophy is, an easy beginning that gives us a picture of what is to come and where to go from here gives a confidence-building leg up. Expertise, takes years to build of course, but it has to start some place, like here. So, why not here, why not now, why not us?
So, let us begin.
That
brings up our first concern, a question of basic understanding:
Programming is . . .
Programming can be seen as the coding of algorithms acting on data in structures.
That is . . . and pardon a mouth-full that we will unpack as we go along:
a --> there is a programming language (such as Python) that
b --> allows us -- programmers just hatched from the egg! -- to tell a computer to carry just how certain step- by- step procedures (i.e. algorithms) solve particular problems or achieve given results. It does so,
c --> by acting on data (the "stuff" that IT is about), that
d --> must be put in agreed, standard forms -- "types" and "structures" -- and stored in a way that makes it easy to find and pull it out again.
e --> The instructions that process the data are of course written ("coded") in a computer language, such as Python etc.
How that is done, is that first we make an electronic machine called a "computer" that has circuitry that carries out two-state, "binary digit" logical operations. "Bit" is just the shortened word for "binary digit." This is the actual hardware, the bottom layer of a "layer-cake."
Layer cake? Yes:
The actual, physical machine, is then set up so it can be programmed using a "machine language," based on the binary logic of 1's and 0's grouped in 4-digit chunks called nibbles . . . or more often 8-digit chunks, bytes.
Those chunks are stored in blocks called registers -- often, in computer memory -- and are manipulated using the machine language. For example:
Shifting focus to block diagrams with resisters storing 1's and 0's -- believe it or not! -- is a lot easier than having to figure out the detailed, complex electronics of digital logic gate and flip flop circuits running between power supply "rails" of 5 Volts and 0 Volts.
We have moved up one layer in the cake.
Obviously, even this first big step of simplification is not very "user friendly"; as we can see here from a "Hello, world" program written in machine code:
![]() |
| Fig P.1(a): A Machine Language Hello World program. (The machine code is given in base 16, so a to f count up from 10 to 15 in ordinary decimal numbers. a = 1010, b = 1011, . . . f = 1111 in binary.) |
![]() |
| Fig. P.1(b): Hello, World! |
So, we continue: layer by layer, step by step, we make the machine more and more easy to use by building up a stack of imaginary, virtual machines that are more easy for ordinary people to work with.
To get there, we can represent instructions and locations with short code words, creating assembly language (as seen). There is then an operating system [that's what the "OS" stands for in DOS or iOS etc]. The OS bridges the assembly language to the level of the applications or apps we are more familiar with.
By that stage, we can have a mouse and pointer, windows with visual representations of software objects, icons and even touch gestures etc.
We can now illustrate how the computer layer-cake works, more or less like this:
![]() |
| Fig P.2(a): The "Layer-Cake" view of the Computer |
By the way, the machine code given above is for just one specific machine, the 8086; which, is an early member of the Pentium family of microprocessors. Its internal layout in terms of functional units, storage registers, buses etc -- its architecture -- is:
![]() |
| Fig. P.2(b): The Functional units and links of the 8086 microprocessor. This addresses the bottom three levels of the layer-cake model. |
If you do not appreciate that framework, it will be very difficult to code in Assembly, much less machine code. Personal observation, when I worked with the Motorola 6809E, I wrote in assembly and hand converted to hexadecimal code [base-16, 4-bit, nibble code, see here], having first designed the code using flow charts and structured programming. I had to actually make an "honest injun" statement to that effect, as often the flowchart or the like is written up after the fact. (Poor practice!)
And since computers are usually tied to the Internet nowadays, it shouldn't surprise us to see that the Internet's architecture is another layer-cake, with the physical layer at the bottom and apps on top, which is tied to the Open Systems Interconnect [OSI] seven-layer reference model.Where, the Internet was "invented" in 1969, so it is sort of the prototype for the moch later, more refined OSI model.
The difference from the single computer layer cake, is, this is for a network of computers, or for the global inter-network [Internet] of computers:
So, now we have at least a picture of how a computer works, and how computers work together. That's useful for programming now and later on.
Of course, all of this simply showed us layer cake designs in action, again, for now; network programming is for programmers quite a bit further along than we are! (But, when you are buying a router or gateway for WiFi access, such machines work at certain lower levels of the layer cake. It makes sense to be aware of the layer cake concept.)
We can see from this, why the layer cake view helps us to more easily understand what a computer is about and how it works, without getting bogged down in details that are hardware specific. However, if we are going to do serious interfacing and control (notice, the target system, a robot), we need to have some appreciation of the lower levels. That's for later.
Back to getting our feet wet.
Python, by utter contrast with machine code, is a general purpose, high level language, one that is more suited to people and so is very useful to write applications in a relatively simple-looking form. As, we are about to see:
"Hello, World!" -- Python 3 style
Which, brings us to the core of a Python "Hello World" in the Python 3 style:
print("Hello, World!")
That's about as simple as it gets.
Of course, there is as always a lot more to the story than that seemingly simple one-liner.
But yes, that is the core of the program.
Call a function, print() and it is to print the text string "Hello, World!" in the console/terminal or integrated development environment. [Think of the brackets () as "jaws" that gobble up what is to be acted on by the print function. That will be a useful concept in Python, let's call it the jaws concept.]
To get an idea of some of the details that are baked into default settings, let's just glance at equivalent code in Java . . . which we will look at "later" if we go on to the Java based 3-credit programming course (currently under construction):
To interpret what that means -- stuff hidden under the "simple" surface of Python, we may glance at:
There is a lot going on here!
Python, of course is also an object oriented language, but is designed to look simple.
Part of that, likely, is that it was designed to make a hello world program look as simple as possible.
(Hello world is a traditional first program and can be used to see if things are set up right. We will start here then build up!)
Let's go, next, to a short video example:
Of course, that video starts with, how to get to Python on your machine, some of the background stuff behind the one liner above.
Notice, there are two versions, one where you interact with the Console for Python, and directly execute code.
Of course, that is not the same as creating a full program file, storing it in a directory of Python programs, calling it and executing it. That is the second example, where you write a code file and save it with an acceptable name in a Python program file, such as xyx_abc.py
In fact, with a simple text editor program, you can code Python.
![]() | |
| Bill Gates allegedly spotted using an iPad . . . MS shares reportedly dropped 5%. Of Course, that's a spoof site so take with a 6" on the side grain of salt. Likely photoshop. Just for fun. | |
(EXERCISE: A Text Editor is a step or two below a word processor, producing formatted text but without the fancy bells and whistles we expect from Word or the like. Libre Office Write is even worse, it is a low end desktop publishing app in disguise. That's why coders like to use text editors instead, e.g. I can remember having problems with HTML code; then, I realised I had smart curly-tailed double quote marks being installed by Open Office Write, not simple, less attractive straight ones: a href = "" . . . /a. [Libre Office is a fork of Open Office.] Now, for the exercise, download and install a Text Editor, or if such is already on your PC or Raspberry Pi, open up and play a bit with the Text Editor. Then, code and run a Hello World program as we just saw. FYI, MS Visual Studio Code for Windows is here. VS Code for the Raspberry Pi has recently been introduced, Feb 2021. You might want to go for Notepad++ or JEdit for example, if Microsoft is not your favourite software source. Go with what your tutor or facilitator recommends, for this class, so that there will be uniformity. First suggestion, as it is a fresh addition to the Raspberry Pi and that will be a focus for some of our studies in due course, try MS Visual Studio Code. Here is a useful introductory video with a hello world. [No, it is unlikely to have hidden spyware and Bill Gates is too busy running a charity to have an alarm go off on his iPad so he can pop in and spy on you, putting secret 666 Beast Number code in your computer. Just joking about his iPad, likely, he uses a MS Surface -- which runs Windows 10 -- instead. That's a different vision, while they can be used to create digital documents etc., iPads and most other tablets are optimised for highly portable digital consumption; the Surface is a lightweight, office productivity machine.] If you install Visual Studio, on the opening screen, click on installing Python and java for additional language support.)
Indeed many programmers prefer to code in that way. Others tend to use integrated development environments, which work at a different level. Such IDE's help with debugging, creating projects and setting up applications. Some of them are simple enough for beginners to use, for instance IDLE. IDLE stands for, integrated development and learning environment and is itself a Python program. Then of course you have those who code in a text editor and paste to the IDE for debugging etc.
To use IDLE, once Python 3.x is installed, type IDLE into the Windows search and click to open. Here is the IDLE, with a Hello World modified to Hello, IDLE:
There is a short primer here. For example, the initial window does not like multiple line programs. Open a new file, key in and save then run.
(And yes, a big part of our getting feet wet module, is to guide on where to begin. We now know how to do a Hello, World from a text editor and how to do the same from IDLE. Beyond, we can save programs as xxx.py files and keep them in a handy folder, such as py3eggs. Yes, that's a lame little joke. Perhaps, in the C drive folder as a top level directory, which will be useful if you use Windows Console to change directory. That's already a case of one small step for a person, a giant leap for being able to program. So, if you are impatient to get going, here is a series of tutorials, and here is a 4+ hour "code camp"- in- a- vid; notice, the oh there's no [obvious] scaffolding is both Python's biggest strength and at the same time its biggest weakness -- that's life. We bet, though, that most people will need something a bit more reflective and go at your own pace or at the pace of a guided class or workshop, so keep going.)
More elaborate programs
Obviously, a one liner program such as Hello World does not do a lot. To do bigger tasks, we need more elaborate structures that chain instructions step by step from a definite start point, through a process logic that carries out the desired task and then ends with a definite stop.
Without getting into too much detail, there are three main structures, with a fourth one built up from the first three that allow us to carry out any procedure that can be broken down in steps like that:
For example, here is a simple Python program to add two numbers:
Here, there are four steps in sequence:
- Steps one and two, request that input values be typed in and store them in labelled memory locations, number1 and number2.
- In the background, the variables are created; as labelled memory locations.
- Step 3 then processes information stored in the two variables, number1 and number2, adds them and stores the result in a third new variable, sum. Which, is displayed as a main output.
- We are seeing input --> process --> output (IPO) in action.
- Notice, Step 3 also converts the numbers to floating point format, a type of data which can hold whole number parts and fractional parts.
- Step 4 is a more sophisticated "print" operation than we saw for Hello World. Suppose you set number1 = 2 and number2 = 3, giving sum = 5 after step 3. Step 4 will now print the sentence "The sum of 2 and 3 is 5."
- To do that, it will automatically plug in the values for the first, second and third variables following the formatting code that {0} is number1, {1} is number2 and {2} is sum.
- This shows how the variables are listed as counted places after the first member of the list, {0} being the first, number1, {1} the second, number2, and {2} the third, sum.
- With this format operation, we see an array in action, here, a linear one, more or less a numbered, ordered list. That is, we have data in a structure.
- By using data stored in structures, the program and the machine know just where to store, find and fetch data from for processing and output.
- We are also seeing how a program is a coded, step by step procedure (= algorithm) that uses data in structures and how such a program takes in inputs, then processes them and generates outputs. Note, memory is used for storing information.
- Notice, how the # comments are not executed but are added by the programmer to briefly explain what is going on. (When you come back six months or six years later that will help you to understand what you wrote back then. Or, it will help the next person who has to work with the code you made. Good comments are often worth their weight in gold.)
Obviously, a lot is going on behind the scenes for even a fairly simple program.
As an . . .
EXERCISE, set it up and run it similar to the Hello World program. To do so, observe carefully, my version:
This is from Visual Studio Code.
- Did you spot, that in the "print" line, the array -- here, a row of numbers -- created by .format() is referred to as 0th, 1st, 2nd elements in a given order, a row?
- That, in the print string, the blue highlighted array references use double brackets, { } -- NOT ordinary simple brackets, ( )? [Miss that, and the print output will go to "the sum of (0) and (1) is (2)".]
- Notice, no space after the dot in dot-format?
- Notice how, on running, there is an output pane . . . the terminal . . . that appears -- it should catch your eye, and that you need to put the cursor in the right place to enter numbers?
- Did you see that double quotes or single quotes make no difference for the print?
- Notice, how the output shows a decimal point, showing that it is a floating-point number not an integer?
- [Modify float() to int() and run the new program. See a difference? Yes, the answer now has no decimal point, not even with a zero fractional part. That can be important, as for relevant example votes are normally whole numbers and if fractional parts show up something may be, er, ah, squirrelly. Then, try entering, say, 3.1 + 5.1 in the integer version and see what happens, I got: "ValueError: invalid literal for int() with base 10: '3.1' "-- which could be a useful clue. As in, integers are whole numbers, so why are you feeding me decimal points and fractional values? In the float() version the addition yields 8.2 as expected.]
- Notice, how colour highlights make a big difference?
- And, did you get my admittedly lame adder -- another type of snake -- joke?
Here is a sample output, for the floating point version:
please enter the first number: 5
please enter the second number: 8
the sum of 5 and 8 is 13.0
Yes, coding is THAT exacting.
Next, let's look at another interactive example that works with strings:
This second user-interactive example extends a case study in a "cheatsheet" here, and not only works with strings but responds differently to particular cases using if and elif tests for character string length:
- The header shows where the xxx.py file is stored, and yes, on the desktop for now. (Better practice is to set up a projects folder, why is that so?)
- Lines 1 and 2 are comments that give the title and a brief explanation. Comments help other people or even you to understand what is going on.
- Line 3 defines name as a string that takes an input, the name a user gives in response to an implied printed request in the terminal.
- The next line gives an output, using "addition" of strings, and string variables, i.e. it responds to the user. Notice, this is not sophisticated enough to allow for corrections etc.
- The "addition" of strings using the plus sign operator + is called CONCATENATION. (That is a "big" word that means chaining together.)
- Also, notice how VisualStudio Code colour codes, line numbers etc are helpful in understanding and debugging. Error messages might help too but too often they are a bit hard to figure out.

Pliers principle: Python function brackets (arg) act like
the jaws of a pair of pliers, grabbing the argument(s)
Line 5 sets up an integer variable, length of the string. Notice the jaws principle in action, again: i.e. how functions in Python use brackets to show what they gobble up to do their work. An empty pair of brackets means nothing needs to be gobbled up. What is gobbled up is called an ARGUMENT. Yes, you can define your own functions and call them just like the standard ones such as print(), len(), str() etc.- (More details here. This allows us to modularise the program by calling functions we have defined to do routinised, named sub tasks. This is a developed form of the old FORTRAN subroutine, and indeed the function . . . like just about everything in Python . . . is an object, a software actor on a virtual stage we may look at through another object, the software window. The actor has a distinct identity, has specific characteristics or attributes, holds a state (which can change with time and interactions), has certain behaviours and uses methods to carry out such behaviours. It is an instance of its class, in effect the framework it fits into and exemplifies; think of Fido, the Labrador breed dog sitting next to you. Such a class is of course a data structure. Notice, the browser window is a software window, as is the terminal for Visual Studio Code . . . notice the characteristic, default black background. )
- Line 6 will output the length of the name, chained together with an explanation. To do so, the integer variable, length, is turned into a string using string(length). Of course, that is abbreviated str(length).
- Observe, how space characters are used to keep words apart instead of beingchainedtogether.
- Lines 7 and 8 address a condition, a first case, if the name has four or fewer characters it will print a certain message. Notice, the indentation.
- The other case, of five or more characters is addressed in lines 9 and 10. Notice, greater than or equal to here.
- After this, age is taken as an input, again, an integer or whole number.
- Lines 12 and 13 respond, echoing the input to the user.
- (NB: If you look at the cheatsheet, you will see that it has a typo, it uses curly, smart quote marks, Generally, when you code, curly quote marks will be taken as errors.)
- We are already seeing how programs are put together, using process logic, defining variables, manipulating them, taking inputs, advancing step by step, addressing alternatives.
- Notice, three major data types are now seen in action, integers, floating point numbers, strings. Beyond this, we can set these basic types in data structures, such as lists, trees, arrays.
- Of course, this case is a simplified case of capturing user data, which could then be used elsewhere. Notice, how similar it is to the computerised answering machine you might hear when you phone an office? Why do you think that is so?
Here is a sample output:
Hi! What’s your name? Tom
Nice to meet you Tom!
Your name has 3 characters
Your name is nice and short
How old are you 5
So, you are already 5 years old, Tom!
Contrast, here, the dialogue box for a PRINT command, with a Graphical User Interface (GUI), noting preview pane, buttons, radio button lists, drop-down menus, scroll bars and tabs etc.:
How do you think we can move from the text-based interface to such a sophisticated one with graphical elements and objects? (Hint, Python comes with libraries for doing this.)
Exercise: Key in and run the string_ops program. How would you like to extend it, and how do you think the cheatsheet might help you to do so?
These exercises, likely, will show you that computers generally have no common sense and will do exactly what they are told to do. Yes, there is a whole field in Artificial Intelligence trying to program common sense into computers. Hard to do.
So, we need to see how we can tell -- program -- a computer, step by step, to do the right thing, consistently. That's hard, too, but a lot easier.
The General Approach: IPO, HIPO, GUI (Tkinter), Basics of Algorithm Design
These three programs also help us to see how the general approach for programs is to initialise the machine to a known start-point then to carry out input, processing and output operations:
Before we get to that level, it may be helpful to do a break-down of the overall task for the program into a tree-like pattern of sub tasks that come together to achieve the whole task. Some call this, "divide and conquer."
Above, for example, we wanted to make a computer carry out simple addition. To do that, we saw that it could be broken into steps following the I-P-O pattern. We made the machine "request" two input variables, which the user keyed in. That's input and storage, using types of data and structures to store it. Next we needed to transform and store the result in the value for a third variable. Then, we needed to generate a well-formatted output. String manipulations were similar.
So, we can see how sub-tasks are coded as above and then results are passed from one module to the next until the final results are achieved.
This brings up another old but still useful tool, HIPO Charts:
(Notice, how this is like an upside-down tree, root at the top, then branches down to "leaves" at the bottom. This is a common pattern for information or organisations or categories such as classification of life forms in biology. In computing, it is called a tree "data structure" pattern. Fun fact, hier + archy speaks of how priests were organised with the Highest one on top, then Chief ones and lesser ones to the most junior. This is how many organisations and businesses are also organised. The overall work is divided and delegated through a "work break-down structure." Similarly, a machine is made up from various parts fitted together and arranged to carry out its function and to analyse a machine or system or concept etc is to take it apart into parts, links and relationships, so we understand how it works, why. Divide and conquer is a powerful and widely useful strategy.)
An uprooted, upside-down tree:
![]() |
| The Upside-down tree. Rather artistic, isn't it? |
An example, analysing an ABU 6500C3 fishing Reel:
Here is the assembled reel:
EXERCISE: State the overall function of a fishing reel (does, store, pay out and retrieve fishing line cover enough?) and then draw up a tree of sub assemblies down to parts or blocks of parts -- handle, spool, foot, frame, levers, gears, screws, nuts, sleeves, washers, etc -- that come together to make it work. Count the number of parts, and note how fast things add up to get a real world object to work. Cars, BTW, have many thousands of parts and things like aircraft can be well beyond that. Coming back to the reel, think in terms of procedures and modes of operation that use it: load line, fit to a rod (the foot), cast the bait or lure, retrieve, hook and play a fish (why drag washers to brake runs by a fish?), reel in. Notice, even a fairly simple real-world object has a lot of well-matched bits that are carefully organised, arranged and put together to get it to work properly. (INSIGHT: Realistic programs are like that, so if you ever feel bogged down in a programming task after this, remember the fishing reel case study.)
A common pattern with HIPO organisation of software is to have a main, controller module for a program, which then passes key tasks to lower and lower modules -- the old, Fortran Language word for it is, "subroutines" -- as necessary. These subroutines then return results which are used in the following steps; until, the overall task is performed. Again, divide and conquer, using the power of the tree data structure pattern.
![]() |
| Admiral Grace Hopper's original, literal bug. It was blocking a relay. BTW, Adm Hopper was an African American woman, Mathematician and a chief contributor to COBOL, a major early language. |
Where, cost to find and fix a bug balloons as we move from early ideas to design, development, installing and commissioning then post rollout maintenance and updated versions, etc. Where, the easy to find bugs usually get found and fixed quickly but subtler, harder to find ones take significant effort to search for, recognise and fix. Similarly, the less else one has to change, the cheaper and more likely it is for a "fix" to be successful. Where, a further problem is, that if to fix one bug several other things have to be changed, that becomes more and more likely indeed to cause new bugs. A self-defeating exercise.
There is thus a saying: no complex piece of software is truly bug-free, just, more or less reliable.
This leads to a consequence of the divide and conquer approach:
The modularity, 1+1+1, principle: software modules should have one normal entry point and one normal exit point, should carry out one clearly defined task, and should not be tightly coupled. (That is, they should not be . . . or become . . . a tangled, tightly interwoven, interlocking, cross-referenced mess known as spaghetti code.)
One way, is to take advantage of the modularity of a HIPO approach.
So, we can define a topmost framework and see that the overall function is reasonably defined, writing dummy "stubs" that deliver stand-ins for lower functions to be called. (Of course, if a canned software module . . . a package or a library . . . exists, that's where it would be called.)That is, the top module is the interface to the user for inputs and outputs, calling on support services as required to fulfill the mission of the program. It will probably help to consult with users, their managers and other stakeholders and draft a preliminary program mission statement. The topmost module's job, then, is to be the user interface to fulfill that mission. Lower modules provide support services towards completing it.
Then, as development proceeds, the initial dummy stubs are replaced by real service-providing modules, taking advantage of the 1+1+1 approach. Then we integrate and test the whole.
Of course, as noted, part of the current solution is to have libraries of "canned" reliable routines that carry out relevant functions etc that can be imported into a given program.
For example, for Python, we can see as an introduction:
This also means that for Python and other modern computing languages, the divide and conquer approach is backed by a large array of useful, reliable service-providing modules, e.g. see here for The Python Standard Library. As an exercise, click the link, and see the list of modules available -- huge, and like a supermarket, one goes to the aisle and picks up what one needs for use when one needs it. We can also see how, while it is easy to get feet wet, full mastery takes years of actual practice with realistic or better yet actual challenges that you want to solve enough to fight through the software development and debugging cycle.
Indeed, presence of such libraries of resources is a key feature of modern computer languages, so even a "newbie" such as we are can easily call up powerful services and can write programs that do a lot more than the equivalent of playing with alphabet blocks. In that regard, Python is not just "the new BASIC," but it is what BASIC dreamed of being.
As an example, we can see a simple exercise on creating a module xxyyzz.py -- notice, dot-py is thus, clearly, a file type -- then importing it; here.
An important onward topic would be creating a Graphical User Interface (GUI, often said "goo-eee") for a program. The archived primer here is worth noting for reference for that.
Let's clip from an online tutorial on creating a GUI based Hello World module based on Python's built-in Tkinter ("tee-kay-inter" or sometimes "tah-kinter"):
The code:
import tkinter
window = tkinter.Tk()
window.title("GUI")
window.geometry("640x420")
hello_message = tkinter.Message(window, text = "Hello World GUI")
hello_message.place(relx = 0.5, rely = 0.5, anchor = tkinter.CENTER)
window.mainloop()
![]() |
| WIMP interface features. P of course is the pointer, (HT: Abraham D/ Slideshare) |
- Tkinter is invoked, to provide services to the mission (here, to display Hello World in a GUI Window) and to create a window, details to follow
- The window being created is titled, GUI
- It is of course rectangular in geometry and is to be 640 x 420 pixels
- The message text is specified
- The message is to appear in the centre of the window, relx and rely give as at 50% across and 50% down from the upper left corner of the window
- Window is to be displayed as the main loop.
- Notice, how many details have to be given so the machine is instructed as to exactly what is to be done
- This will apply to further window elements such as radio dials, menus (e.g., pull-down), scroll bars, buttons, text windows, icons, etc.
Of course, to write a windowing program from scratch would be a monumental task, instead of what, eight lines of standardised code as we see above. That shows the power of libraries that deliver support services.
Here is the video tutorial:
Exercise, try building and running it. Notice, the indents above have to be removed. This is a beginning, windows obviously can be built up into complicated interfaces such as we are used to but that is beyond present purposes. (Again, see the reference here.)Contrast, what could be called hello_world_button dot py
from tkinter import *Notice, how this hello world uses a different style:
from tkinter import ttk
root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()
- to import tkinter, it uses a from statement with an asterisk as wild card for "all."
- It then imports the Tk themed widget set introduced in version 8.5, using from tkinter import ttk.
- This overrides the older set of "dated"-looking widgets for such windows.
- It also implicitly creates the window and lastly
- it puts the text as a label for a button.
This button version shows, too, the Python philosophy of using defaults to do much of the work of programs behind the scenes; something which was bypassed to some extent for the previous hello_world_gui example above by its stipulation of details for the window and text in it. Beyond this, various other window elements can be added that would allow for the more familiar interfaces we commonly use.
Exercise, do this example also. Why is it different in appearance and size from the previous GUI window?
(It is worth considering, that if you really do need to build fresh service modules to carry out truly unusual, specialised services, it may make sense to create them as libraries also, cf here. But that is an advanced point for future reference, beyond our current scope. For now, bear it in mind for future work.)
Modularity and divide and conquer, together, are clearly quite powerful, especially when we use libraries of resources that are built in in Python (and of course, other languages).
We can now better appreciate our more sophisticated PRINT dialogue box, as we have seen a small slice on the kind of code that lies behind it:
However, we must remember, the word-picture of software development we have painted so far is idealised and simplified.
For, a second dirty secret of complex tasks is that by the time we are able to precisely define what the challenge is, we have provided maybe the biggest part of the solution. Yes, as a rule, the bigger part of problem solving is actually clarifying what the problem really is, in a way that helps us move towards a reasonably reliable solution.
The early efforts therefore help us to better understand the mission (or, how we failed to understand it), and thus how to better fulfill it.
But, let us beware of excessive mission-creep; it is better to prioritise a core that must be met, with outer rings of lower priority capabilities. This points to the MoSCoW prioritisation rule:
- MUST have this requirement to meet the business needs.
- SHOULD have this requirement if at all possible,
- (but the project success does not rely on this).
- COULD have this requirement
- (if it does not affect the fitness of business needs of the project).
- WON'T represents a requirement that stakeholders have postponed
- (due to the timeliness requirement)
Expect to have to come back and rework this charter for the implied project for the creation of the program; but again -- it's that important and that easy to forget: avoid mission creep. And yes, all of this is often quite a challenging task.
No pain, no gain.
So, dirty secret no 3: be prepared to have to "go back to the drawing board" as unexpected issues crop up.
Where, too, we must note the classic GIGO ("gee, joe") principle:
GIGO principle: Garbage in, garbage out. That is, no software is better than the quality of its inputs, design and execution by its programmers. Garbage in, presumed gospel truth out is doomed to fail. Translated: put in the effort, early, to get it right. A stitch in time saves nine -- or nine thousand.
Now, the above addition of two numbers case used our first program structure, a simple sequence of steps, which of course has one normal entry and one normal exit point. (Abnormal exits are provided for in case of failures, to make sure failures are handled safely. For instance, on power fail, a safe procedure detects the failure, stores current state and work then shuts down in an orderly fashion, all in milliseconds if possible. This is a big topic and beyond our current scope.)
![]() | |
| Then Commodore Grace Hopper, in 1984, an inspiration for women in science, math and technology |
Sometimes, though, we we want to test for a condition, then do an operation or else depending on the result, i.e. make a decision. That's the second structure. IF_ELSE. And BTW, Adm Grace Hopper was an early pioneer of English-like programming languages.
Then, too, we can loop the decision, repeatedly doing something until a condition holds or doing something while a condition holds, depending on what is more convenient. DO_WHILE. (Sometimes, DO_UNTIL.) That's the third main structure.
Such looping is so important that it is estimated that typical software may spend 80% of its time in loops.
![]() |
| Rotary Switch |
All of these are examples of Algorithms. Where,
Algorithm Principle: An Algorithm is a finite sequence of specific steps used to achieve a result. Finite, as it must complete the job without endlessly flailing away (or else it must find a safe way to shut down promptly if it fails). Sequenced, to progress logically from start, to taking inputs, processing data and delivering outputs. Specific, as -- since Computers have no common sense -- it must say exactly what is to be done on what, how. Computable, so it actually works on a given type of computer. Effective, reliable and satisfactory, as it must satisfy its users that they can trust it to address their needs without letting them down.Indeed, we can see -- again -- that:
Programming Principle: Computer Programming is the coding of algorithms that act on data stored in structures to achieve desired results.A useful though now old fashioned tool to do so is flow charts such as we saw for the four structures above. They are easy to draw and help us focus our thoughts on the process logic to carry out the tasks and modules identified through HIPO charting, integrating into a whole.
A somewhat "jazzed up" format uses nested, interlocking blocks similar to MIT's Scratch children's programming language. For Python, then 14 year old Joshua Lowe of the UK developed EduBlocks, which can also generate editable Python text code in a sub-window. Screen shot:
As an example, a traditional flowchart for an algorithm is:
(For more on flowcharts, go here, and this is a video.)
Flowcharts etc are about algorithms. Algorithms, which need input and stored data organised in a standardised way in order to have anything to process to give an output. That output, too, has to be similarly organised.
![]() |
| A Tree is a typical data structure. Pointers are used to connect nodes. [HT: TutorialRide, cf. for details] |
Data Structure Concept: In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data
Where, Clifford A. Shaffer adds:
[A] data structure is any data representation and its associated operations. Even an integer or floating point number stored on the com-puter can be viewed as a simple data structure. [Usually, termed a data type.] More commonly, people use the term “data structure” to mean an organization or structuring for a collection of data items.So, we see that data has to be organised in a standard, agreed way so that an algorithm can fetch it, interpret it correctly and act on it effectively. This starts with values for whole numbers [1978 or - 1978]. It holds for "decimal numbers" [19.78] or "scientific notation" numbers [1.978 x 10^9], both of which are called floating point numbers in Python. Other languages distinguish fixed and floating point numbers. Also, it holds for characters coded in ASCII or UNICODE or strings [s-t-r-i-n-g-s] of such, or two-state "binary" variables that serve as yes/no or true/false flags about conditions we are interested in. Shaffer then adds:
A sorted list of integers stored in an array is an example of such a structuring. Given sufficient space to store a collection of data items, it is always possible to search for specified items within the collection, print or otherwise process the data items in any desired order, or modify the value of any particular data item. Thus, it is possible to perform all necessary operations on any data structure. However, using the proper data structure can make the difference between a program running in a few seconds and one requiring many days.The issue of efficiency is crucial:
A solution is said to be efficient if it solves the problem within the required resource constraints . . . The cost of a solution is the amount of resources that the solution consumes. Most often, cost is measured in terms of one key resource such as time [required to compute the result]. [In, Data Structures and Algorithm Analysis, Edition 3.2 (C++ Version), Sept 2011, pp. 4 - 5.]Why are these technical details important?
Because, often, the dominant part of computing is not actual calculation of results, but the creation, accessing and processing of data in such structures, leading to the importance of cost-effective, efficient solutions and algorithms to search, sort and generally manipulate such. Never mind the name, "computer" -- which used to be a job you hired math whizzes for by the office-full -- the main job for a computer is data and information access, organisation, retrieval, storage, processing and communication. Hence, too, why the Internet global information exchange is so vital.
The design, or architecture, of data structures is therefore quite important.
Thankfully, programming languages usually have a good selection of built-in, "canned" "native" data types and structures. For example, Python builds in complex numbers. For Java, to get there, one has to invoke Math. And yes, that's a clue on how important complex numbers are for technical computing.
For Python, Rohit Sharma summarises:
[T]he primitive data types include Integers, Float, Strings and Boolean ["flags"], the non-primitive datatypes are Array, List, Tuples, Dictionary, Sets and Files. Some of these non-primitive data types, such as List, Tuples, Dictionaries and Sets, are in-built in Python. There is another category of data structures in Python that is user-defined; that is, users define them. These include Stack, Queue, Linked List, Tree, Graph and HashMap. [Data Structures & Algorithm in Python: Everything You Need to Know, May 6, 2020. See the complete article for details and here for short illustrated briefs on the various structures. Also, see Wikipedia here, here and here.]Some of these will now come up as we look at:
As, Arithmetic is obviously vital -- as we have seen, here is Ms Socratica on Arithmetic in Python 3. Here:
(NB: Notice, she introduces Complex Numbers which extend "real" numbers using the "imaginary" axis at right angles to the "real" number line. She uses the engineering style where j is square root of -1; which is used by Python.
To see how that makes sense, put x on the real line . . . X-axis . . . as vector 0 to x. Magic step: define an operation j* as "rotate 0 to x by 90 degrees, anticlockwise." 0 to j*x is now on the Y-axis. Now, do j*[j*x]. The second rotation puts us at 0 to -x on the real number line. So, j* j* or j^2* is -1*. It is obvious, j^2 = -1. So, by definition j is the square root of -1. This rotating vector approach saves a lot of headaches.
Complex numbers are a way to move from reals as one dimensional vectors to two-dimensional vectors. 1 is the real unit and j is the imaginary unit. And yes, real numbers and integers are vectors, with size and direction. Complex numbers are very powerful in Math, Science and Engineering. For the curious, here are a couple of complex numbers, using i as symbol for the square root of -1:
Okay, enough for now on that.)
A similarly important feature is IF_ELSE. Ms Socratica, again:
Notice, how the special vocabulary of Python (and other Computing languages) begins to crop up. The ">" and "<" symbols are as expected. Mathematical equality comes up with a double equal sign, "a = = b" . . . without the gap I put in for clarity. Here, a = = b is an instruction: test for equality of a and b then report true or false. (We can interpret that as yes or no, to then select a branch.)
Not equal is "a!= b" and so forth. For instance, a raised to the power b is a**b, other languages may use a^b. Such, are often used for tests that then help to make decisions on alternatives to be taken at decision-points. (For details on many more exotic symbols and operations you may run across, see here.)
This brings up Boolean variables:
From this, we may explore Functions:
Vocabulary: in Python and other languages certain "key words" and key or special symbols will be reserved for special use and you may see odd word-like things such as "elif" for else, if. And so forth. Here is a glossary of key terms.
At decision points, comparisons are made to see if something is true or false, which then drives which fork of a decision will be taken (as is shown on the Euclid flowchart above).
Strings are another key topic:
Then, of course, there is the built-in interactive help feature:
(To go on in detail, I suggest, here is the same series of tutorials from Ms Socratica, and here is a 4+ hour "code camp"- in- a- vid. A basic "cheat sheet" is here. Another is here.)
In Python 3 and other languages, we commonly test for conditions and use the result to select alternative chains of steps:
These operators should be familiar from Maths class, but we can see that to easily type them in, different symbols are used for computing. For example instead of crossing out the equal sign, an exclamation mark is put just in front: !=. Similarly, instead of = which is used to assign the RHS value to a variable named on the left, we use a double equal sign ==. The way greater than or equal to is written avoids the special blended symbol. AND and OR operators from Boolean Algebra or Logic are written as and or or.
And so forth.
The case or switch structure, of course, is a chain of if then else statements, comparable to a multi-position rotary or sliding switch.
Here is a "simple" for-next loop structure (which is based on looping a decision and chain of actions):
Now, let us look at a While Loop:
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
Good bye!
![]() |
| A bottle-filling machine in action |
Now, such seems trivial. But, what we are actually seeing is pre-programmed decisions to do something a set number of times, or while/until a condition is met. That is, we have delegated decision-making power to a machine.
![]() | |
| Raspberry Pi based drip irrigation controller (HT: jenfoxbot and Instructables, NB: For an NPN Transistor, the Collector is normally at a higher voltage than the emitter to allow the controlled current to pass, the - and + VDC relay terminals seem to be switched in the diagram. HINT: Never blindly carry out what you see in a book, magazine or online.) |
Here is a more realistic example, which chains decisions:
One might be tempted to imagine this is a mere academic exercise. It is actually, for one, a glance into the world of using computers to do calculations. (Yes, they can also be programmed to solve algebraic exercises.) More technically, what could we do to convert a sensor voltage to volume or weight of a liquid, then use that to control a process?
What about converting a similar voltage from each sensor in an array and converting it into areas or facets, curves, lines, edges, colours, etc then modelling a 3-dimensional image of a road or the like to guide a vehicle? Or, a robot explorer on say Mars?
In short, getting our feet wet is a step towards much bigger things.
Exercise: enter and run this, then save it as an exercise in your python folder.
Going Further: Try the list of exercises with sample solutions here. This will gradually build programming skills. Mosh's 6 hr video course may also help.
Where, we must understand that real-world problems tend to be complicated and full of technical details, as is so for any significant technology. Yes, "simplicity" is often a myth and a seemingly simple user interface is a carefully designed framework set up to meet the needs of the basic or newbie user. That's so for a point and click or touch and swipe software window interface for an app, or for a steering wheel, windscreen, side and rear windows, mirrors, brakes, accelerator and paddle shifter interface to drive an automatic transmission vehicle. Just think about engines, power trains, suspensions, steering and front vs rear vs four or six wheel drive or caterpillar tracks.
Another classic case is the trigger, bolt, shoulder stock and iron or even telescopic sights of a rifle . . . the complexities of ballistics and building a tack-driver rifle are such that this played a key role in developing modern industries. Indeed, just external ballistics is a gateway to aerodynamics, flight control and yes, rocket science.
Compare:
The obvious balance is, set up a simple, newbie oriented interface, but make sure you have used HIPO and process logic charting to get adequate solutions of ALL of the relevant technical details (and set up a second interface for the techies who have to support and maintain the software). Those technical details exposed through HIPO are the logic of the process, driven by the underlying forces, materials, energy flows, transformations, cash flows, business issues, legal questions, activities, information flows and physical or chemical laws at work -- the socio-technical system dynamics that have to be "good enough for government work." Of course, that also requires MOSCOW prioritisation to get good enough goods out the door within some reasonable semblance of the set target date. Consider yourself duly warned.
What we have done in this Unit is just enough to get toes wet. (But, that's the point, we need Somewhere To Start, with a basic map of the territory with where to go from here.)
[WHERE TO GO FROM HERE]
So, for:
- an overview of Python, Wikipedia has a useful article [it is often surprisingly good on non-political, technical subjects] and this nuts and bolts survey of features at wikibooks is worth downloading as a cookbook, short note, quick-answers go-to. (If that's not enough web search key words being used in it to find out more.)
- The official survey of data types etc is here.
- Always bear in mind that we have worked with Python 3, the future of Python (they were supposed to stop officially supporting Python 2 in 2020.) Of course,
- the official Python site is the proper go-to for all things Python and
- the big PyPi Repository of "modules" will give you all you are likely to need for technical references and canned heavy duty software.
- Take a look at the Raspberry Pi 400, as a US$ 100 package computing PC in a keyboard with its GPIO port accessible and "only" needing a TV to act as monitor. (This makes for a very affordable Linux OS computer for programming and interfacing work; you don't have to risk your main productivity PC. The available Mathematica alone makes this a huge value for money proposition. [Apparently no longer bundled, download here.])
- Do not under-estimate Python's ability to be the sweetie-wrapper interface for decades of legacy technical modules written in other languages such as C or even Fortran etc. Where
- these days, programmer time and energy are far more expensive than programming power and storage, do not hesitate to optimise the really scarce resource and use a "slow" interpreted language such as Python to save your time, relegating hard core nuts and bolts to modules you import and use from libraries and/or fast code for technical functions written in other languages. (And yes, don't think one programming language is enough, part of why this set of units will go on to Java!)
- For the next level on using Visual Studio Code, here is a good video tutorial
________
APPENDIX A:
A Note for Numerical Analysis Programming
This is a get your feet wet module, but we cannot be blind to a major reason to get into Python based programming, so let us take up a few points. The following are just a quick set of notes pointing to where to go from here, to use numpy, scipy, pandas and matplotlib:
1 -- Installing libraries or packages or canned modules:
Python has many libraries, the above four with IPython and Jupyter are likely to be particularly relevant. We assume, Windows 10 and a current Python Installation. Such will come with PIP, the Python installer package. (Getting Pip is itself a further challenge.)
First, consider installing the Anaconda Python Distribution, that will have packages pre-loaded.
Alternatively, first, to install a current Python 3 version, search for a current distribution and use the Windows exe file. From 3.4 up, we are up to 3.9+, then follow the usual double click and wait. If you need this, it doesn't make sense to say, customise folder to a C-drive first root level for convenience, just go with the defaults.
Search for Python with the Windows search bar. Click on it when it appears, and try a Hello World sanity check. It never hurts to be sure things are working. Close off.
Search for Windows Console, click on it then at the right angle brace> type pip install numpy then press the enter key at the right hand side of your keyboard. If it works, you should either have a protest that it is already there or else it will install. (If things do not work simply, go get help. As I noted, installing the Anaconda Distribution probably will save trouble.)
Repeat for Scipy, Matplotlib, Pandas and Jupyter. The last will install a lot of extra stuff, wait patiently.
2 -- getting familiar with the packages:
A useful set of notes on numpy and scipy is here. A video introduction will help, a lot:
- A 4 hr tutorial at College level, here
- A 2-hr one that starts at more basic level, here
- For pandas, go here and here
- Matplotlib, try here or, here
- And, more
Numpy, is an array based computing package that creates arrays as a bloc stored in memory, fitted for mathematics. Other packages build on it. Scipy gives a broad range of mathematics, science and statistics. Pandas extends statistics. Matplotlib is a plotting package.
3 -- Working with your own data:
Once you are familiar enough, try your own data.
4 -- Opening up a new world of programming for digitally enhanced productivity:
Jupyter and similar programming notebook technologies open up a whole new style and context for programming as part of one's productivity as technologist, professional, data analyst, general analyst, etc. Robert Johanssen aptly summarises this:
The Jupyter Notebook . . . is a web application in which Python code can be written and executed through a web browser. This environment is great for numerical computing, analysis, and problem-solving, because it allows one to collect the code, the output produced by the code, related technical documentation, and the analysis and interpretation, all in one document. [Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib, 2nd Edn (New York: Apress/ Springer Science+Business Media, 2019), p. 5.]
This of course inverts the often noted advantage of compiled high efficiency languages [think, C] over interpreted, scripting languages executed one line at a time. In a day where programmer effort is the bottleneck resource, we here see flexibility using a browser to implement a blended document with text, code, output, visualising plots and inferred interpretation integrated in a single web technology document. Which, is also immediately highly collaborative. It seems, even books are being written this way, exploiting the associated text editor features.
A glance:
In this context, Python's ability to be the sweetie wrapper on functional modules developed using C, Fortran etc should not be underestimated. In short, here we see a glimpse of tomorrow's digitally enhanced professional, to whom a working notebook, naturally integrates programming code and its output. In education, the same abilities are of course potentially huge.
That is part of why this module has now taken on a life of its own, as helping to open a door to a new education and technological/professional paradigm.
[WIP . . . ]








































