8.1 - Data Types
Main data types in programming
Programming languages use data types to store and manage different kinds of information. A data type defines the kind of value a variable can hold, how it is stored in memory, and what operations can be performed on it. There are five primary data types commonly used across languages: integer, real (also known as float), boolean, character, and string.
Characteristics and examples of data types
The table below outlines each data type, its typical code representation, key characteristics, and examples.
| Data type | Code | Characteristics | Examples |
|---|---|---|---|
| Integer | int | Whole numbers without decimal parts, including positive, negative, and zero. | 0, 15, 2048, -123 |
| Real (or float) | real (or float) | Numbers that can include decimal parts, used for precise measurements. | 0.25, -7.12, 50.0 |
| Boolean | bool | Limited to two possible values, representing true or false states. | True/False, 1/0, yes/no |
| Character | char | A single letter, digit, or symbol. | 'A', 'k', '5', '-', '$' |
| String | string | A sequence of characters used to represent text. | "GhJkL7", "#value#" |
Memory allocation for data types
Each data type requires a specific amount of memory, which is the space allocated in the computer's storage to hold the value. Using the appropriate data type helps make programs more memory-efficient, meaning they use less storage and run faster. It also makes code more robust (less likely to break) and predictable, as the program knows exactly how to handle the data.
Typical memory usage
| Data type | Typical memory allocated |
|---|---|
| Integer | 2 bytes or 4 bytes |
| Real | 4 bytes or 8 bytes |
| Boolean | 1 bit is sufficient, but usually 1 byte is used |
| Character | 1 byte |
| String | 1 byte per character in the string |
Strongly typed versus weakly typed languages
Programming languages differ in how strictly they enforce data types, which affects how code behaves during operations like addition or comparison.
Types of language enforcement
- Strongly typed languages - These enforce strict rules on data types and do not automatically convert between them. This leads to more errors if types mismatch, but results in more predictable code. For example, trying to add a
stringto anintegerwould cause an error. - Weakly typed languages - These attempt to automatically convert data types to avoid errors, which can make coding easier but lead to unpredictable results. For example, adding the
string"2" to theinteger3 might result in 5, but it could also concatenate to "23".
Casting between data types
Casting is the process of manually converting a value from one data type to another. This is useful when you need to perform operations that require matching types, such as combining user input (often a string) with numerical calculations.
In Python, built-in functions handle casting:
int()- Converts to an integer.float()- Converts to a real (float).bool()- Converts to a boolean.str()- Converts to a string.
It's important to note that values like the integer 1, the real 1.0, and the strings "1" or "1.0" are distinct and treated differently in code.
Examples of casting
Here are some examples in Python:
ASCII functions for characters
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns a unique number to each character, such as letters and symbols. This allows computers to store and process text numerically.
ASCII conversion functions
Two functions work with ASCII:
ord()(similar to ASC()) - Converts a character to its ASCII number.chr()- Converts an ASCII number to its corresponding character.
Examples of ASCII functions
In Python:
Selecting appropriate data types
Choosing the correct data type for variables is essential to ensure the program works as intended, handles data efficiently, and avoids errors. Consider the nature of the data: whole numbers use integers, text uses strings, and yes/no questions use booleans.
Example: Data types for a registration form
Consider this registration form with sample data:
| Field | Sample data |
|---|---|
| Initial of first name | N |
| Surname | Chapman |
| Age (in whole years) | 35 |
| Height (in metres) | 1.78 |
| Married? | No |
Appropriate data types:
- Initial of first name - Character (
char), as it is a single letter. - Surname - String (
string), as it is a sequence of characters. - Age (in whole years) - Integer (
int), as it is a whole number. - Height (in metres) - Real (
realorfloat), as it includes a decimal part. - Married? - Boolean (
bool), as it is a yes/no (True/False) value.