Grade 11 Information Technology unit 6
Python data types: when Python converts, and when you must
4 min · 2 past exam questions worked through
Parts of the lesson
Tap a part to play the lesson from there.
A 4-minute Temari video lesson on Grade 11 Information Technology unit 6: Python data types: when Python converts, and when you must.
More on unit 6
The chapter's own page holds the rest of what Temari has on it, such as short notes and practice questions with their answers.
Open the unit 6 pageEverything the lesson says
The lesson's words, part by part. Tap a time to play the lesson from there.
0:00Introduction
Every value in Python has a type: a whole number, a decimal, some text. Mix two types, and sometimes Python converts one for you. Sometimes it refuses, and you must convert it yourself. Learn which is which.
0:16Four major data types
A data type says what kind of value a variable holds, and which operations work on it. Python has four major built-in types. int is for integers, whole numbers like thirty four. float is for floating-point numbers, numbers with a decimal point, like four point five. str is a sequence of characters in quotation marks, like Hello world. And bool holds only True or False.
You never write the type down. Python sets it when you assign a value. Assign a whole number, and the variable is an int. To check a variable's type, use the type function.
0:56Implicit: Python converts
When you mix types, Python may convert one. This is type conversion, and it comes in two kinds. The first is implicit conversion, done by Python itself, without you. Say x equals five, an int, and y equals two point five, a float. Now z equals x plus y.
z holds seven point five, and its type is float. Python always converts the smaller type, int, to the larger type, float. Why? To avoid losing data. If z were an int, the point five would be lost, and the value would not be correct.
1:37Practice question 1Past exam
Here is a practice question. Look at this code. x equals five. y equals two point five. z equals x plus y. What is the data type of z? Pause the video, and try it.
x is an int, and y is a float. Python converts the int to a float, so nothing is lost. z is seven point five, a float. The answer is B. A, int, would lose the point five. C, str, is text, and there is no text here. And D, bool, holds only True or False.
2:22Explicit: you convert
The second kind is explicit conversion. Here you, the programmer, convert the value yourself, with the functions int, float and str. You need them because Python will not convert everything. Python does not allow adding an integer to a string. Try it, and you get an error.
Here is where that catches students. The input function reads what the user types. But it always returns a str, even when the user types a number. So convert it first. x equals input. Then x equals int of x. Now x is a number, and x plus five works.
3:03Practice question 2Past exam
Try this one. A program asks the user for a number and then adds five to it. The user types a whole number. Which code correctly adds five to the number? Pause, and try it.
The input function returns a str, so it must become an int before you add five. Only C wraps input inside int. The answer is C. A and B add five straight to the string, and that gives an error. D converts to str, and that is still text.
3:41Summary
So remember. Python has four major types: int, float, str and bool. When you mix an int and a float, Python converts the int to a float for you, so no data is lost. But input always returns a str, so convert it yourself before you do maths. Keep practising questions like these on Temari.et.
Practise this unit in the app
Temari turns this chapter into timed practice and marks it as you go, with an AI tutor to explain anything that did not land.
Start freeTemari.et is also an Android app, free on Google Play.

