Programming Concepts

No GCSE exams I am aware of require you to use a specific programming language to answer questions. In this section however, we are going to look at the fundamental programming principles using Python. Once you have the basic skills displayed here it becomes easy to combine them to solve complex programming problems.

Python is a programming language, used to give instructions to computers. In theory, it can be used to create anything you can use on a computer, from basic tools like calculators, to complex programs and games.

Programming languages are used to tell computers what to do. Open a document, type a word, play a game, all of these things have had to be programmed by a human before they can be done by a computer, using lines of code which tell a computer what you want it to do, and when you want it to do it. Computers don’t actually speak Python – in fact they don’t speak any language other than 0s and 1s. Programming languages are a nice intermediary way of joining computers’ binary code with our English language. They are just a middle ground between what we speak and what they speak. After all, you wouldn’t want to be programming a piece of software with just 0s and 1s

Python is a nice introductory language for students to learn. Compared to C++ and Java, it has a nice friendly structure and in only a couple of lessons you can be creating your own programs from Scratch, learning as you go!

You may notice that this page is pretty long. Rather than split it up in to separate pages, I have decided to use a single page and then use anchor links which are listed below (and used in the same way you would when browsing a Wikipedia article)

Note that this page is not intended to teach you Python from scratch. It's meant to be a simplified accompanyment to classroom teaching. You will not be able to learn how to code in Python by using this page, but you may remember how to structure a for loop. There are plenty of other web resources designed to teach you Python from nothing.

SECTIONS


PYTHON DATA TYPES

If you’ve ever used variables with both text and numbers before you will notice that they’re written differently. The text needed speech marks around it, while the numbers didn’t. Why is this?


The computer categorises any piece of data it deals with in to what are called ‘Data Types’. A data type just states what type of information it is, for example is it a word or is it a number?


The table below shows the different data types that Python deals with and what they look like:

DATE TYPE

EXAMPLE

Integer

7

Floating Point Number

4.5

String

“Hello, how are you?”

List

1, 2, 3, 4, 5

Although there are more you may encounter in the advanced stages of programming, these are the ones we will deal with.
The data type is very important to a computer. If it doesn’t know what type it is, it doesn’t know what to do with it. Luckily, Python automatically recognises most types so we don’t have to tell it. Some programming languages don’t, so if you wanted to use an integer (a whole number) you would have to tell it what it is about to receive.

Strings however, do need to be explained to Python. Strings are just words and sentences. We do this by putting speech marks around them. If we don’t do this, it won’t understand that we have just entered words and may get confused later on. Likewise, if you were to enter a number in speech marks, like “8”, the computer would not know it was a number, and would store it as a string. We then wouldn’t be able to use it in any math equations.

OUTPUTTING DATA

To output data means to send it to an output device, using a particular method to display to the user. To output data in Python we use the PRINT statement. It looks like this:

print("Hello World")

If we run this in Python we get the following message

Hello World

You must always use the brackets around something you wish to output. The speech marks are used when you are outputtting a string. If you are outputting a number or a variable you do not need the speech marks.

PYTHON VARIABLES

Variables are one of the most fundamental tools in programming. Without them, programming anything would be a long, arduous process. Here’s how they work.
A variable is just like a box. Let’s say we have a box called my_number.  We can put anything in our box, but as the name suggests, it’s going to be used for numbers. If we put the number 8 in our box, then the computer would know, every time we mentioned my_number, we were talking about the number 8. In Python, we can put something in our variable by writing the following:

myVariable = 8

Now we have a variable called myVariable which has the value of 8. We can now use that variable name in our program and it will mean that it refers to the number 8.What happens then if we type in the following command after creating our variable?:

print(myVariable)

As you can see, you receive the following output

8

Variables can hold any type of data, such as strings, integers and floating point numbers.

USING MATHEMATIC OPERATORS

You can perform basic and complex maths in any programming language. The ability to do this is a cornerstone of any programming language. Without mathematics your program can't do much. To use mathematic operators, we type them just like we woudl write them in maths for the most part. The most basic mathematic operators are as follows:

OPERATOR

FUNCTION

+

Add

-

Subtract

*

Multiply

/

Divide

% Modulo (get the remainder of a sum)

We type them like this

print(8*4)

This gives us

32

But look what happens if we type with the speech marks like we did in the earlier example.

print("8*4")

Becomes

8*4

This is because using the speech marks makes the data become a string and just outputs exactly what you typed.

INPUTTING DATA

To input data from the user we use the following syntax:

myInput = input()

Now whatever the user types in response will be saved in the variable myInput. Look at the following code which combines an input and an output.

myInput = input()
print(myInput)

For example if I type in "Hello" and then click enter it will read back "Hello" to me.

This only works with strings however. With any other data type you need to tell Python what the data is going to be, otherwise it will just be saved as a string and you will never be able to use it in any other way (for example in calculations).

For example, to take an integer as an input we use

myInput = int(input())

And to take a floating point number we would use:

myInput = float(input())

 

LOOPS

A loop in programming is something which happens more than once. We use loops to make things easier. For example, if you wanted to get someone to input 30 of the same type of data, you would use a loop rather than 30 separate input statements.

There are two types of loop you will need to know about, the for loop and the while loop

FOR LOOP

The for loop runs for as many times as you tell it to. It looks like this

for i in range (1, 10):
jjbjbprint("Hello")

Let's just explain what each part does.

The for just tells the program what type of loop it is

The 'i' is a counter. It starts off at the first number and keeps going up until the loop runs for the required amount of times and then stops.

the 'in range' just means that it runs for the amount of times written

The numbers are the starting and ending points of the loop

You must always remember to put a : sign after the first line of a loop. This says that whatever comes after it is inside the loop and should happen for the required number of times. When you press ENTER after typing a : in Python you will notice that the next line is a little bit to the right. This positioning is called INDENTATION and signifies that the line is inside the loop.

If we run the program we will get the following output

Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

A very useful part of the for loop is the 'i'. Because it counts up, if we output it inside the loop it will change. For example, the following code:

for i in range (1, 5):
jjbjbprint(i)

will output like this

1
2
3
4
5

This means we can make use of it in mathematics like so. The following program will print out the 6 times table up to 60

for i in range (1, 10):
jjbjbprint(i*6)

As you can see, because i goes up by 1 every time, the output will be:

6
12
18
24
30
36
42
48
54
60

After we have finished with a loop, make sure you write everything else back in the original place on the l

WHILE LOOP

A while loop runs while something is true. It looks like this:

while 4 > 10:
sdsprint("Hello")

What do you think the above code would output? If you said nothing then you'd be right. Because 4 will never be greater than 10, the line inside the code will never run.

With two set numbers a while loop is pretty useless as one number will never change in value. However, it is useful when you start using variables. Consider the following use of the while loop. It is used to ask the user a question and give them as many tries as they need until they get it correct

userguess = ""
answer = 9
print("Guess what number i"m thinking of")
userguess = input()
while userguess != answer:
sdsprint("Incorrect. Please guess again")
asduserguess = input()

As you can see, a for loop would be useless here as we do not know how many times the user will get the question wrong.

CONDITIONAL STATEMENTS

A conditional statement does a differnet thing depending on something else. Think about this real-life example - if it is raining you will wear a raincoat, if it is snowing you will wear boots and if it is sunny you will wear a t-shirt. The outcome of your choice depends on the condition of the weather. In Pyhton we use the IF statement. It looks like this:

if weather == "snowing":
aaaprint("Put on some boots")

The first thing to notice is that we have used 2 equals signs. This is because double = is used in Python when you are comparing two things to see if they are the same. One equals is used to asign a variable.

This is the most basic form of an if statement. However, it only allows us to have 1 possibility which is pretty useless. To add another one we use elif (which is a shortening of 'else if')

if weather == "snowing":
aaaprint("Put on some boots")
elif weather == "sunny":
aaaprint("Put on a t-shirt")

But what happens if the weather is something we haven't considered. For this eventuality we add an else statement at the bottom. The else statement is what happens if the weather doesn't meet any of our stated criteria.

if weather == "snowing":
aaaprint("Put on some boots")
elif weather == "sunny":
aaaprint("Put on a t-shirt")
else:
dddprint("I really don't know what to tell you")

You can have as many elif statements as you want in a conditional statement.

ARRAYS

Sometimes you might want to store a lot fo the same type of data. Let's take for example a list of the test scores from a class. We could assign the data to variables like this:

studentOne = int(input())
studentTwo = int(input())
studenThree = int(input())
studentFour = int(input())
studentFive = int(input())
studentSix = int(input())
studentSeven = int(input())
studentEight = int(input())
studentNine = int(input())

However there are a few problems with this. Firstly, it takes a lot of room and a lot of typing. Secondly, what if we don't know how many students are going to take the test. We might want to use the program for a lot of different tests and we don't want to have to rewrite the code every time. To get around this we use an array. An array is a variable that holds a lot of values of the same type. It looks like this:

studentScores = [67, 64, 54, 65, 657, 67, 56, 56, 45, 45, 57, 67, 76,]

If we want to access the first student score in our program, we type the following

studentScores = [0]

If we want to access the 10th score in our program we type

studentScores = [9]

In Python, the firts position in an array is 0 but in other programming languages this might vary. So let's take a look at how we can make our earlier list of variable inputs easier by using an array

Firtstly we make a blank array with 9 positions

studentScores = [0]*10

This is a bit strange to anyone used to other programming languages, but in Python they work a little differently. We actually have to populate our array if we want to be able to assign values to it in the method we will use below. You can instead create a blank array and use 'append' but this is a little more difficult to remember how to do and so we'll use this method.

We then use the following command

for i in range (9):
...studentScores[i] = int(input())

All we have done is created a for loop which contains the input for our array. Because we have used [i] as the array position, this will increase by 1 every time the loop runs which means the array position we are inputting to will first be studentScores[1] then studentScores[2] and so on.

1) Give two methods of compressing an image

2) Explain the difference between lossy and lossless compression