Programming fundamentals - OCRData types

Programs are designed using common building blocks, known as programming constructs. These programming constructs form the basis for all programs.

Part ofComputer ScienceComputational thinking, algorithms and programming

Data types

Each must have a data type. The data type determines what type of value the variable will hold.

Data typePurposeExample
IntegerWhole numbers27
FloatDecimal numbers27.5
CharacterA single alphanumeric characterA
StringOne or more alphanumeric charactersABC
BooleanTRUE/FALSETRUE
Data typeInteger
PurposeWhole numbers
Example27
Data typeFloat
PurposeDecimal numbers
Example27.5
Data typeCharacter
PurposeA single alphanumeric character
ExampleA
Data typeString
PurposeOne or more alphanumeric characters
ExampleABC
Data typeBoolean
PurposeTRUE/FALSE
ExampleTRUE

Different data types have limitations:

  • integers and floats 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 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 convert a string to an integer and an integer to a string:

str(68) returns “68”
int(“54”) returns 54