Login
Password

Forgot your password?

Learn How to Program a Computer - Fun With Data

By Edited Jan 25, 2014 0 0

Programming a computer is a simple concept that can even be taught to children. The core of all programming languages is the storage and manipulation of data. The computer is designed to process data, but what is data? Data is whatever we need it to be. To the computer, all data is just numbers. The first thing that a computer does when it starts is to retrieve some data and attempt to execute it as an instruction. The programmer must ensure that the first data that the computer finds is a valid instruction that starts to tell the computer what to do. The computer then gets the next instructions as it runs. Eventually, Windows starts and the computer is ready to process our data. To do this, we have the computer execute our program. The programming language Python is highlighted by this "Learn to Program a Computer" series. All further references will be to this language. If you are using something else, simply translate the shown commands to those applicable to your language.

Python is a programming language that is very popular and easy to learn. It is free to install, runs on most all types of computers and can be used to build applications that are full of features. The language supports graphical elements such as drawings, windows, buttons and the other standard computing items that are now recognizable by most computer users. Python is building in popularity and it has an active user community ready to offer support to newcomers.

Our Python programs have data that we want the computer to process. Since the computer regards all data as numbers and has no preference, it is up to us to decide what data is for. We must establish certain rules about the data. For example, an accounting program will need to maintain an account balance. While some programming languages dictate that you must declare your data in advance, Python does not. To set up a balance, we can start using it:

balance = 0.00

This establishes an area of memory that is called "balance". Each time our program refers to this data, the computer will go to the memory and get the value stored there. When we change it, the computer stores the new amount back in memory. While there is lots of memory available to store data, there is no computer data that remembers what just happened. If the program calls for the variable "balance", the computer must retrieve it. If the program wants "balance" again, the computer retrieves it again. Each separate reference causes the computer to retrieve the variable from memory. The computer can't remember where the data is just because a program recently used it. There is a symbol table, a cross reference that provides the mapping between the memory location and our name for it. Here is the current (simplified) symbol table for our program:

Symbol Location
balance 88291F1

The symbol is what we called our data and the location is the address in memory that corresponds to our data. Your computer will have many millions of individual memory locations, each of which has a unique numerical address. Each will be used to store whatever we need it. One location will store "balance", some locations store our program, others store data needed to run Windows and so on. Think of the computer memory as millions of post office boxes. Each stores something different. Often, they can only store similar items, like letters. Others can store letters for a while, then different sized boxes. Certain things, like water, can't be directly stored in a post office box. If it is packaged correctly, however, water can be stored, but it isn't necessarily recommended. The same goes for data. It is up to us to use the memory locations to correctly store our data. Data might not fit unless it is correctly formatted.

Python keeps additional information about our data. We specified that balance has a decimal point. Python classifies our balance data at "float". We are only concerned with the name of our data and the type. Only the computer needs to know the location in memory. If we next want data to count something, then we establish that as well:

count = 0

This sets up a memory location called "count". Because there is no decimal point, Python classifies "count" as an integer, that is, it will hold whole numbers without decimals, (1, 2, 3, 4). The simplified symbol table now contains:

Symbol Location Type
balance 88291F1 float
count 8829115 integer

Since Python recognizes a data variable as soon as you reference it, there can be unintended errors. Consider these two lines:

balance = 1000.0
balanc = balance - 10

The current symbol table is now updated to include a new float variable called "balanc". The computer doesn't know that "balance" was misspelled. This is an unfortunate error that will often be hard to detect.

In addition to integer and float variables, Python has strings, lists and classes. Only strings are of immediate use to the beginning programmer. A string is simple text. For example:

error_message = "The value specified is invalid at this time"

The symbol table now contains:

Symbol Location Type
balance 88291F1 float
balanc 8829125 float
count 8829115 integer
error_message 882923B text

A text variable in Python supports certain operations like addition. More characters can be added to a string:

error_message = error_message + " and place"
print(error_message)

When run, these lines generate:

The value specified is invalid at this time and place

Subtraction is not supported for strings, but substrings are:

error_message[0:19] results in "The value specified"

Note that the numbers in brackets refer to the first and last characters in the substring and that the numbering starts at zero. Try the following substring references in Python:

error_message = error_message + " and place"
print(error_message)
print(error_message[0:3])
print(error_message[20:22])
print(error_message[25:30])

When you are using descriptive words for your data, it is best to give them meaningful names. The account balance could be called "balance". You could add words together like "account_balance". Notice that the joining character is an underscore, not a hyphen. That is because "account-balance" could easily be taken to mean "subtract balance from account". There are many characters that are invalid in variable names. If in doubt, (and to maintain compatibility with other languages), programmers should only use letters, numbers and the underscore in variable names. You should be aware that many programming languages are case sensitive, including Python. "Balance" and "balance" can exist at the same time. This can be very confusing to all programmers, including the author. Be aware of this when errors occur. Such attention to the naming of variables is part of the program design that you control. The concept is known as structured programming. Here are some examples of good and bad variables:

Good Bad
balance bal
account accnt
tax_rate t_rte
sales_date s_dte

if balance > order_amount:
ship_order = True

if bal > a_amt
s_ord = True

Which of the above statements is easier to understand?

Abbreviations of names used to be common in order to save memory and speed typing. Both of these goals are now obsolete. Memory is a very cheap resource now. Typing speed is better aided by training and good keyboards. Both were not available back in the 1950's when short variable names were common. The concept of structured programming will take another article to completely describe. For now, strive to make variable names as meaningful as possible.

As you can see, data can be fun, or at least easy. After all, the computer does most of the work. It is just up to us to keep the data correctly flowing into and out of our variables. We have to keep them organized in our mind, the computer keeps them organized when our application runs. What you do with your data variables is part of program design which should be the next subject that you learn.

Advertisement
Advertisement

Comments

Add a new comment - No HTML
You must be logged in and verified to post a comment. Please log in or sign up to comment.

Media

Explore InfoBarrel

Auto Business & Money Entertainment Environment Health History Home & Garden InfoBarrel University Lifestyle Sports Technology Travel & Places
© Copyright 2008 - 2016 by Hinzie Media Inc. Terms of Service Privacy Policy XML Sitemap

Follow IB Technology