Trace: » basics_of_programming

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

You don't have an account yet? Just get one: Register

Forgotten your password? Get a new one: Send new password

This stuff is here as a learning exercise (for me and hopefully for you too) in the hope it makes the information easier to remember and refer back to. If you’d like to edit it please do. I’ve used this information as a guide. If you’re looking for a python introduction, visit that link for a good primer.

Programming Basics

Data

Data is the raw information that can be/is manipulated by the program. Data can have operations performed upon it; for example, mathematical operations such as add or subtract.

Variables

Variables are labels attached to instances of data. It helps to think of a variable as being a label attached to a mailbox. The mailbox contains letters (data). The variable (label) makes sets of data easier to find.

thisIsVariableOne

Character Strings

A string or sequence of characters. For example

"this is an example of a character string"

Integers

An integer is any whole number (a number without a decimal point) through all values from negative to positive. Signed integers are numbers from negative to positive whilst unsigned integers are positive only. The reason for this is that on computers, there is a maximum amount of numbers which can be used. When using unsigned integers, negative numbers are unused to allow a larger range of positive numbers.

1234567

Real Numbers

A real number is also known as a floating point number. This is a number with a decimal point.

1.23

Boolean Values

Values used to test if something is true or false. Boolean operators allow the application of operations to data to establish whether a value is true or not.

Operator Example/s Description Effect
A and B AND True if A,B are both True, False otherwise
A or B OR True if either or both of A,B are true - False if both A and B are false
A == B Equality True if A is equal to B
A != B, A <> B Inequality True if A is NOT equal to B
not B Negation True if B is not True

Collections/Containers

Exactly what it says on the tin. A collection is just that - a collection of things. Collections come in a few flavors.

Lists

Like a shopping list. A sequence of items that you pick through one at a time to find what you want.

[1,2,3,4]

Tuples

Just like a list only you can’t append or remove a value from it.

Dictionaries/Hashes

In a similar way to a literal dictionary, a dictionary in programming associates the values held within it, with a key. This makes it more powerful than a list, as you can use the key to find an item later.

['farm'] = "a collection of animals"
['garden'] = "a collection of plants"

In the example, farm is the key, and “a collection of animals” is the value. If we wanted to find our value later, we could retrieve it using the key rather than having to pick through all of our data (as in a list). The garden and “a collection of plants” is the next key and value in the dictionary.

Arrays

A list of items indexed for easier retrieval. An array differs from a list because you have to specify how many items it will store up front.

Stacks

Like a stack of trays in a restaurant, each new item sits on top of the one below, when you remove an item, you remove it from the top of the pile. Last in First Out.

Sets

Sets store one of each type of item. Just like a sticker album...

Queues

Just like a stack, only the first item into the queue is also the first item out again.

Classes

A class (see object orientated programming) is several bits of data grouped together as a record. You could use a class to group together different types of data.

Class Address
     Public  int    HouseNumber
     Private  string Street
     Public  string Town
     Public  string ZipCode
     Private string Telephone 
     
     public setStreet(string s){
         this.Street=s
     }

     public getStreet(){
         return this.Street
     }

End Class

The example above shows a class called address which contains variables addressing data of types integer and character string. If you think of an object as a black box containing data then the way to get to the data could be via buttons or levers on the box. In the above example of code the way to get to the data would be via the button (or normally called a ‘method’) ‘setStreet’. This method takes in a string (held temporarily in the variable ‘s’) which is used to populate the private variable Street. The only way to change the ‘Street’ variable is via this method, and the only way to get the information held in the ‘Street’ variable is to use the ‘getStreet’ method.

An example of the code used to populate and retrieve the ‘Street’ variable:

   string streetInfo                        # a local variable used later on..
   int    numberInfo                        # a local variable used later on..

   Address myAddress = new Address()  # creates a new object called myAddress
   myAddress.Number=10                # populate the public 'Number' variable with the integer '10'
                                      # because it was made 'Public' we can address it directly
 
   myAddress.setStreet("Somewhere Street") # use the 'setStreet' method, passing it a string

   streetInfo=myAddress.getStreet()  # now lets get that street information back and place it into 
                                     # our local variable made earlier..

   numberInfo=myAddress.Number # get the number from the address object, accessing it directly

So that’s setting and getting of private and public variables, but I hear you cry why would i every use ‘methods’ to access variables? I could just make them all public and then access them directly, I wouldn’t have to mess around with all that extra coding? but.... think of this scenario... You just made a mission critical application and your Uber code goes live using the above object, then a user enters -10 in as a number?