Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holborow and welcome to Computing.

I'm so pleased that you decided to join me for the lesson today.

In this lesson, we're going to be looking at how we can use variables in programmes to store and update values.

We'll also look at the need for naming conventions.

Welcome to today's lesson from the unit: Programming Sequence.

This lesson is called Variables and Constants in Programmes.

And by the end of today's lesson, you'll be able to apply appropriate naming conventions and explain the differences between variables and constants.

Shall we make a start? We will be exploring these key words in today's lesson.

Variable.

Variable: a named piece of data stored in a computer's memory which can be accessed and changed by a computer programme.

Declaration.

Declaration: The process of stating the name of a variable or constant and defining its data type.

Initialization.

Initialization: Assigning a starting value to a variable to let the compiler know that the memory location is required.

Constant.

Constant: A value that cannot be changed during the execution of a programme.

Look out for these keywords in today's lesson.

Today's lesson is split into three sections.

We'll start by declaring, initialising and assigning variables.

We'll then explain the need for naming conventions.

And then we'll move on to explain why constants are used in programmes.

Let's start by declaring, initialising and assigning variables.

A variable is a named piece of data stored in a computer's memory which can be accessed and changed by a computer programme.

A variable can hold one value at a time.

This value can change throughout the execution of a programme.

You might use a variable to keep track of the score for a game.

At the beginning of the game, the score might be zero.

During gameplay, the player might earn a point.

So we can see here, we have the line score is equal to score plus one.

So this would change the value held by score by increasing the value by one.

So score now holds the value one.

The previous value of score, which was zero, has been overwritten.

True or false? The value of variable holds cannot change.

Pause the video whilst you have a think.

That's right, it's false.

Variables can change during the execution of a programme.

If the value of variable holds is updated, then the previous value is overwritten.

A memory location needs to be allocated for the variable value before it can be used by a programme.

Declaring a variable means stating what data type will be used for the value.

Initialising a variable means setting the initial value.

Python doesn't use variable declaration, only initialization.

Here are some examples of variable declaration in Visual Basic and C.

Integer or int just means whole numbers.

Both of these statements mean declare the variable age as an integer.

So you can see in Visual Basic, we have Dim age As Integer.

In C, we have int age.

Initialization is when the initial value is assigned to the variable.

Remember that variables can only hold one value at a time, and this value can change throughout the execution of a programme.

Here are some examples of variable initialization.

This means hold the value 22 under the variable name, age.

So in Visual Basic, we have age is equal to 22.

In C, we have age is equal to 22; this time with a semicolon.

And in Python we just have age is equal to 22.

So these are very similar across the three languages.

Time to check your understanding.

What is this an example of? Score is equal to zero.

Is it A, declaration, B, initialization, or C, sequence? Pause the video whilst you have a think.

That's right, it's initialization.

We are setting the initial value of score to zero.

Okay, we're moving on to our first set of tasks for today's lesson.

Start by typing this Python code into an ID of your choice and then run the programme.

Then swap lines one and two around and run the programme again.

Read the error message that appears carefully.

For part three, explain why the error occurred.

For part four, look at the following code.

Can you predict what will happen when this code is run? And then for part five, copy the code from part four and run the programme in your IDE.

Were your predictions correct? Pause the video whilst you have a go at these activities.

How did you get on? Well done.

For part one, you were asked to type the Python code into an ID of your choice and then run the programme.

Hopefully your programme would output the name Sam.

That's because Sam is held in the variable name.

For part two, you were asked to swap lines one and two around and run the programme again.

You were then asked to read the error message that appears carefully.

So you should have an error message similar to the one below.

NameError: name 'name' is not defined line 1 of main.

py.

You were then asked to explain why the error occurred.

The error occurred in this programme because the programme tried to use a variable before it had been initialised.

For part four, you were asked to look at the following code and make a prediction about what would happen when the code was run.

I predict that the name Andeep will be displayed.

This is because the value held by name is overwritten on line two.

Were your predictions the same or did you have something different? Finally, for part five, you were asked to copy the code from part four, run the programme in your IDE and state whether your predictions were correct.

In this sample answer, yes, my predictions were correct.

Andeep was printed rather than Sam.

This is because Andeep was the last value that was assigned to the variable name.

Remember, your answer may differ slightly, but that's absolutely fine.

Okay, we are now moving on to the second part of today's lesson where we're going to explain need for naming conventions.

Variables should always be identified in a meaningful way, like the files from the starter activity.

A, B, C, X or Y are not helpful names for a variable.

If a variable is going to hold a name, then call that variable name.

So here we have two examples.

X is equal to Gerry, it's not very helpful.

Whereas name is equal to Gerry is much more helpful because it tells us what our variable is holding.

In programming, there are naming conventions that should be followed to make it easier to read and understand your code.

Consistent naming also makes it easier to modify and debug code.

All programming languages have their own naming conventions.

A programmer will read the documentation for the programming language to find out what the conventions are.

Python has an agreed set of conventions that programmers should follow and you can find these easily online if you want to.

In Python, variable names should be written in lowercase with an underscore separating words if required.

So here we have two examples: age, and then we have first_name.

Time to check your understanding.

True or false? X, Y, and Z are suitable variable names.

Pause the video whilst you have a think.

Did you select false? Well done.

Variables should always be identified in a meaningful way.

X, Y, and Z are not helpful variable names.

Which of these variable names follows Python naming conventions? Is it A, B, or C? Pause the video whilst you have a think.

That's right, both B and C follow Python naming conventions.

A doesn't follow Python naming conventions because it's in uppercase.

Okay, we're moving on to our activities for task B now, and you are doing a fantastic job, so well done.

I'd like you to explain why naming conventions are needed in programming.

Pause the video whilst you answer the question.

How did you get on? Let's have a look at a sample answer together.

You were asked to explain why naming conventions are needed in programming.

Clear naming helps developers read and understand code quickly.

For example, user_age is clearer than X.

Consistent naming also makes it easier to modify and debug code.

Did you have something similar? Well done.

Okay, we're moving on to the final part of today's lesson now where we're going to explain why constants are used in programmes.

Like a variable, a constant holds a value in a memory location that is needed for the execution of a programme.

However, unlike a variable, constants are used when a value needs to be available to the programme but will not change during the execution of the programme.

In some programming languages such as Visual Basic and C#, a constant can be declared as a constant and as such will be immutable, which means that it's not possible for the value to be changed during programme execution.

Here are some examples of constant declaration.

So in Visual Basic we have Const, or C-O-N-S-T, Speed As Integer is equal to 10.

And then in C#, we have const, this time in lowercase, int Speed is equal to 10 with a semicolon.

Some programming languages such as Python do not have constants.

However, so that you and other people reading your code can see that a value should be treated as a constant, it's a common naming convention for the identifier to be written in capital letters.

So for example, here we have SPEED all in capital letters, or case, is equal to 10.

However, capitalising the identifier does not make the value immutable, and you need to be careful not to write code that will cause the value to change during execution.

It's common to define constants at the top of the programme so that people reading your code are aware of them.

Meaningful identifiers will also help make their purpose clear.

So here we've changed the speed variable to CONST or C-O-N-S-T, _SPEED.

So people are aware straight away that the variable speed is a constant value.

Sam's got a really good question.

"If Python doesn't have constants, what is the point of defining a variable as a constant?" Maybe pause the video whilst you have a think.

Declaring a constant means that you can use a clearly named identifier throughout a programme such as the name 'TAX' to represent a tax rate instead of the value 0.

2 for 20%.

If the government changes the tax rate, a single changes required to the programme code and you can be confident that no other changes will need to be made.

This reduces the time taken to keep the code up to date and reduces errors, which can be hard to find.

Time to check your understanding.

Which of these constant names meet Python naming conventions? Is it A, B, or C? Pause the video whilst you have a think.

That's right, both A and B follow Python constant naming conventions because they use uppercase letters.

Match the term to the correct definition.

So we have three terms, variable, constant, and naming convention.

Pause the video whilst you read the definitions carefully.

How did you get on? Did you manage to match the terms? A variable is a named piece of data stored in a computer's memory which can be accessed and changed by a computer programme.

A constant is a value that cannot be changed during the execution of a programme.

And a naming convention is a standard for naming things in a chosen programming language.

Well done.

Okay, we're moving on to our final set of tasks for today's lesson, and you've done a great job, so well done.

I'd like you to start by explaining why constants are used in programmes.

I'd then like you to have a look at this code and make a prediction about what will happen when the code is run.

Pause the video whilst you have a think about the tasks.

For part three, I'd like you to open the starter programme, oak.

link/silly-sentences.

Run the programme.

Were your predictions correct? For part four, in which line is the variable adjective initialised? For part five, in which line is the variable adverb first reassigned? And then for part six, change all the values in both occurrences of noun, adjective and adverb to something different to make your own silly story.

Pause the video here whilst you have a go at the activities.

How did you get on? For part one, you were asked to explain why constants are used in programmes.

Constants are used in programmes when a value needs to be available to the programme but will not change during the execution of the programme.

You are then asked to look at the code and make a prediction about what will happen when the code is run.

Here's a sample answer: I think the first line of the programme that will be printed will say, "The car was loud when it gently went to school." The second line of the programme that will be printed will display, "The zebra was giant when it aggressively went to school." This is because the values of the variables are updated in the programme.

Did you have something similar? You were then asked to run the programme and state whether your predictions were correct.

Here's a sample answer: I thought the programme may just display the words 'noun,' 'adjective' and 'adverb,' but it displayed the values held in these variables in the sentence.

For part four, in which line is the variable adjective initialised? This happens on line three of the programme.

For part five, in which line is the variable adverb first reassigned? This happens on line six.

And then for part six, you were asked to change the values of both the currencies of noun, adjective and adverb to make your own silly sentences.

Did you make some silly sentences? Great job.

Okay, we've come to the end of today's lesson and you've done a great job today, so well done.

Let's summarise what we have learned in this lesson.

Naming conventions improve code readability and maintainability.

A variable is a named piece of data stored in a computer's memory which can be accessed and changed by a computer programme.

Variables must be declared and assigned values in a programme.

Constants hold fixed values that do not change throughout the running of a programme.

I hope you've enjoyed today's lesson and I hope you'll join me again soon.

Bye.