Data Types¶
Data Types¶
We will use values of different types in our micro:bit programs, for example: we could capture acceleration values from the accelerometer. Alternatively, we might want to count the number of button presses the user has made or to show a message to the user telling them the temperature of the room. In order to do these things we need to be able to describe the data we want to use. Python, and most other programming languages, recognise several data types including:
- Integers - these are whole numbers.
- Floats - these are numbers that contain decimal points or for fractions.
- Strings - these can contain a combination of any characters that we want to treat as text such as letters, numbers and symbols.
- Boolean - used for
TrueandFalsevalues.
In a simple program we might use all of these. Here are the data types we could use for a program storing information about our favourite micro:bit games:
Image from: <http://www.bbc.co.uk/education/guides/zwmbgk7/revision/3>
Numbers: integers¶
Integers are whole numbers:
>>> a = 23
In Python, you don’t have to declare a variable’s data type when you create it. Python will decide for you. In this example, simply declaring the variable a as a whole number means that a has the integer data type.
Numbers: floats¶
Floats are floating point numbers, numbers that have decimal places:
>>> a = 17.42
In Python, you don’t have to declare a variable’s data type when you create it. Python will decide for you. In this example, simply declaring the variable a as a number with decimal places means that it has the float data type.
Text data: strings¶
Text or symbol data is stored in data types called strings. The content of a string must have quotation marks around it:
a = 'micro:bit'
display.scroll(a)
In Python, you don’t have to declare a variable’s data type when you create it. Python will decide for you. In this example, simply declaring the variable a as characters inside quotation marks means that it has the string data type.
Booleans¶
Booleans can only have two values, True or False:
love = True
display.scroll(love)
In Python, you don’t have to declare a variable’s data type when you create it. Python will decide for you. In this example, simply declaring the variable love as True means that it has the Boolean data type. Note that True and False must start with capital letters.
Converting data types¶
Sometimes you need to convert data from one type to another. This is also called casting.
number to string¶
Convert a number (integer or float) to a string using str():
score = 17
display.scroll('Score: ' + str(score))
You can use int(), float() as well to covert other data types to integer and float, respectively.
Testing data types¶
You can test what data type a variable is with type().
a = 23
if type(a) is int:
display.scroll('a is an integer')
b = 17.42
if type(b) is float:
display.scroll('b is a floating point number')
You can also display a variable’s data type. This will scroll <class ‘int’> on the LED display:
a = 23
display.scroll(str(type(a)))
Exercises¶
- Assign the following numbers to a variable (10, 11, 15, 5). Then convert it to float. Then combine the numbers into a single strng delimited by a space, and show it on the LED screen through
display.scroll().