Data types and casting

Integer, real, Boolean, character and string, and converting between them.

F1.8Programming basicsGCSE15 min

Do this lesson in the simulator

The last lesson ended with a robot that would not drive "20" centimetres, and a program that thought "20" times 3 was 202020. Both come from the same idea: every value in Python has a data type, and the type decides what you can do with it.

Five types

Type Python name Examples From the robot
integer: a whole number int 4, -3, 100 battery()
real: a number with a decimal point float 4.0, 2.5, -0.1 distance(), heading()
Boolean: true or false bool True, False bumped()
string: text str "BugBot", "20", "" input()
character: one letter, digit or symbol Python uses a string of length 1 "A", "7", "?"

type tells you the type of any value. Ask the robot:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print(battery(), type(battery()))
print(distance(), type(distance()))
print(bumped(), type(bumped()))
print("20", type("20"))
print(20, type(20))

Run this in the simulator

The battery is a whole percentage, so an integer. The distance is measured to a tenth of a centimetre, so a real number. Whether the robot has just bumped into something is either true or false: a Boolean. And "20" in quotes is a string, whatever it looks like.

Why the type matters

The same operator does different things to different types:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print(20 + 5)
print("20" + "5")
print(20 * 3)
print("20" * 3)

Run this in the simulator

+ adds numbers but joins strings. * multiplies numbers but repeats strings. And some mixtures are refused altogether: try adding a line print("20" + 5) and read the runtime error.

Casting: changing the type

Casting means converting a value from one type to another. Each type's name works as a function that makes that type:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print(int("20") * 3)       # text to integer: 60
print(float("2.5") * 2)    # text to real: 5.0
print(str(20) + " cm")     # integer to text: 20 cm
print(int(7.9))            # real to integer: 7, it chops, it does not round
print(float(3))            # integer to real: 3.0

Run this in the simulator

Now fix last lesson's robot. Run this and type a distance:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

far = float(input("How far shall I drive, in cm? "))
print("OK, driving", far, "cm")
forward(50, distance=far)
print("I am at", position())

Run this in the simulator

input returns a string, and float turns it into a real number straight away, before it is stored. Use int when only whole numbers make sense (how many times, how many sides) and float when fractions do (how far, how long).

When casting fails

Run the cell above again and type twenty. ValueError: there is no number in that text to convert. Try 20.5 with int instead of float as well: int refuses text with a decimal point in it. A program that asks a person for a number has to expect this; module F6 shows how to check an answer before using it.

Task: drive what you type

Ask How far?, turn the answer into a number, and drive exactly that far forward. The task will answer 35, so the robot should stop 35 cm up the mat. Your program must work for any distance: do not type 35 into it.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

far = input("How far? ")
forward(50, distance=far)

Challenges

  1. Ask for a number of seconds and wait that long, with the LED on.
  2. Ask for a distance in metres and drive that many metres, converting to centimetres.
  3. Print type(position()). It is none of the five types above. Find out what it is.