Data types
Each variableA memory location within a computer program where values are stored. in a programSequences of instructions for a computer. must have 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 (also called Float) | Decimal numbers | 27.5 |
Char (also called 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 (also called Float) |
---|---|
Purpose | Decimal numbers |
Example | 27.5 |
Data type | Char (also called 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:
- integerA whole number - this is one data type used to define numbers in a computer program. Integers can be unsigned (represent positive numbers) or signed (represent negative or positive numbers). and floating pointA data value in computer programming used to denote decimal numbers. cannot be concatenationThe operation of connecting strings of characters next to each other., ie joined together
- numbers held in stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. and charactersData type that holds a single character. cannot be subject to mathematical operations
Type coercion
Sometimes programmers need 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 type coercion. The following PythonA high-level programming language. examples convert a string to an integer and an integer to a string:
str(68)
returns 鈥68鈥
颈苍迟(鈥54鈥)
returns 54
Type coercion is sometimes referred to as castingChanging the data type of a variable..