2A.02 – Data Types


Previous: 2A.01 - Variables

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 '$'.

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", "!@#", ""

Data types can be changed, 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?

Tap/click to reveal A boolean: it needs to evaluate to either True or False.





Next: 2A.03 - Operations



© Rujul Nayak 2024-