Computer science > Programming and Algorithms
Algorithms and Interpreting Them
Algorithm - a precise method for solving a problem
(Command) Sequence - an ordered set of instruction
(Command) Sequence - an ordered set of instruction
-
Pseudocode
-
Flowcharts
-
Python
<
>
Pseudocode (fake code) is used as a means of displaying an algorithm often to plan code. There is no set pseudocode, it is simply interpretable and translatable so it can be converted into a programming language. Pseudocode allows you to focus on the logic of a problem without worrying about the rules of a programming language.
We are using our own Pseudocode on our pages (inspired by Edexcel Pseudocode)
We are using our own Pseudocode on our pages (inspired by Edexcel Pseudocode)
Image from BBC Bitesize
Python is a high-level programming language (more specifically a scripting language) which can be used for a variety of tasks (making it a general purpose language).
When programmers learn a new language, it is customary to write a "Hello World" program. A "Hello World" program does practically nothing apart from outputting the words "Hello World" to the screen. It is a good way to learn the general syntax of the language and get a feel of what writing in the language might be like.
In Python, a "Hello World" program is incredibly simple to write, requiring only one line.
When programmers learn a new language, it is customary to write a "Hello World" program. A "Hello World" program does practically nothing apart from outputting the words "Hello World" to the screen. It is a good way to learn the general syntax of the language and get a feel of what writing in the language might be like.
In Python, a "Hello World" program is incredibly simple to write, requiring only one line.
helloWorld.py
Great! If all is well, you should see the words "Hello World" outputted to the screen (this will change depending on how you are choosing to edit your python code). If this doesn't happen, you might get an error. This is most likely not a big deal and you should compare your code with the above to ensure that it is typed correctly. Make sure that print doesn't have a capital 'P' and there are two speech marks and brackets.
Variables
Variables are very important in any programming language, and Python is no exception. We can think of variables as a box. Our box will obviously store something and might have a label. This label will tell us what is inside the box and if we open our box, then we can do whatever we want with the contents. A variable is like this. When we declare a variable, we create a point in our computer's memory which will contain a value. We will also give our variable a name which our program will recognise the next time it comes up, and it will "open the box" (by visiting the variable's memory address) to see what is inside.
Python is a weakly typed language which means that we don't need to specify the data type (we will talk more about data types in a second).
Variables
Variables are very important in any programming language, and Python is no exception. We can think of variables as a box. Our box will obviously store something and might have a label. This label will tell us what is inside the box and if we open our box, then we can do whatever we want with the contents. A variable is like this. When we declare a variable, we create a point in our computer's memory which will contain a value. We will also give our variable a name which our program will recognise the next time it comes up, and it will "open the box" (by visiting the variable's memory address) to see what is inside.
Python is a weakly typed language which means that we don't need to specify the data type (we will talk more about data types in a second).
helloWorld.py
Now we have created a variable called myVariable, containing the contents of the words "Hello World", which we can use in our program:
helloWorld.py
The above code is essentially doing what our first code snippet did, except now we have created a variable containing what we want to output, and the computer uses that to find out what to output.
Data Types
We have created a variable called myVariable which contains some words. That's great, but what if we want to store something else, such as a number? Here, we must use a different data type.
Data types are what you might think they are, types of data. They are as follows:
Data Types
We have created a variable called myVariable which contains some words. That's great, but what if we want to store something else, such as a number? Here, we must use a different data type.
Data types are what you might think they are, types of data. They are as follows:
Name |
Description |
Examples |
Int (integer) |
A whole number that can be either positive or negative |
96, 21, -2 |
Float / Real (Floating point) |
A decimal number |
1.5, -2.9, 3.1415 |
String |
A collection of characters, represents words or numbers with no meaning (such as telephone numbers) |
"John Doe", "Hello World", "Apple" |
Boolean |
A true or false value |
True, False |
Please note that the above table applies to Python. Other programming languages (such as C and C++) have various other data types but these data types follow the same principles as the above, it's just the binary representation of the data that is different.
Let's write a simple program to see all these data types in action
Let's write a simple program to see all these data types in action
factFile.py
The above program creates a variable called name, containing a string, an age, containing an int, a height in feet and inches, containing a float and a likesPython variable which contains a boolean.
The above program is also quite boring however, so let's make our user enter in our data and output our factfile. This requires the use of a new function called the input function, which will take whatever our user enters in and puts it in a variable. Between the brackets we can put in a message that the user will see.
The above program is also quite boring however, so let's make our user enter in our data and output our factfile. This requires the use of a new function called the input function, which will take whatever our user enters in and puts it in a variable. Between the brackets we can put in a message that the user will see.
factFile.py
Note the use of the plus to append our string. Because we want our output format to be something along the lines of 'Name: <your name>', we need to add our name variable to the end of our 'Name: ' string and print that out.
When you run your program now, you will see that you will be prompted to enter in something. Type it in and press enter.
There are several issues with our program, though. Firstly, we can enter in anything we want because there are no restrictions on what we can enter. Secondly, the input function will give us a string, which means that all our variables are of the string data type and if we wanted to add 3 to our age for example, we would get an error because it is not an integer. Let's fix this by casting our variables to the appropriate data type.
Casting will convert our variable from one data type to another. However, we will get an error if our new data type cannot properly represent the old data. For example converting the string "100" to an int is fine, but converting the string "One hundred" to an int is not.
When you run your program now, you will see that you will be prompted to enter in something. Type it in and press enter.
There are several issues with our program, though. Firstly, we can enter in anything we want because there are no restrictions on what we can enter. Secondly, the input function will give us a string, which means that all our variables are of the string data type and if we wanted to add 3 to our age for example, we would get an error because it is not an integer. Let's fix this by casting our variables to the appropriate data type.
Casting will convert our variable from one data type to another. However, we will get an error if our new data type cannot properly represent the old data. For example converting the string "100" to an int is fine, but converting the string "One hundred" to an int is not.
factFile.py
Let's see what our program is doing:
We take in a name which is already a string so needs no casting.
Then we take an age and convert it to an int.
Then we take a height and convert it to a float.
Then we take in whether or not the user likes python and convert to a boolean.
Now we output our name which is already a string so we can append it with no issues.
However, our age is an int and needs to be converted into a string in order to be appended, so we cast it to a string and do the same with the others.
Control Flow
Until now, our program has been running line by line (see the sequence programming construct below). However, sometimes we need bits of code to be run multiple times, or only under some conditions. This is called control flow.
First, we will look at if loops. If loops take in a boolean value and evaluate whether it is true or false, only running a designated piece of code if it is true. The syntax for this is below:
We take in a name which is already a string so needs no casting.
Then we take an age and convert it to an int.
Then we take a height and convert it to a float.
Then we take in whether or not the user likes python and convert to a boolean.
Now we output our name which is already a string so we can append it with no issues.
However, our age is an int and needs to be converted into a string in order to be appended, so we cast it to a string and do the same with the others.
Control Flow
Until now, our program has been running line by line (see the sequence programming construct below). However, sometimes we need bits of code to be run multiple times, or only under some conditions. This is called control flow.
First, we will look at if loops. If loops take in a boolean value and evaluate whether it is true or false, only running a designated piece of code if it is true. The syntax for this is below:
Programming Constructs
Variables
Variables are stores of data. Each variable stores one piece of data under a variable name and can be changed or used throughout a program. Variables can be declared and initialised:
Declaring - Setting the type of data the variable stores
Initialising - Setting the data a variable stores
Iteration
Iteration is repetition: When you repeat an algorithm or sequence, you could write it out again an again but it is significantly more efficient to use an iteration statement. These are for, while and repeat...until loops
Selection
Selection chooses between going through a block of code or not. This is done with if statements:
Subroutines
Subroutines are algorithms or modules for performing specific tasks. They are either functions or procedures.
Parameters are values that are passed into a subroutine when it is called.
Procedure
In both cases, the parameters were a and b (3 and 7) but not all functions and procedures need parameters.
Variables are stores of data. Each variable stores one piece of data under a variable name and can be changed or used throughout a program. Variables can be declared and initialised:
Declaring - Setting the type of data the variable stores
Initialising - Setting the data a variable stores
- SET age TO [INT] 16
- In the above line of pseudocode, the variable (age) is being declared as an integer and initialised as 16
Iteration
Iteration is repetition: When you repeat an algorithm or sequence, you could write it out again an again but it is significantly more efficient to use an iteration statement. These are for, while and repeat...until loops
- SET age TO (INT) 16
- FOR i FROM 1 TO 10:
- age += 1
- END FOR
- SET age TO (INT) 16
- WHILE age > 10:
- age -= 1
- END WHILE
- SET age TO (INT) 16
- REPEAT:
- age += 1
- UNTIL age>25
Selection
Selection chooses between going through a block of code or not. This is done with if statements:
- SET age TO (INT) 16
- IF age = 16:
- SEND "Good Luck" TO DISPLAY
- END IF
- SET age TO (INT) 15
- IF age = 16:
- SEND "Good Luck" TO DISPLAY
- ELSE IF age = 15:
- SEND "Good Luck Person" TO DISPLAY
- ELSE:
- SEND "Hello" TO DISPLAY
Subroutines
Subroutines are algorithms or modules for performing specific tasks. They are either functions or procedures.
Parameters are values that are passed into a subroutine when it is called.
Procedure
- A subprogram that contains a set of instructions it carries out when called without returning a value to the main program.
- PROCEDURE add (a, b):
- SET sum TO a + b
- END PROCEDURE
- add (3, 7)
- A subprogram that contains a set of instructions it carries out when called that returns a value to the main program
- FUNCTION adding (a, b):
- SET sum TO a + b
- RETURN sum
- END FUNCTION
- c = adding (3, 7)
In both cases, the parameters were a and b (3 and 7) but not all functions and procedures need parameters.