GCSE Computer Science
Python type casting โ GCSE Computer Science
The most common casting pattern โ converting input() to a number โ is covered in the Python variables guide. This page covers casting as its own topic: all four conversion functions, casting between numeric types, and what happens when casting fails.
The four casting functions
int()โ converts to a whole number.float()โ converts to a decimal number.str()โ converts to text.bool()โ converts toTrueorFalse.
Casting between numeric types
pi = 3.9
print(int(pi)) # 3
print(float(7)) # 7.0
int() doesn't round โ it truncates, cutting off everything after the decimal point and keeping only the whole-number part. int(3.9) is 3, not 4, and int(-3.9) is -3, not -4. Rounding and truncating are different operations, and exams test the difference deliberately.
Why casting fails
age = int("hello")
This raises a ValueError โ not because the syntax is wrong, but because "hello" isn't a valid whole number, and Python can't turn it into one. Casting only works when the value you're converting actually represents the target type. int("42") works; int("forty-two") doesn't.
str() for combining numbers with text
score = 85
message = "You scored " + str(score)
print(message)
Python won't let you concatenate a string and a number directly with + โ str() converts the number to text first so the concatenation works.
bool() โ truthy and falsy values
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hi")) # True
0, an empty string "", and None all convert to False. Almost everything else converts to True. This matters when a value is used directly as a condition โ if my_list: is really asking bool(my_list), which is False only when the list is empty.
Assuming int(3.9) rounds to 4. It truncates to 3. Python's int() always cuts toward zero โ it never rounds.
What examiners actually test
Casting questions usually ask you to state the output of code involving int(), float(), or str(), identify which cast is needed to fix a type error, or explain why a given cast would fail. The truncation-not-rounding rule for int() is the single most commonly tested casting fact โ know it as a fixed rule, not a guess.
Python Coach covers hundreds of challenges taking students from their first print statement to advanced Python โ with progress tracking for every student.
Start your school's free trial →