Monday, July 8, 2019

Communicating With a Computer - The Programmer's Perspective

The author has the pleasure of teaching an online course in Python at this time for the University of the People. This is a low-cost international distance education opportunity for many across the globe.

Note: UoPeople could use some more computing instructors, if you have a masters or doctorate degree in a computing discipline. Simply apply at www.uopeople.edu.

While working with these students, many who had not taken a previous computer programming course, he sought a way of expressing the simplicity of computing.

And, when simply put, one is communicating with a computer when programming. One simply gives the computer a list of commands, that could be easily spoken in this day of speech recognition. Then, the computer performs these tasks the same way that one would if he were checking the commands for accuracy.

The following passage was shared with the author's students:

Python Programming Explained In Terms Of English.

This is five typed pages and 1083 words, nearly a short paper. You already have plenty of reading for the week, but some of the comments in this passage might address some of your confusion, if you have been struggling with Python. You can read it, if you have time. It is not required.

For some of the class, this will be overly simplified. For others, it will be “spot on”. Summarizing all of the basic abstractions concerning computer programming in a few paragraphs is a near impossibility. But, I will try. Hopefully, this will shine a light on the darkness in which some of you find yourselves.

Electronic computers can complete some amazing feats of computation, but they can only do what they are told. They cannot reason outside those bounds. When we communicate with computers, we do so with an intermediary language. It is not their mother tongue, which is a series of “low-level” commands most naturally expressed with zeros and ones. And, it is not a “natural spoken” language such as Hindi. It is somewhere in between. It is called a “high-level” language, because it is human-readable and not written in a cryptic string of zeros and ones.

“High-level” computer programming languages come in differing classes. The most common class of such languages are those based upon giving the computer “commands” that it must perform. These are called “imperative” languages. In the English language, an “imperative” sentence would be, “Open the door”, a direct “command”. In this sentence, the subject, “You”, proceeding “open” is implied.

So, the following Python command:

>>>print(“Hello World”)

Could be “mentally” read as:

Computer, print the phrase “Hello World” on your screen.

The following commands for calculating the area of a circle:

>>>radius = 5

>>>approximatePI = 3.14

>>>circleArea = approximatePI * radius**2

Could be “read” as:

Computer give the storage location called “radius” a value of five.

Computer store the value of 3.14 in the data location called “approximatePI”.

Computer compute the value produced by what is in the storage location called “approximatePI” multiplied by the square of the value in the data location called “radius” and place the result in a position within memory named “circleArea”.

It must be noted that if you tell the computer that it should do the wrong thing; it will do it “faithfully” every time.

These commands, also called imperatives, that we give a computer follow a format and have certain rules of structure as do spoken languages. Natural languages often have subjects, explicit or implicit, and predicates with verbs plus indirect and direct objects. Also, they contain certain modifiers for the nouns and verbs used in them. And, they use punctuation for signaling certain parts of the sentence, such as the end, with a period or question mark. In Python, the end of each command is simply the end of the line on which it sits. This is marked by a couple of non-printing characters, the carriage return and the line feed. Also, punctuation such as ( ) { } [ ] , “ ' # and others have significance and express something important about the current Python statement. Plus, the punctuation must be used in a certain way so it is meaningful and correctly expresses what the programmer intended.

One of Python's unique features that is not found among many other programming languages is the importance of the program's “indenting” pattern. In most other languages, indenting is simply added for readability. But, it Python, it effects how the interpreter “parses” and understands that commands that is given. A “missing” or “misplaced” indentation can result in a syntax or run-time error, plus it can produce erroneous output although a program completes successfully.

Also, from some of the discussion comments during the second units, it seems that some of the class struggles with the difference between a simple series of instructions in a script and a “well-defined” function.

For instance, if one had the instructions above that calculate the area of a circle, your assignment required that you calculated three areas for the radii of length 5, 10, and 20, plus you did not use a “well-defined” function, then you must list those three instructions three times. Such a program would appear as below:

>>>radius = 5

>>>approximatePI = 3.14

>>>circleArea = approximatePI * radius**2

>>>print( “The area for a radius of “ + str(radius) + “ is “ + str(circleArea) )

>>>radius = 10

>>>approximatePI = 3.14

>>>circleArea = approximatePI * radius**2

>>>print( “The area for a radius of “ + str(radius) + “ is “ + str(circleArea) )

>>>radius = 20

>>>approximatePI = 3.14

>>>circleArea = approximatePI * radius**2

>>>print( “The area for a radius of “ + str(radius) + “ is “ + str(circleArea) )

This is somewhat overly redundant since the value of “approximatePI” does not change and could be set once at the beginning of the list of instructions and then used throughout. Yet, the point is that this usage of Python in not “well-structured” or practical. If for some reason, you needed the surface area of a sphere with that given radius, the calculation of the area and its associated variable name must change in a number of places.

This is why we use the keyword “def” which stands for “define” in Python and create modular functions with it. The modules give our programs structure. Plus, they support the “structured programming” policy of having a “single-point” of modification when corrections or enhancements must be made. A program with a function for determining these areas of a square would be as follows:

import math

def measureCircleArea( radius ):

     areaString = str( math.pi * radius**2 )

     return areaString

print( "The area of a circle with a radius of 5 is " + measureCircleArea( 5 ))

print( "The area of a circle with a radius of 10 is " + measureCircleArea( 10 ))

print( "The area of a circle with a radius of 20 is " + measureCircleArea( 20 ))

This program could be “mentally” read as follows:

Computer, search the files in the Python libraries on this computer for a module called “math” and “import” all of its functions and predefined values for use in this program.

Computer, next define a function with the name “measureCircleArea” that accepts one input, a formal parameter called “radius”.

Computer, when the “measureCircleArea” function is called with an actual argument that should be a number, complete all of the steps that are indented by at least one tab character until you reach a return statement or the indenting stops that started after the function header prefixed with “def”.

Computer, the first step that you should complete for “measureCircleArea” is multiplying a value for the number PI, “math.pi”, that came from the imported library module, by the square of the formal parameter “radius” and store this in a temporary data location called “areaString”, after taking the numeric result and making a string from it with the str() type conversion function.

Computer, next “return” the value in “areaString” for use by the calling program.

Computer, this represents the end of the module “measureCircleArea” as the return has been reached and the program source outdents again.

Computer, for the first line of the programming script that you will execute when this program is called, print on a single line the following phrase, “The area of a circle with a radius of 5 is “ suffixed with the result of performing the “measureCircleArea” function with an input of 5.

Computer, for the next line of the programming script that you will execute when this program is called, print on a single line the following phrase, “The area of a circle with a radius of 10 is “ suffixed with the result of performing the “measureCircleArea” function with an input of 10.

Computer, for the final line of this programming script that you will execute when it is called, print on a single line the following phrase, “The area of a circle with a radius of 20 is “ suffixed with the result of performing the “measureCircleArea” function with an input of 20.

We could have used a variable like “approximatePI” whose value we defined as 3.14, but we used the value of pi stored in a special Python library for mathematical operations. This was done so one concept could be reinforced. The importance of “structure” and modules, such as functions, or subroutines. Someone has written a series of mathematical routines for use by any Python programmer and placed them in a module called “math”. These sub-modules, such as sin() for sine, cos() for cosine, atan() for arctangent, and etc. are available for use by anyone who imports math. Plus, the module contains a number of predefined values that cannot change such as pi.

The important lesson here is that when you learn enough about Python you can write your own modules such as “math” and compile libraries of them. This means that you only must write a function once. Then, you can reuse it many times.

Also, it seems that some of you still have a struggle with the concept of a variable. These are like the variables in algebra, in that, they can take on a wide array of values. But, they are a “little” different in that they represent a label for a location in the computer's memory. If you must use data in your program, you must store it somewhere. Plus, you must have a way of referencing this information. Python is really flexible with variables, in that they can accept data of any type. This is called “dynamic” typing. In many other languages, one must specify the type of data which a certain variable can accept. This specification occurs when you name the variable. This is called “static” typing. Python is much different. One can simply create a meaningful name for a variable. This name is called an identifier. After creating a variable's identifier, one can just store a value in it without specifying any limits on the type of data that it can hold. For instance, the following variable receives a string variable, is printed, is assigned an integer, and then is printed again.

>>>courseNumber = “CS1101”

>>>print( courseNumber ) #prints CS1101

>>>courseNumber = 1101

>>>print( courseNumber ) #prints 1101

In most cases, Python variables must have something stored in them before they are used or an error will occur. Something is stored in them when one uses the assignment operator, =.

Hopefully, that was helpful and not a burden. You already have plenty of reading for the week. If you have any specific questions about any particular Python topic, please send them. Also, please take the time and focus your questions. General questions often get general answers which might not meet your needs.

Note: When this passage was shared in an early term, one student who had some programming experience mentioned that this was a very simplistic view of what was occurring. Plus, he said that much was left out, such as the interaction of the program with the computer's memory. But, these details are not absolutely essential for understanding enough that one can write programs with variables, functions, and other programming structures.

But, most of all, remember that one should hunt, peck. and think while programming.

No comments:

Post a Comment