Introduction to Data Types
Welcome! Today we are looking at Data Types. In Computer Science, data is just information. But here is the catch: computers aren't as smart as humans! If you see the number 10, you know it could be an age, a price, or part of a sentence. A computer needs us to tell it exactly what "type" of data it is looking at so it knows how to handle it.
Think of data types like storage containers in a kitchen. You wouldn't put milk in a toaster, right? Just like that, you wouldn't try to do math with a word. By the end of these notes, you’ll know exactly which container to use for your data!
1. The Five Essential Data Types
For your OCR exam, you need to know these five basic types. Let’s break them down:
Integer (Whole Numbers)
An Integer is any whole number. It can be positive, negative, or zero, but it cannot have a decimal point.
Examples: 7, -15, 0, 1066
Real-world use: Number of students in a class, a person's age, the score in a game.
Real (Decimal Numbers)
In some programming languages, these are called "Floats," but for your exam, we use the term Real. These are numbers that have a decimal point.
Examples: 3.14, -0.5, 10.0 (Even if it’s .0, it's still a Real!)
Real-world use: The price of an item (£1.99), your height in meters (1.75m).
Boolean (True or False)
A Boolean can only ever be one of two values: True or False. It’s like a light switch—it’s either on or off.
Examples: True, False
Real-world use: Is the user logged in? (True/False), Is the game over? (True/False).
Character (Single Symbols)
A Character is a single letter, number, or symbol.
Examples: 'A', 'z', '!', '5'
Real-world use: Choosing 'Y' or 'N' on a menu, your middle initial.
String (Text)
A String is a collection of characters. Think of it like a "string" of beads, where each bead is a character. Strings are usually written inside quotation marks.
Examples: "Hello World", "Computer Science", "User123"
Real-world use: A person's name, an address, a password.
Memory Aid! Use the mnemonic: "I Read Books, Comics, and Stories"
(Integer, Real, Boolean, Character, String)
Quick Review:
• Integer: Whole number.
• Real: Decimal number.
• Boolean: True/False.
• Character: One symbol.
• String: Multiple symbols (text).
2. Choosing the Right Data Type
In the exam, you might be asked to pick the best data type for a scenario. Don't worry if this seems tricky; just ask yourself: "Does it have decimals? Is it just text? Is it just Yes/No?"
Common Mistake: Phone Numbers
Wait! You might think a phone number like 07123456789 is an Integer because it's made of numbers. However, it should actually be a String! Why?
1. Integers automatically remove the leading zero (making the number wrong!).
2. You never need to do math with a phone number (you don't add two phone numbers together).
Rule of thumb: If you don't need to do math with it, and it starts with a zero, use a String!
Did you know?
Computers use different amounts of memory for different data types. An Integer usually takes up less space than a Real number, which is why choosing the right type helps programs run faster!
3. Casting (Changing Data Types)
Casting is the process of manually changing data from one type to another. It's like a magic spell that transforms a String into an Integer.
Why do we need Casting?
When you type something into a computer using a keyboard, the computer usually treats that input as a String. If you type the number "10", the computer sees it as text, not a value. You can't add 5 to the word "10"! You have to "cast" it into an Integer first.
Examples of Casting in Python-style logic:
• int("10") → Converts the string "10" into the integer 10.
• str(15.5) → Converts the real number 15.5 into the string "15.5".
• float(5) → Converts the integer 5 into the real number 5.0.
Step-by-Step Example:
1. User inputs: "18" (This is a String).
2. Program uses int() to cast it: 18 (Now it's an Integer).
3. Now the program can do math, like: 18 + 1 = 19.
Key Takeaway: Use casting when you need to change how the computer "thinks" about a piece of data so you can use it in a specific way (like doing math or joining it to a sentence).
Final Summary Checklist
Before you move on, make sure you can:
• Identify the five main data types (Integer, Real, Boolean, Character, String).
• Choose the most suitable data type for a given piece of information.
• Explain what Casting is and why it is useful (especially for user input).
• Remember that Phone Numbers and Postcodes are usually Strings, not numbers!
Great job! You've just mastered the foundations of how programs handle information. Keep practicing identifying these types in your own code!