CS12-03 Computer Science Watch
Casting
In this lesson
Casting: how to define casting as changing a value from one data type to another, use int(), float(). AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Programming basics 3 of 9. More at https://scholafly.com
By the end: Define casting as changing a value from one data type to another, use int(), float() (real() in OCR's reference language), str() and bool() to do it, explain where casting is useful - above all that keyboard input arrives as a string and must be cast before arithmetic - and point to where casting happens in a given program and say what it does there.
What it covers
- 0:00 Intro: casting
- 0:44 What casting
- 2:34 Keyboard input
- 4:32 Casting the other way
- 6:39 Casting in a given program
Key words
About this video
Casting | GCSE Computer Science | CS12-03
Casting: how to define casting as changing a value from one data type to another, use int(), float().
AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Programming basics 3 of 9.
More at https://scholafly.com
Watch first: CS12-02 - https://youtu.be/vAE8x16LgDE
Video code: CS12-03 (search "ScholaFly CS12-03")
#GCSEComputerScience #ProgrammingBasics #Casting
For teachers
This GCSE Computer Science lesson teaches casting. By the end, students should be able to define casting as changing a value from one data type to another, use int(), float() (real() in OCR's reference language), str() and bool() to do it, explain where casting is useful - above all that keyboard input arrives as a string and must be cast before arithmetic - and point to where casting happens in a given program and say what it does there. It works through four worked examples and the mistakes examiners report.
Exam board specification references:
OCR J277
- 2.2.2
Read the transcript
A program asks your age. You type fifteen, and it adds one to show your age next birthday. What does it show? Sixteen seems obvious, and it is wrong: some languages show one, five, one, a one glued onto the text, and Python stops with an error. What came from the keyboard was never a number, and one short word in the right place fixes it. That one short word turns up shortly, when the age program gets mended.
Casting is changing the data type of a value, from one type into another. Four casts cover most needs. int turns a value into an integer. float turns it into a real, and one course's reference language spells that one real. str turns a value into a string, and bool turns it into a Boolean. Think of each cast as a small machine. A value goes in one side as one type and comes out the other side as another, while the original stays where it was. One student defines casting as changing an integer into a string. Why is that not a definition of casting? It is one example. A definition has to cover every case, from any type to any other type. Here is how one examiner's report put it. Many candidates correctly defined casting as changing data from one data type to another. Some candidates defined this term as changing a variable from an integer to a string, which is only one example of what can be done and not a definition. So the fix is the order. Give the definition first, then an example under it, such as int turning the text fifteen into the number fifteen.
Whatever is typed at the keyboard arrives in a program as a string, even when every character is a digit. In Python, age equals input stores the typed text in age. Typing fifteen stores the string fifteen, in quotation marks, and not the number. Wrap the input in int, and the text is cast before it is stored: age equals int of input. Now age holds the integer fifteen, and age plus one is sixteen. A program reads two numbers and prints their sum. Typing three, then four, prints three four, side by side. Why does it print three four, and which lines need a cast? Both inputs arrive as strings, so plus joins them. Cast each input line with int, and plus adds them to give seven. One examiner's report, on a question that took user input into variables, says a lot of students made errors with datatypes, either forgetting to cast, and it goes on to list others. The habit that fixes it: any input that will be used in arithmetic gets int, or float for a decimal, wrapped round it on the line that reads it. One limit, though. Typing a word where a number is expected makes int fail, because only a string that holds a number can become one. Checking for that is the job of the video Input validation.
Casting runs the other way when a number has to join some text. With age holding sixteen, age plus the text years old fails in Python, because plus cannot join a number to a string. str of age, plus the text years old, gives sixteen years old. Read a line like that from the inside out. First the cast turns sixteen into the text sixteen. Then the plus joins two strings. A cast makes a new value. int of x gives back an integer, but x itself keeps its old type, unless that result is stored back in x. And int of a real cuts the decimal part off. It never rounds, so int of three point seven is three. Here are four to work out. int of nine point nine nine. str of twelve, plus the text three. int of the text twelve, plus three. And float of the text two point five, times two. Pause and work out all four values, one at a time. The first gives nine, because int cuts rather than rounds. The second gives the text one two three. str makes twelve a string, and plus joins. The third gives fifteen. int makes twelve a number, and plus adds. The fourth gives five point nought, a real, because float keeps a decimal part.
Some questions hand you a program and ask how casting is used in it. Here are four lines. Line one reads a number of tickets with int round the input. Line two works out cost as tickets times six. Line three builds a message from the text Total and str of cost. Line four prints the message. Two of those four lines change a value from one type to another. Pause and say where casting is used, on which value, and to which type. Line one turns the typed text into an integer, so it can be multiplied. Line three turns cost, an integer, into a string, so it can join the message. One examiner's report says this about a question of that kind. Casting was well understood by most candidates. Casting converts one data type to another data type. This question was an A O two question and so requires an answer in context. An A O two question asks you to apply what you know to the program given, so in context means about this code. So every answer names three things from the program in front of you: the line, the value being cast, and the type it becomes.
One question per cast direction, then a line to read and the age program from the start, all with values this video has not used. What does int of seven point eight give? Seven. The decimal part is cut off, not rounded. Try the other direction: str of five, plus the text five. What comes out? The text five five. Both sides are strings by then, so the plus joins them. Here's a line: height equals float of input. What type does height hold? A real. The typed text is cast to a float before it is stored. One more, the age program: which line reads the age so that adding one gives sixteen? Age equals int of input. That one short word, int, makes fifteen an integer before the one is added, so the sum is sixteen.
If you feel confident on casting now, leave a thumbs up, and it drops off the list of things to watch again. If it is still wobbly, save it, give it a day, and look again with fresh eyes.
Next in the chapter: Arithmetic operators, DIV and MOD. Hopefully that's next in your feed, but if not, search for ScholaFly C S twelve, oh four.
Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.
Related terms
For: OCR GCSE J277, AQA GCSE 8525, Edexcel GCSE 1CP2
On the specification
| Board | Spec | Statement |
|---|---|---|
| OCR GCSE J277 | 2.2.2 | Data types |
| AQA GCSE 8525 | missing | the plan names no spec point for this board |
| Edexcel GCSE 1CP2 | missing | the plan names no spec point for this board |
For teachers
This GCSE Computer Science lesson teaches casting. By the end, students should be able to define casting as changing a value from one data type to another, use int(), float() (real() in OCR's reference language), str() and bool() to do it, explain where casting is useful - above all that keyboard input arrives as a string and must be cast before arithmetic - and point to where casting happens in a given program and say what it does there. It works through four worked examples and the mistakes examiners report.