The Raspberry Pi offers a range of programming languages that include Python, C, C++, Ruby, Tiny Basic, assembly and more. Along with these languages come a host tools that allow you to program and examine code on the Raspberry Pi. With so many languages available which one should you select? This is normally a matter of preface or a matter of necessity. For example, PHP is used as a programming language for creating dynamic web pages on the internet.
Although PHP can be used from the command line its real power is displayed on the web. If you want a fast powerful language then you would use some type of assembly language because it is faster. This does however come with a price and that price is that it is harder to understand.
Python is a great language because it is powerful and at the same time is very easy to understand. Python has a built in program called IDLE which is an integrated development environment (IDE). This will allow you to program directly into it and see the results line by line. This has the advantage of seeing an error before you out it into a file.
With other languages you will create a file which is called the source file and this source code is compiled into an executable program. This program is executed and the results are displayed when the program is running.
Programming languages have some basic common traits which include syntax, variables, control structures and data structures.
The syntax is the structure of statements in a computer language much like the arrangement of words in a sentence. They must appear in the correct order in order for the Raspberry Pi to understand what to do.
Stand up, turn 90 degrees to the left and walk 10 paces forward.
If this appears in this order
Turn 90 degrees to the left, walk 10 paces and stand up.
You can instantly see at the end of the sentence that the person was not standing up so how did they manage to walk 10 paces to the left.
A variable is used to store some type of value from numbers, text strings, space ships, house location, credit card numbers and ID numbers to name just a few. A variable is usually named with something meaningful that relates to the value being stored.
For example if we are programming a computer game and you are storing the number of lives left in a game then a variable may look like this.
Lives = 3
This is assigning the number 3 to a variable called Lives. Another example may be that you want to store a person’s name.
Name = “Homer Simpson”
Control decisions are used to determine which direction the program will take. For example in a role playing adventure game you may we arrive at a cross roads and you have the option to turn left or right. Based on your decision the program will take a different path in the code. Here is an example.
if the direction is left then
walk down the mountain
else if the direction is right then
walk up the mountain
Another example might be to allow a player to upgrade their car if they have enough money.
If money is greater than 100
allow the player to buy the new car
These control structures can also be combined for example,
If money is greater than 100 and the player is female then
allow the player to walk across the bridge
Another type of control structure is a loop. A loop is a repetitive block of code that repeats and may exit when a certain event occurs. For example a person is only allowed to purchase food from a takeaway food outlet when their total spend is greater than $10 dollars.
While the cost of food is less than $9
Buy a burger at $2
Take the food
When the person has purchased 5 burgers they are able to take the food away. Each time a burger is purchased is adds $2.
The first pass through the loop is $2. The next pass is $4, $6, $8 and on the final pass it will become $10 dollars. When the cost becomes greater than $9 dollars it will exit.
A data structure is a way of storing organised data making it easy to access. For example to describe a house address you will need to specific way to store this data. Take a look at the example below.
Street number = 17
Street Name = “Harlow Avenue”
City = “London”
Post Code = “SW1”
This data could be contained in a data structure labelled House address or Location. This type of structure really comes into play when we use Arrays. Arrays are used to store a list of similar items just like the address above. Take a look at the image below to understand why data structures are powerful.
You can see how the data structure is common across the house locations and a way to access each house location in by calling upon its number. If you want to retrieve the data in Paris, France then you could write.
Get House Location 
Data structures will appear often so if at this stage it’s a little fuzzy don’t worry you will understand them over time.
Writing programs are never simple. You will at some point have a bug. A bug is a problem with the code which isn’t immediately apparent and therefore you will have to go through the section of code that is causing the problem.
Take the following example which should allow Jane to take out money from her bank account.
Input_amount = 10
if input_amount greater than 5 then
dispense £5 from the account
Take a look at the above code to see if you can spot the problem. Jane will only ever receive £5 if the amount is greater than 5 even though her input amount for £10. Debugging is an art form and you will learn more as your begin programming. Programming and debugging go hand in hand.
Next Raspberry Pi Beginning Python – Day 2 >>