In this video, we take a look at some of the following programming fundamentals. The use of variables, constants, inputs, outputs and assignments. So before we dive into this video, what actually is a variable?
Well if you go right back to what we covered at the start of the course, we looked at one of the registers in the CPU, the memory address register. And this contains the address of an instruction or data to be fetched or written to the memory. And in a very similar way, a variable is nothing more than a pointer to a memory address, which you're then able to give a user-friendly label or name.
So in order to go through these concepts we're actually going to use a simple program written in Python, a beat that dice game. So this is a simple game for young children learning to compare one and two digit numbers. The rules are simple, a player rolls two dice, doubles do not count and must be rolled again.
The player's score is the value of the highest and the lowest dice combined and the highest dice is always four. So if a five and a seven were rolled The result would be 75. So let's just do a quick example round. Craig rolls a three and a four. The total of his throw is 43 because four is the highest of the two numbers. Note the dice are not added together so it's not four plus three is seven, it's 43. Dave then rolls a three and a three.
The roll doesn't count as it's a double so he throws again. He rolls two and one, the highest dice goes first so he gets 21. Okay, so here's the outline code written in Python for this game. Let's go through it now and pull out some of those concepts we mentioned at the start of the video. So here in this line we see an example of a variable and we write total equals zero and that's us assigning a value to our variable total.
A variable is simply a value that can change or vary, hence its name, whilst the program is running. and it's simply an address in memory. Of course referencing a memory address is very difficult for programmers, so having a user-friendly label like total is much more helpful, and it makes our code easier to read and understand.
Now every variable requires a data type, but in Python, unlike most languages, we don't actually have to declare our data type first. Python works it out based on the value you initially give it. So here we've said total equals zero, so it assigned the data type integer to the variable total. Here we see an example of a constant, roles per player equals two, and it looks exactly the same as our variable in Python. Now, a constant is a value which remains fixed and does not change while the program is running.
So we must set it at design time when the program is first written. Now the reason it looks identical here to the previous line of code we looked at is that Python doesn't actually support constants. If we wanted to implement constants in a Python program we would have to set a value like we have here, roles per player equals two, and then we would have to make sure we never actually changed it.
In most programming languages you could actually declare this as a constant and it would be unable to be changed during program execution. Assignment is simply when we give or supply a value to a variable or constant. So in both the previous lines of code we've looked at, we are supplying a value to a variable or a constant.
An assignment is performed with the equal symbol. You assign the value on the right of the equal symbol to the variable or constant on the left. So here we see an example of an operator.
Total equals total plus role value. And operators can be used on variables and constants. So with this plus symbol, we are adding the contents of total to the contents of role value. And then the result of that calculation is being stored back over the original contents of total. Now the topic of arithmetic operators is covered in more detail in a later video, so we won't look at other examples now.
You'll notice how we're actually using the plus operator here in two very different ways in our program. So In the first example, higher up the program, where we're saying roll value equals dice one string plus dice two string, we're actually joining together or concatenating two string variables. In the second example, the plus is performing an arithmetic operation of addition and is adding the value of total to the value of roll value. When... the same symbol is used to do two completely different things it's actually known as overloading but this is not something you need to worry about at GCSE here we're carrying out two actions at once the first is getting input from the user via the command input and this is how we typically get input from say the keyboard and we're assigning it to the variable user input But at the same time, we're also casting or converting the value entered by the user from a string into a number.
And that's performed by the command int. That's because even if the user presses the key 6 on the keyboard, that's the ASCII character 6. If we want it stored internally as a number, we have to cast it or convert it into an integer. And we do that with the int command.
So there's two things happening at once in that line of code. We can see casting occurring in a number of additional places as well. So we can see in these top two lines that we take the value that's held in the variables dice1 and dice2, which are integers, and then we cast or convert those into their string equivalents and then assign those to the variables dice1 string and dice2 string. You can see lower down, we take the contents of the variable role value, and we cast and convert it to an integer before storing it back into role value. We can actually cast from any data type to any other data type.
The next thing to cover here is outputting, another quite common thing to do in programs. We gather input, we process that input in some way. But typically, we also need to report the output to the user so they know what's going on.
An output statement is used to print the screen in Python. Here we've combined the contents of a string, dice1 colon or dice2 colon, with the contents of a variable, dice1 or dice2. You'll notice that strings were enclosed in single or double quotes, and this depends on the language. So let's just recap some of those key terms.
A variable is a value stored in memory that can change while the program is running, whereas a constant is a value that does not change while the program is running and is assigned when the program is designed or written. Assignment simply means giving or supplying a variable or constant a value. And casting is when we convert a variable from one data type to another, for example, from an integer to a string.
Input is a value that is being read from an input device, typically a keyboard, but it doesn't have to be. And output is data generated by the computer and displayed to the user in some form. You need to understand some of the advantages of using constants over variables, even if they aren't supported in the language that you've been learning, for example, Python.
So constants make a program easier to read. as they're usually declared and assigned at the top of the program. Constants can easily be changed by the programmer in one single place, that's where they're declared, instead of changing every instance of a particular value throughout a program, and this leads to less chance of errors. When you use constants, the compiler can optimize the code, and this makes the program run more quickly if constants are used instead of variables.
And finally, why is casting needed? Well, casting changes a variable, as we've said, from one data type to another. Input from the keyboard always comes in as characters, and multiple characters are called strings. However, to perform addition, the arithmetic logic unit must use numbers.
Therefore, a string has to be cast to a number. This makes sense when you think about how the character 1 typed on a keyboard is stored in binary. In ASCII, it would be 0011001, but the number one...
ALU needs for a calculation is stored as the binary number 0000001 if we were using 8 bits. Integers require less bits of memory than numbers with a decimal part. Therefore, it makes sense to use integers where we can to make a program more memory efficient. However, it may well be necessary to cast an integer to a real or float data type in a program.
Some commands also require data in a particular data type, another reason why we might need to use casting. So we've talked about data types a few times in this video. So here are four of the most important you need to know. Integers for storing whole numbers, positive or negative. And we use these when arithmetic on data is required.
Real numbers, that's numbers with a decimal part, sometimes called a float. characters is a single alphanumeric character typically used for menu choices and remember one on the keyboard when pressed would be the character one not the number one and a string is a multiple alphanumeric characters so we use this for things like words and telephone numbers most languages support many other data types such as dates and pictures but they're beyond the scope of GCSE