Each variableA memory location within a computer program where values are stored. has a data typeIn computer programming, data is divided up and organised according to type, eg numbers, characters and Boolean.. The data type determines what type of value the variable will hold.
Data type
Purpose
Example
Integer
Whole numbers
27
Real
Decimal numbers
27.5
Character
A single alphanumeric character
A
String
One or more alphanumeric characters
ABC
Boolean
TRUE/FALSE
TRUE
Data type
Integer
Purpose
Whole numbers
Example
27
Data type
Real
Purpose
Decimal numbers
Example
27.5
Data type
Character
Purpose
A single alphanumeric character
Example
A
Data type
String
Purpose
One or more alphanumeric characters
Example
ABC
Data type
Boolean
Purpose
TRUE/FALSE
Example
TRUE
Different data types have limitations:
integers and real numbers cannot be concatenated, ie joined together.
numbers held in strings cannot be subject to mathematical operations.
Casting
Sometimes a programmer needs to change the data type of the contents of a variable. For example, an integer may need to be converted to a string in order to be displayed as part of a message. This process is known as casting. The following examples in Python convert a string to an integer and an integer to a string: