INSIDE THE MIND OF SOFTWARE
The primary objective for these series of articles is to get you to learn how to think like a computer scientist. The way computer scientists think is essentially a combination of some of the most important aspects of mathematics, engineering and natural science. As mathematicians tend to do, computer scientists make use of formal languages in order to help break down and visualize ideas, and in this particular instance, computations. And as engineers tend to do, their job is to design stuff, where various components are put together into systems and assessing tradeoffs as well as alternatives. As scientists tend to do, their work revolves around the observation of behavior in sophisticated systems, the formulation of hypotheses and experimentation with predictions.
The most important skill that is in the computer scientist’s repertoire is that of problem solving. The essence of problem solving lies in the ability to identify problems and dissect them using creative thinking as to churn out solutions, and elaborate on a solution with clarity and preciseness. Just so you know, the processes involved with programming are a unique opportunity to explore and work on your problem solving prowess. This is why we refer to this section of these series of articles – “Inside the Mind of Software”.
On one hand, we will be teaching you how to program, which is a very valuable skill in itself. On the other hand, you will utilize your new found programming skills as a way to solve certain problems and create solutions. As we continue throughout these series of articles, these solutions will begin to effortlessly reveal themselves.
WHAT IS SOFTWARE?
Software refers to an electronic program, a set of instructions and commands that directs the software into the performance of certain computation, function or series thereof. The computation or function itself might be of a mathematical nature, as in solving a complex series of mathematical equations or attempting to trace the root of a polynomial, but it can also pertain to computations that are slightly more symbolic in nature, such as being able to search and replace a text in a document, or something more graphical and visual such as the tampering of an image or video file.
The exact technically literate details tend to vary from language to language, but there are a few fundamental instructions that tend to be essential for every language:
– Input: Receive information from the keyboard, a file, the network or some other devices that is used to transmit data into the computer. – Output: Show information on the screen, save it in a file, transfer it over a network, etc. – Math: Execute basic mathematical functions such as multiplication and subtraction. – Conditional Execution: If certain conditions are met, run the code associated with that condition. – Repetition: The repeated execution of an action, with periodic variation.
It may be hard to believe, but this is the essence of how computer programs called software tend to work, and what is required for them to function as they do. Every program you’ve likely utilized for one end or another, regardless of it’s level of sophistication, is comprised of instructions that in essence very closely resemble these. So programming in your mind should be essentially taking a gigantic task that is with a wide breadth of complexity and breaking it down into smaller, bite sized instructions and subtasks and then in turn breaking those further sub-divided into even smaller subtasks until they are able to be performed with one of these basic core instructions mentioned above.
AS IT PERTAINS TO USING PYTHON
One of the issues people often face in order to get to use Python is that they are often required to install it on their computer, together with it’s related software. If you are well acquainted with the operating system that you normally use, and most particularly if you are comfortable using the command line interface within it, then you should find no qualms in installing Python and getting it to work. But for those who are new to system administration and programming, learning these both simultaneously can prove a difficult task.
In order for us to circumvent that problem, it is suggested that you begin by running Python in a web browser. As you gain more familiarity with Python, you will be able to shift to running it on your computer instead.
There are several websites that you can use to run Python on your web browser. If you already have a preferred website that you use, you can go ahead and start using it there. Otherwise, it is recommended to use PythonAnywhere. Here are the instructions for using Python on PythonAnywhere can be found in the 1a. Inside the Mind of Software – Python How-To Guide article, which is meant to be a supplemental article to this one.
There are primarily two versions of Python being widely used today – Python 2 and Python 3. There are stark similarities between these two versions, so learning either will be fine for you to quickly adapt and use the other version. As a beginner, there are only a few slight differences that you will run into that will become apparent as you use them. These series of articles are primiarly intended to be a guide for Python 3, but some tips regarding Python 2 will be included as well.
The Python interpreter is an application that functions in a way where it takes Python code and produces a functioning program out of it as a result. If your environment is one in which it is desktop based, then we most likely launch the application by clicking an icon. You can also use the command prompt to start Python via a command line. When the application launches, it should display something similar to this:
Python 3.4.0 (default, Apr 27 2018, 17:26:43) [GCC 4.8.2] on linux Type “help”, “copyright”, “credits” or “license” for more information. >>>
Breaking down this information, we can see that the first three lines consist of information regarding the interpreter and the operating system it’s using to run, so it might show some other information on your machine. However, it’s adviseable that you look into the version number, which is 3.4.0 in this particular example, so you can see that it does indeed start with 3 and is running Python version 3. If the prompt lists a version number that starts with 2, then you might have already figured out that it is indeed running some version of Python 2.
The line at the end is a prompt which suggests that the interpreter is all set for you to enter code into it. If you put in this line of code and press the Enter key, the interpreter will display the following:
>>> 1 + 1 2
Now you are in the most excellent position to begin your journey. Since we’ve come to this point, we think it’s safe to make the assumption that you have learned how to start the Python interpreter and execute lines of code on it.
WRITING YOUR FIRST PROGRAM
A somewhat of an American tradition has students in computer science write their first program in any new computer programming language and is called “Hello, World!”. The reason for this is because the line of code executes only to display the words Hello, World! on the programmer’s screen. In Python, writing this program requires the subsequent piece of code:
>>> print (‘Hello, World!’)
This is what a print statement looks like, even though it’s printing is not meant to print directly on paper using your printer. This program ends up displaying the words:
Hello, World!
The quotation marks that we put in the line of code show where the start and finish of the text is supposed to be, therefore they are not meant to be part of the result.
The parentheses mark the fact that print is a command, and in this particular case provides a function. We’ll cover more on functions in later articles in these series
Whereas in Python 2, the print statement looks a bit dissimilar to the one in Python 3, as it does not considered a function by the language, and therefore does not utilize parentheses in it’s syntax.
>>> print ‘Hello, World!’
This tiny difference will come together in a more sensical way later on, but it should be sufficient for your first lesson.
ARITHMETIC OPERATORS
After the ‘Hello, World!’ program, the next part of our lesson consists of arithmetic. Python gives you operators, which are symbols meant to function mathematically and computationally like addition and subtraction.
Operations such as +, -, and * perform the functions of addition, subtraction and multiplication, respectively, such as indicated in these examples:
>>> 42 + 3 45 >>> 45 – 4 41 >>> 8 * 10 80
This next operator is used for division computations:
>>> 36 / 3 12.0
You might be flabberghasted as to why the result produced had a decimal point instead of being just a whole number. We will explain that soon.
And lastly, the double asterisk operator elevates the multiplication function to exponentiation, which means it makes the number a power:
>>> 8**2 + 5 69
Nice.
Now, with other languages, they typically use the symbol ^ in order to indicate expoentiation, but in Python it’s considered something called a bitwise operator that goes by the name of XOR.
>>> 6 ^ 2 4
There is no need to go over bitwise operators in these series of articles, but nonetheless you can read about them in the following article:
https://wiki.python.org/moin/BitwiseOperators
VALUES AND TYPES
A value is one of the most fundamental components of a program, and include things such as a single letter or a number. Some values that we’ve been able to ascertain so far have been 2, 12.0 and ‘Hello, World!’.
These values can be attributed to distinctive types: 2 is an integer, 12.0 is a floating point number, and ‘Hello, World!’ is a string, and is called as such because the letters are strung together to form a world.
If you’re unsure to what type a value possesses, the interpreter is a good place to find out this information:
>>> type(2)
As we can see the interpreter has given us some useful information, of which the word class is a sort of categorization that represents the types and what value they belongs to them.
As we can figure out, integers belong to the type int, strings belong to the type str, and floating point numbers belong to the type float.
So how about the values ‘2’ and ‘12.0’? They indeed seem to be numbers, yet they are being put in quotation marks similar to the strings.
>>> type(‘2’)
Suddenly what would be an integer and a floating point would automatically become a string.
When you input a big integer number, you might automatically put commas between groups of digits in order to illustrate the number more clearly for yourself, as you would in the number 2,000,000. This type of usage is not considred legal integer in the Python programming language, but it is legal in and of itself but produces a result you would not expect:
>>> 2,000,000 (2, 0, 0)
This will come as a surprise. Python seems to interpret the 2,000,000 as a comma-separated sequence of integers. We will dive into more details on these types of sequences later on in subsequent articles.
FORMAL AND NATURAL LANGUAGES
Natural languages are languages that were formed naturally over time as a means of communication between human beings in different parts of the world, as is the case with Russian, German and Chinese. These languages are not artificially designed by human beings, even though there are attempts at creating order and structure in the languages on a more formalized basis such as through the education system, they tend to evolve and progress naturally.
Formal languages are languages designed by human beings that have utility in more specific matters. To give you one example, notation in mathematics is a type of formal language meant to differentiate and define the connections between letters and symbols. Formal languages are also used in chemistry as a way to illustrate the chemical structure of molecules. And in relations to what we’re learning:
Programming languages are designed by human beings as formal languages and are meant to convey computations.
Formal languages often consist of syntax rules that are fairly strict when making up the structure of certain statements. To give you an illustration of this, see how in mathematics the statement 4 + 4 = 8 has proper syntax, yet 4+ = 4$8 doesn’t. A similar thing happens in chemistry – where H2O is a syntactically correct formula, 2Zz is not.
Rules regarding syntax tend to come primarily in two different ways, and those ways are tokens and structure. Tokens are the fundamental components of a language, such as words, numbers and chemical components. One of the particular issues pertaining to 4+ = 4$8 is that $ is not a legal token in mathematics. In chemistry, it is also not legal to write the combination of 2Zz as there is no chemical element with the abbreviation Zz.
In the second example, the syntax is not applicable due to how tokens are supposed to be arranged due to the nature of the syntax itself. In this particular case, the equation 4+ = 4 is illegal because despite the fact that both + and = are legal symbols in mathematics, the way they are arranged makes no sense in that the syntax tells us you cannot have one right after another. In chemistry you also have a similar syntactic rule – the subscript is supposed to come before the element name as opposed to after it, in order to legally define a chemical structure using a syntax that is universally readable by all humans.
This is @ well structured Engli$h sentence with invalid t*kens in it. This sentence all valid tokens has, but invalid structure with.
When you read a sentence in the English language or a statement as they’re called in formal languages, the structure is one of the first things you tend to figure out, where in formal languages it is more of a manual process requiring conscious effort, in natural languages it tends to be subconscious. The process of doing this is referred to as “parsing”.
Despite the abundance in shared similarities between formal and natural languages, such as tokens, structure and syntax, there are a few differences that distinguish them:
– Ambiguity: Natural languages possess a plethora of ambiguity, that people tend to filter through to find the meaning of what was said via contextual clues and an assortment of other information. Formal languages on the other hand are by design purposefully and artificially made to be totally unambigious, which presupposes the fact any statement has precisely one meaning, even if the context with which it is used is entirely different each time.
– Redundancy: In order to minimize misunderstandings and compensate for ambiguity, natural languages utilize an abundance of redundancy. As a side effect of this rule, natural languages tend to be very verbose. Formal languages tend to be less redundant and more concise.
– Literalness: Natural languages tend to possess an abundance of idioms and metaphors that can be used as a way to convey meaning. If you were to say something like “The penny dropped”, a fluent English speaker who is aware of colloquialisms will be able to ascertain that there is no penny and therefore nothing dropping, as this phrase, also known as an idiom, means that someone was finally able to understand something after some period of confusion and deliberation. Formal languages are meant to be as literal as can be, to mean exactly what they say.
Since we are all accustomed to speaking natural languages as we were born and raised into learning this sort of communication, it can be quite difficult to get used to using a formal language in a fluent and efficient manner. When it comes to what makes them distinctive, a useful analogy might be comparing natural languages and formal languages to poetry and prose. This is what is meant by this:
– Poetry: A poet is meant to take words and use them both for their auditory pleasurability as well as well as their definition, and the whole poem put together using these strategies is meant to inspire or draw out an emotional response or a sort of mesmerising effect. The use of ambiguity is seldom a coincidence and is meant to amplify the artistic effect of the poem.
– Prose: In prose, the literalness of words is a distinctive and crucial feature, and the structure of how they are put together tends to either amplify or underplay the intended message. Prose can be interpreted with more clarity than poetry but still manages to retain some ambiguity.
– Programs: The meaning embedded in a computer program is meant to be literal and unambiguous, and the writing of a computer program can be understood perfectly by the structure and tokens that are used to write it.
Formal languages tend to have a higher density in the information they possess compared to the length of your typical natural language structure, and as a result of this it takes longer to read a statement in a formal language than a sentence in a natural language. The structure is also very important when constructing a formal language statement, therefore it’s not always a wise idea to read from top to bottom and then left to right. Instead of doing this, it’s better to learn and practice how to parse a computer program inside your own head, making interpretations of the structure and attempting to identify the tokens and symbols and how they all come together to try and convey what meaning. Lastly, the devil is in the details. Even the tiniest mistakes when it comes to spelling and punctuation, which is not considered so bad when it comes to conveying a message in natural languages, are detrimental to the message you are trying to convey in formal languages.
DEBUGGING
It is a widely known fact that programmers are prone to making mistakes. Due to a fable-like anecdote that programmers find funny, errors encountered during programming are aptly named bugs and the process of figuring out how to fix them is called debugging.
Programming, and particularly the process of debugging, has a way of rubbing people the wrong way, sometimes stirring violent and powerful, frustrating emotions in them. If you are finding figuring out a bug in your code challenging, then you may feel enraged, flustered and even embarrassed.
There have been studies conducted whose results suggest that people are naturally inclined to responding to computers as if they were other human beings. When they work as they should, we tend to think of them as compatriots engaged with us in a sort of collegial intercourse, but when they are incompetent, insolent and rude, we tend to treat them as such, and they elicit the same responses in us.
If you take the time to prepare for these very human responses, it will help you cope with them when you inevitably do run into a debugging situation. One way to establish a coping mechanism is to think of the computer as a sort of employee of yours that has positive traits like speed and precision, but also possesses negative traits such as a lack of empathy and the incompetence to understand the big picture.
You can think of your relationship with the computer as somewhat of a good manager would have with this type of employee: You want to take advantage of the positive traits of this employee while minimizing the continual resurfacing of the negative traits that they possess. It is also wise to use your emotions to find ways to work efficiently towards a solution for any problem you might encounter, but do not let them get in the way where it becomes distracting.
While the learning curve that comes with debugging might make you have premature hair loss, it is without a doubt a skill that you can find utility in not just in programming, but in other facets of life as well! At the end of each article in these series of articles, we will provide you with some tips on debugging. Hopefully they help you through your programming journey!
GLOSSARY:
Problem Solving: The process that requires the identification of a problem followed by the subsequent formulation of a solution to that problem, as well as the execution of this solution.
High-Level Language: A programming language similar to Python that in it’s design is made so humans are easily able to read and write it, as it tends to be very similar to how a natural language would look like.
Low-Level Language: A programming language that is designed to be easy for a computer to work with, also called a machine or assembly language.
Portability: A component of a program that is able to execute on more than a single type of computer.
Interpreter: A program designed to read the commands in another program and to execute these commands so that the new program becomes functional.
Prompt: A series of characters or symbols that are presented by the interpreter to notify the user it is ready to receive input from them.
Program: A series of commands that directs the computer to perform a function or series of functions.
Print Statement: A command that forces the Python interpreter to present the user with a value on their screen.
Operator: A particular kind of symbol that stands for simple mathematical computations such as addition, subtraction, division, multiplication or even string concatenation.
Value: One of the most fundamental units used to convey data, which includes a number or a string, which a program is able to manipulate.
Type: Values that can be categorized in the same basket. So far we’ve learned about these various types – integers (type int), floating point numbers (type float), and strings (type str).
Integer: Integers are a type that indicate a whole number.
Floating-Point: A type which consists of numbers with decimals or fractional parts.
String: A type consisting of a sequence of characters, typically used to form a sentence.
Natural Language: A natural language is one that owes it’s evolution to humanity using it, changing it and developing it over time, in a natural sort of way.
Formal Language: A formal language is one that has been arbitrarily desined by humans to help simplify or conceptualize some specific idea, such as mathematical formulations, or with computer programs, and therefore all programming languages can be seen as being formal languages.
Token: One of the most fundamental components of the syntactic structure of a program, comparable to what a word would be in a natural language.
Syntax: The rules that restrict the behavior of a programmer as it relates to coding within the confines of the structure of the program.
Parse: Parsing a program means to analyze it and try and figure out it’s syntactic structure, and therefore the meaning of the program and how everything works together.
Bug: A problem with the program that results in some part of it not working.
Debugging: The process of locating a bug and squashing it, figuratively speaking, or to correct errors in your program that prevent it from functioning as intended.
EXERCISES:
Exercise 1.1:
It’s advisable that you attempt the following examples as you are reading this article.
As opposed to avoiding mistakes, in computer science and experimentation with new features in your development environment, it is encouraged that you make mistakes, because it will help you uncover how stuff works and attain a sense of intuition surrounding it’s functionality and applicability. To give a beginner appropriate example, what do you see when you take out a quotation mark from the “Hello, World!” example? What about if you take both of them out? What if you misspell the word print?
This type of experimentation is useful for memorization of what you read, but it is even more useful when it comes to understanding the error messages you’ll frequently run into in programming. It’s more worthwhile to make arbitrary mistakes now and learn from them as opposed to later on and completely by mistake.
1. If you were to write a print statement, what occurs when you leave out one parentheses, or both of the parentheses?
2. What about with a string? If you leave out one of the quotation marks or both, what appears to happen?
3. The minus sign can be used to make a negative number, such as -3. What would occur if you put a plus sign instead before a number? Try writing the equation 3++3 and see observe what happens.
4. When it comes to math notation, putting a 0 in front of another number is considered legal. But is it legal if we try this in Python? Find out for yourself.
5. What appears to occur if you put together two values with no operator between them?
Exercise 1.2:
Boot up the Python interpreter and use it to conduct mathematical calculations.
1. How many seconds are there in 43 minutes and 43 seconds?
2. How many miles are there according to the interpreter in 12 kilometers? To give you a hint, there are 1.61 kilometers in a mile.
3. If you run a 12 kilometer race in 43 minutes and 43 seconds, what is your average pace? In this case the average pace would be time per mile in minutes and seconds. What is your average speed in terms of miles per hour?