2.2 – Programming fundamentals

🎇2.2.1 Programming fundamentals

This section covers:

  • The use of variables, constants, operators, inputs, outputs and assignments

  • The use of the three basic programming constructs used to control the flow of a program:

    • Sequence

    • Selection

    • Iteration (count- and condition-controlled loops)

  • The common arithmetic operators

  • The common Boolean operators AND, OR and NOT

🏠 Click here to go back to the main page.

Other sections in this topic:

🔗2.2.2 Data types

🔗2.2.3 Additional programming techniques

This page is NOT YET READY


Variables

It is difficult to think of a useful computer program that does not need to store data in some way. The program may ask the user to input some information, or read a value from a sensor - this will then need to be stored before further processing is done and the computer can react.

This data will be stored in the main memory of the computer, which leads us to this important definition:







You should try hard to remember this definition, defining what a variable and constant are as they often appears in the exam.

(...nearly always, you will be asked what both are, and it is very important to remember that the examiners will have been told that if you answer "A variable is a constant that can't be changed", then they should award zero marks)



You should try hard to remember this definition, defining what a variable and constant are as they often appears in the exam.

(...nearly always, you will be asked what both are, and it is very important to remember that the examiners will have been told that if you answer "A variable is a constant that can't be changed", then they should award zero marks)



You should try hard to remember this definition, defining what a variable and constant are as they often appears in the exam.

(...nearly always, you will be asked what both are, and it is very important to remember that the examiners will have been told that if you answer "A variable is a constant that can't be changed", then they should award zero marks)



Constants

As well as storing data that changes over time, computer programs often need to store data that will not change when the program is executing. The data will be stored in exactly the same way as a variable, although this is sometimes slightly different in order to make access to the data faster. Examples of data that may be stored in a constant are the value of Pi, the current VAT rate etc.







Input and Output

A program will usually need some data to start with, and will output the data when it has been processed.

We will need to store any values that have been input, and the most logical way to do this is by storing the value in a variable. in Python, we use a statement similar to this:

name = input("What is your name?")

This will display the prompt "What is your name?" and then program execution will then pause until the user has types something and pressed the enter key. Whatever the user typed will then be stored in the variable name.

It is important to note that the Python input function accepts only string values. If you need the user to enter a numerical value, then you must cast it to either a floating point or integer value:

# This will ask the user for their age, and then convert what they type in from a string value, into an integer value:

age = int(input("How old are you?"))

# This will do a similar thing, but this time the input will be converted to a floating point value:

height = float(input("How tall are you?"))

Python also allows the program to output data using the print() function. Everything inside the brackets will be printed. It is possible to print longer groups of characters by using a "," symbol (this will add a space directly between the two items), or using a "+" character (this will not add a space). Python has some formatting methods as well, so multiple lines of data can be displayed in a tabular form, or a set number of decimal places are used.

# The following will display "Hello John" because the , will add a space:

name = "John"

print("Hello",name)

# This line will display "alltogther" without a space, because the + symbol concatenates the two words:

print("all" + "together")




Assignment

Most computer languages use a single = sign to signify "assignment". This means that the variable or constant will be altered and its value changed.

Unlike other languages, Python does not have a way of defining a constant, the programmer should make sure that they do not inadvertantly change a value by mistake.





Casting.

This is the name given to the technique of changing one data type to another. Some modern programming languages will do this automatically, but others require you to specifically change the data type. Python is one of those languages,





Programming Constructs

In your exam, you will be asked to identify which of three different programming constructs have been used. Watch the following presentation to learn about the different ones.



String Techniques

There are many different ways of manipulating strings. As a very bare minimum, you should be able to find the length of a string, break it up into shorter sections and change the capitalisation of a string.







File Handling



We can also use a file stored on secondary storage to store data. This file can be anything from a plain text file, to a more complex file type such as CSV etc.

When we want to access the file using Python, we must first open the file. In Python, we can also specify a mode - we will look at that later.

filehandle = open('textfile')

This will open a file that is called textfile. If the file does not exist, it will give an error ('No such file or directory'), but if we are successful, it will return a token called a filehandle that we can use to access the file. You could give this variable any name that you wish, but the example above has used filehandle.

We can use the filehandle to read information from the file (input), or write information to the file (output)

fileContents = filehandle.read()

print(fileContents)

This will read the whole file into a variable called fileContents.

data = 'Some data to store...'

filehandle.write(data)

The final and probably the most important operation is to remember to close any opened file. This is done like this:

filehandle.close()

This operation will finish writing any information to the secondary storage, and close the file. If you do not use it, you will lose and possibly corrupt the data stored in the file.


This is the output from the program, each line has been processed separately:



File Access Modes.

As mentioned earlier, it is also possible to specify a mode when you open a file.

filehandle = open('filename', 'w') will open an existing file and enable write mode. You can save data to the file, but you cannot read data from the file.

filehandle = open('filename', 'r') will open an existing file and enable data to be read from the file. The file must already exist (or you will get an error), and you cannot write data to the file.

filehandle = open('filename', 'a') will open a file for appending, creates the file if it does not exist. You cannot read data from the file.

filehandle = open('filename', 'x') will create the file, and will give an error if the file already exists.

In addition you can specify if the file should be handled as binary or text mode

filehandle = open('filename', 't') - Text - Default value. Text mode, if you do not specify text or binary, then text mode is used.

filehandle = open('filename', 'b') - Binary - Binary mode (e.g. images)

The final two options are always used with one of the first four (so the complete set of options are wt, wb, rt,rb,at and ab

When using file handling in Python, it is usually best to read an entire file into an array, then perform any operations, and finally reopen the file in w mode and saving all the data.



Records and SQL



DB Browser for SQLite

Open the DB Browser app which is under the ICT menu on the school system. You can easily install this at home by searching for it on Google, or ask me and I can supply the installation file for free (It is open source) Open the file 2.2 Lesson 5 - world.sqlite3 from my teachers folder.

Spend a few minutes looking through the app.

Browse the data - what does it contain?

How many records?

What are the field names?






SQL (Structured Query Language, pronounced SeQueL)

SQL databases are organised as tables which are made up of a number of fields and store records of data. You can find and manipulate information in the database using SQL statements which are made up of human friendly commands. The SQL command can be written in upper or lower case but it is normal to use UPPERCASE for the SQL commands, and lowercase for table name, and field names.

The SELECT command is used to literally select fields from a table. You can select inidividual field names (e.g. SELECT firstname FROM students), but you can also use a wildcard to select all the fields at once. The symbol for all is a *

The SQL statement SELECT * FROM students will return all the fields for all of the students stored in the students table. This is not a particularly effective statement, but it becomes a lot more powerful when you add a WHERE clause to the end.

SELECT * FROM students WHERE firstname="Paul" - this statement will return all of the records from the students table that match the criteria firstname="Paul"

You can also combine more than one criteria, connected with AND, OR, and NOT statements:

SELECT * FROM students WHERE form="9Mauritius" AND gender="M" will return all the boys from the 9 Mauritius form. Once again, there are some wildcard characters that we can use.

The % symbol is used to represent zero, one, or any number of characters, so name=P% would match any name that had a capital P at the start.

Have a look at these Wildcard queries, taken from the W3C School SQL tutorial website.







We will often want the results from the query to be sorted into order, either alphabetically or numerically. We can do this by adding ORDER BY to the end of our SQL statement.

SELECT * FROM students WHERE form="9Mauritius" AND gender="M" ORDER BY surname will sort the results of the query into alphabetical order of surname.

Adding, Updating and Deleting records

If the table above, which is called "world", we can add a new record using the SQL:

INSERT INTO world VALUES('United Kingdom', 'Europe', '242900', '64105700', '2471600000000', 'London')

When you use this command, it is important that you add the correct number of fields, and surround each item in speech marks, otherwise you will get an error message.

UPDATE world SET area=242890 WHERE name='United Kingdom'

This SQL will alter one or more fields. You have to specify where the data is that you want to change (the world database), what specifically it is that you want to change, and to what (area=242890) and lastly add a criteria clause so that only one record is altered. If the criteria clause was missed off in this example, then every single record in the database would be updated.

DELETE FROM world WHERE name='United Kingdom'

The delete statement should be used cautiously as you could potentially delete the whole database if you make a mistake! The actual statement is easy to understand, we are deleting a whole record from the world table that matches the criteria of having a name of 'United Kingdom'



Using SQL with Python

We are able to use SQL with Python, in fact you will probably need to use it when you tackle your programming project. When we want to use SQL, we always need to follow these steps:

Step 1: Import the sqlite3 module using this code

import sqlite3

Step 2: Create a connection to the database file, which is known as a cursor. For simplicity, make sure that the database is saved into the same folder as your Python program. Here is the code:

cursor = sqlite3.connect('world.sqlite3')

Step 3: Execute a SQL query, using this code:

results = cursor.execute("SELECT * FROM world WHERE name LIKE 'A%'")

Because the SQL statement needs to contain speech marks, you need to pay close attention to how you use single and double quotes.

Step 4: We aren't quite there yet! We now need to fetch the results:

data = results.fetchall()

This returns a multi dimensional list, with one row for each record. We can print it like this:

for record in data:

print(record)



Here is the full Python program, with the results shown below. You can see that it has returned a two dimensional list, which we have printed one line at a time. Each line in the printout is also a list, so record[1] would be the continent etc.

Click here to visit a really good tutorial on using SQL with Python.



Arrays



As we saw on the previous page, we can store data in variables which can be altered by the computer as a result of a programmed operation. The data can be different data types, such as text, integer numbers, floating point numbers and Boolean - each data type is stored in a slightly different format and it is essential that the data type is taken into account when any calculations or operations are done to the data.

For very simple problems, this is enough. We can use a separate variable for each item of data that is stored and the program will function correctly.

Of course, computers are not really needed for simple problems, it is only when the problems get really complicated that we turn to a computer.

In reality, we usually want to store a multiple values of each data item. A good example is if you consider a teacher storing the scores that the students got in their exams. The teacher will need to store many different students names - we could get around this by using variables called "student1", "student2","student3", etc., and exam scores as "score1", "score2", "score3" to match each student.

This would lead to a program that would be very difficult to understand, and would be a nightamre to maintain! What would you do if the class size changed, and how would you find a mistake in the code?


Can you imagine how difficult it would be to find a mistake in code written like this? What would it look like if there were thirty students?



Arrays in Python (Lists)



An array looks similar to a normal variable, followed by brackets which contain a number. The number is referred to as the 'index'

All programming languages deal with arrays in a slightly different manner, and Python is no exception. In Python we refer to arrays as lists and the index goes inside a square bracket.





We can initialise a new list by setting its value to an empty list, like this:

name = []

...or we could initialise it to a list of three elements like this:

name = ["John", "Mary", "Peter"]

To access one of the elements, use a number inside the square brackets (indexes start at zero, so the first element is [0])

print(name[1])

...will print out

Mary

We can also use a for loop to iterate through the list...

for person in name:

print(person)

This will set the variable 'person' to each value in the list, one at a time, and will display:

John

Mary

Peter

We often need to add an item to a list, we can do this by using this code:

name.append("Lucy")

print(name)

This will print out:

['John', 'Mary', 'Peter', 'Lucy']

We can find out how many items are in the list using the len() function:

print(len(name))

Will display

4

You can remove all items matching certain criteria using .remove()

name.remove('Mary')

print(name)

This will display:

['John', 'Peter','Lucy']



Two Dimensional Lists

It is a quite common requirement when we are writing a program that uses lists to store items that are connected in some way. We need to store firstnames and surnames, and possibly students addresses and scores for their exams.

We can do this using a two dimensional list. Instead of just one index, we can have two - think of them as being the row and column numbers:

students[0][0] = "John"

students[0][1] = "Wayne"

students[0][2] = "10 Poplar Avenue"

students[0][3] = 12.5

If this example, we can see that all of the data in row [0] is linked together.

The code is valid Python, but it is more normal to add a full one dimensional list to another like this:

students = []

students.append(["John", "Wayne", "10 Poplar Avenue", 12.5]) # Add a list to our students list

students.append(["Billy", "Elliot", "15 High Street", 14.2]) # ...and add another.

There are two ways that we can iterate through this two dimensional list. The first method uses a control variable which will take the value of each row on each successive loop, so child will be ["John", "Wayne", "10 Poplar Avenue", 12.5] in the first iteration and then ["Billy", "Elliot", "15 High Street", 14.2] in the second. We use a second for loop to iterate through the loop, in this case the variable item. Item will be "John", and then "Wayne", and the "10 Poplar Drive" etc.

for child in students:

for item in child:

print(item)

The second method uses two for loops, and we can use the len() function to determine how many items are in the rows and columns. This code produces exactly the same result:

for x in range(len(students)):

for y in range(len(students[x])):

print(students[x][y])



Arrays are great ways of storing data but cannot cope with large volumes of data very well, particularly when we need to search and filter the data. To do this, we need to use a database.

A database is an organised collection of data. Data is stored in tables, and the tables can have any number of fields. Each item added to the table is referred to as a record. The data is stored is a fairly standard format, which can be accessed using a database browser program.



Functions and Sub Programs



Python 3 uses functions a lot and most of the reserved words that it uses are actually functions. Let's examine an example:

print('This is a function', 'that can take multiple arguments')

This is the print function which takes values inside the brackets and displays them on the screen. To do this, Python uses a large section of code that can vary from one operating system to another. The programmer will never know this, as all they see is the print function.

We can easily make new functions:

def sayhello(name):

print('Hello', name)

This will give us a new reserved word, that we can use anywhere in our program. Whenever we type sayhello('John') then the message 'Hello John' will be printed to the monitor.

Difference Between a Function and a Procedure

A function is able to return a value, so for example:

total_payable = calculate_VAT(sale_price)

This line uses a function called calculate_VAT() which is passed the parameter sale_price and returns a value which is stored in a variable called total_payable.

On the other hand, a procedure does not return a value. An example of a procedure may be a subroutine that displays a game board on the screen.

Functions and procedures, also known as subroutines, make program code much easier to understand. By using a modular approach (sometimes called black box programming and more formally known as object orientated programming), then larger problems can be broken down into smaller sections. In the above example, we could easily change the function to output 'Hello John, how are you?' and by changing just the function, every part of the program that called the function would be updated.

The variable (or variables) inside the brackets are known as parameters. When the function is called, whatever is inside the brackets will be passed to the function and the function will set the variables in the brackets to those values.

Functions can also return values. Values can be passed to a function by including them inside the brackets, but you can also use them like this:

def includeVAT(price):

VATrate = 0.2

return price + price * VATrate

This will make a new function that will add VAT (20% or 0.2) onto any value that is passed to it. We can use the function like this:

price = 4.20

totalPrice = includeVAT(price)

print(totalPrice)

Variable Scope

When you use Thonny, you will notice that it display some of the variable names in italics. That is because the variables inside a function are known as local variables and thier scope is said to be local. in the above example, we created a new variable called VATrate inside the includeVAT function, and set it to the value 0.2. If you try and refer to this value outside of the function, then you get an error. Variables outside of the functions are said to have a global scope.

We can illustrate this with the following example:







Data Types

Numeric data can easily be stored in a computer system but converting the numeric data to binary and storing the data as a series of on and off signals. Values up to 255 can be stored in single bytes and larger numbers use more than one byte to make a sixteen bit (or greater) value.

This kind of simple data is known as integer data and variables that store integers are said to be on an integer data type.

Most numerical data will need greater accuracy that a whole number (integer). This numeric data can be stored in a real or floating point data type variable. Very large numbers can be stored using multiple byte memory locations.

Of course, we will often need to store textual information. To do this, we store each single letter as a one or two byte number, so for example, every capial letter A is stored as the number 65. This data typs is known as character (single characters) or text.

A less obvious data type is Boolean. A Boolean value (it is named after George Boole, so should be spelt with a capital B), can be either true or false. This data type is used to represent logical values, and can take up a very small amount of space as it only need a single bit.



Arithmetic and Comparison Operators.







Logical (Boolean) Operators

We can also use these three logical operators:

      • AND The returned value will only be true if the left hand and right hand side are both true.

      • OR The returned value will be true if either the left hand or the right hand side are true.

      • NOT The returned value will be the opposite, ie if the answer is initially true, then it will return false, and vice versa.