Loading...
Hello, my name is Mrs. Holbrook and welcome to Computing.
I'm so pleased you've decided to join me for the lesson today.
In this lesson we'll be exploring different data types and seeing how errors can be caused by incorrect data types.
Welcome to today's lesson from the unit, programming sequence.
This lesson is called common data types, and by the end of today's lesson, you'll be able to identify different data types and explain how they are processed.
Shall we make a start? We will be exploring these keywords in today's lesson, data type.
Data type, defines the kind of data a computer can store and the actions you can perform on it.
Casting.
Casting, changing the data type of a piece of data from one type to another.
Look out for these key words throughout today's lesson.
Today's lesson is split into two sections.
We'll start by exploring data types and then we'll move on to explain errors caused by incorrect data types.
Let's make a start by exploring data types.
When writing programmes, we work with different kinds of data and information.
For example, numbers, text and true and false values.
Python uses data types to store and process information correctly.
There are five main data types that you need to be aware of.
Let's have a look at this table to explore them a bit further.
A string, this holds data as text, but it can also hold numbers and characters and symbols.
So for example, we have password, but we have a zero representing the O, and we have an exclamation mark at the end.
Integer.
This stores whole numbers.
So for example, three.
Boolean, this holds true or false.
So an example would be true.
Real or floating point numbers.
These hold decimal numbers.
So 5.
3 as an example, and then char, which is short for character, is a single string character.
So an example would be the character, a.
Time to check your understanding.
Why does Python use data types for the data it processes? Is it A, to make the programme run faster? B, to store and process the data correctly, or C, to make the code easier to read for humans? Pause the video whilst you have a think of the answer.
Did you select B? Well done.
Python uses data types for the data it processes to store and process the data correctly.
When you use the input function in Python, it defaults to returning the data as a string, even if you enter a number.
So for example, in this piece of code, even if you type in 15 Python treats the 15 as a string, not a number.
To convert values to different data types, you need to know the functions that are available to you.
Here are some of the most common functions that you'll need to know.
To convert to a string you use the str function.
To convert to an integer, you use the int function and to convert to a real or decimal point number, use the float function.
Casting is the process of converting a value from one data type to another.
Functions used for casting, take a value, process it, and return the value in a new data type.
In this code input receives the user input as a string and then the int function converts or casts that string into an integer.
Let's have a look more carefully at the input process and output of the input function in Python.
So in the input stage, the user types in four and presses the enter key.
The four is passed to the input function.
In the input function, the code takes the input, converts it to a string, and returns the new value.
For the output, the new value is returned.
Four is typically held in a variable, but remember this will be held as a string.
Now let's look at the same, but for the int function.
The string value four is passed to the function.
The code written for the function takes the string value four, converts it to an integer, and returns the new integer value four.
The new value is returned.
Four is typically held in a variable and can be now used for calculations.
Time to check your understanding.
What is the term used for converting a value from one data type to another? Is it A interpreting.
B, compiling.
Or C, casting? Pause the video whilst you have a think.
The term used for converting a value from one data type to another is casting.
Okay, we're moving on to our first set of tasks for today's lesson.
I'd like you to describe the five main data types used in Python.
I'd like you to explain why it's important for Python to use the data type of the information it's processing, and give an example of how and why you might convert one data type to another.
Pause the video whilst you answer the question.
How did you get on? Let's have a look at a sample answer together.
Python uses five main data types, strings, which are text, integers, which are whole numbers, Boolean values, which are true or false, floating point, which are decimals, and chars, which are single characters.
Python needs to know the data type to process the data correctly.
For example, you can't do calculations with strings.
Casting data types is needed when using input as it automatically returns text.
To use numbers for calculations, we need to convert them to integers or floats using the int or float functions.
Remember, if you need to pause the video and add some additional detail to your answer, you can do that now.
Okay, we're moving on to the second part of today's lesson and you're doing a fantastic job so far, so well done.
We are now going to explain errors caused by incorrect data types.
Incorrect data types can cause problems during the execution of your programmes.
Let's have a look at this example.
So on line one, we are printing the message to the user enter a number.
On line two, we are storing that number under the variable num1.
On line three, we're asking the user for another number, and on line four, we're storing that value as number two.
On line five, we have a print statement which says print num1 plus num2.
Now if we have a look at the sample output for this programme, we have enter a number and the user has entered the number one.
We say enter another number, the user has entered number two.
Now we'd probably expect the result here to be three, because one plus two is three, but actually the programme is printing one, two.
This code was created to add up two numbers, but it outputs one, two instead of adding them up.
Why do you think this is? Maybe pause the video whilst you have a think.
The data type for an input defaults to a string, so when you add up two pieces of string together, it will procrastinate them or join them.
So instead of adding the two numbers together to make three, it has joined the corresponding strings together to make one, two, not 12.
If you want the programme to use the value as an integer, then you need to specify this by casting the value.
You do this by placing the input function inside the int function so you can see here we now have num1 is equal to int, open brackets input, open close bracket for the input function, and then close bracket for the int function.
Really important to keep track of your open and close bracket here, and we've done exactly the same online four.
So now when we run the programme and the user enters one for the first question and two for the second, the result of three is correctly calculated.
Time to check your understanding.
What happens when you add two string values together in Python? Is it A, they are concatenated or joined together? B, an error occurs, or C, they are converted to integers and added.
Pause the video whilst you have a think.
That's right.
They're concatenated or joined together.
Well done.
Just because we cast values, it doesn't mean that our programmes won't have errors.
Errors can still happen during execution, even when casting has been used.
For example, if the user responds with the word four rather than the number an error occurs.
This is called a type error and it causes the programme to crash when it is run.
So sometimes we cannot predict what the user is going to enter for our input statements and we can still have errors.
When a whole number is wanted and the float function is used.
The output will be a decimal number, a floating point number.
For example, if five is entered into this programme, the output will be 5.
0.
This might not be the desired result if a whole number was in intended.
What would be the output if the user entered three and then three in this code? Would it be A, six.
B, 6.
0.
Or C, 33.
Pause the video whilst you look carefully at the code.
That's right, the output will be 6.
0.
That's because both num1 and num2 have been casted to floating point numbers.
Okay, we are now moving on to the second part of today's lesson and you are doing a fantastic job so well done.
Jacob is writing a Python programme to calculate the area of a rectangle, here's their initial code.
For part one, identify and explain two potential errors that could occur when running this programme.
And then for part two, rewrite the programme to correct these errors, ensuring it accurately calculates the area of the rectangle.
Pause the video, whilst you have a go at the task.
Finally, for part three.
Even with your corrected code, what additional error could still occur? Pause the video whilst you answer the question.
How did you get on? I'm sure you manage that.
Well done.
For part one, you were asked to identify and explain two potential errors that could occur when running this programme.
Here's a sample answer.
If the user enters number for length and width, the programme will try to use the values as strings, not numbers leading to an error.
So you can see both num1 and num2 are stored using the input function, which we know defaults to a string.
If the user enters text as length or width, this will cause an error in the programme because text inputs are not integers.
You were then asked to rewrite the programme to correct these errors, ensuring it correctly calculates the area of a rectangle.
So hopefully what you did was cast both num1 and num2 as integer values.
So line two now reads num1 is equal to int(input()).
Three, even with the corrected code, what additional error could still occur? Here's a sample answer.
It will cause an error of the user enters input that is not a valid number.
For example, words, letters, or symbols.
When they're asked to provide the length or width of the rectangle.
The programme expects numerical input, and if it receives anything else, it will cause an error.
Remember, this type of error is called a type error.
Okay, we've come to the end of today's lesson and you've done a great job, so well done.
Let's summarise what we have learned.
In programming, different types of data are used.
These are known as data types.
Using the correct data type is crucial as using an incorrect data type can lead to errors in your programme.
Sometimes a data type may need to be changed.
This process is called casting, and it allows the conversion of data from one type to another, enabling its use in different ways within code.
I hope you've enjoyed today's lesson and I hope you'll join me again soon.
Bye.