GCSE Computer Science (AQA)

Topic Summaries

ALGORITHMS AND PROGRAMMING: Types of data

Casting (type conversion)

  • Casting is the temporary conversion of one data type to another. It is required when a value’s current type is not suitable for an operation.
  • It allows for operations between different data types, and ensures we get user input in the correct type.
  • User input is usually stored as text (string), even if it looks like a number, but mathematical operations cannot be performed on strings, so casting ensures data is in the correct format for processing. 
  • For example, in Python:

# Convert string input to integer

age = int(input(“Enter your age:”))

 
 

# Convert integer to string

age_str = str(age)

 
 

# Convert string input to integer

price = 4.99

whole_price = int(price) # 4

  • Explicit casting is when the programmer manually converts a data type using a function like int(), float(), str(), or bool().
  • Implicit casting happens automatically without programmer instruction (e.g. combining an integer and a float results in a float).
  • Numerical calculations require input to be cast to int or float. Failure to cast correctly can cause runtime errors.
  • Numbers must be cast to strings when combining with text. This prevents type mismatch errors during output.
  • Value errors occur when conversion is not possible (e.g. text to number).
  • For Boolean values, non-zero numbers usually convert to True, and zero or empty strings convert to False.

Unlock Casting (type conversion)

Sign up to unlock the rest of this topic — plus every other topic, video, flashcard set and eBook guide in your SnapRevise Digital subscription.

Cancel anytime — instant access the moment you sign up.