All data is stored in memory in binary form, but this raises a problem:
The binary form of the integer 36 is the same as the binary form of the character '$' (see 3.05 - ASCII and Unicode).
How does the computer decide which of these interpretations is correct?
The data type of a variable determines how its binary form in memory is interpreted.
Table 1 shows the five main data types.
Table 1
Name | Pseudo-code | Python | Description | Examples |
---|---|---|---|---|
Integer | INT |
int |
A whole number. | 5 , -123 , 0 |
Float (or real) | REAL |
float |
A number with a decimal part. | 1.23 , -10.0 , 0.15 |
Boolean | BOOL |
bool |
Either "true" or "false". | True , False |
Character | CHAR |
N/A | A single character. | 'a' , '5' , '?' |
String | STRING |
str |
A collection of characters. | "abcd" , "!@#" , "" |
You can convert between different data types, as shown in Example 1 (pseudo-code) and Example 2 (Python).
Example 1
INT_TO_STRING(123) "123"
STRING_TO_INT("123") 123
REAL_TO_STRING(1.23) "1.23"
STRING_TO_REAL("1.23") 1.23
Example 2
str(123) # "123"
int("123") # 123
str(1.23) # "1.23"
float("1.23") # 1.23
Which data type do we use as the condition of an
IF
statement?
A boolean: it needs to evaluate to either True or False.