Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Data Types

Categories that classify data values and determine which operations can validly be performed on them.


Quick Answer

Categories that classify data values and determine which operations can validly be performed on them. Use them to predict what an operation will do: 5 + 3 gives 8 because both values are integers, but '5' + '3' gives '53' because both are strings, and + joins text. The common types are integers (whole numbers), floats (decimals), strings (text), booleans (true or false), and arrays or lists (collections). Choosing the right type, and converting when needed, keeps calculations correct.

Why This Matters

Type mistakes cause real bugs: a shop page that reads quantities as text can join '2' and '3' into an order for 23 items, and integer division can turn 7 divided by 2 into 3. Knowing the type of every value tells you what + or / will do before you run the code, and when you must convert, such as turning typed input into a number before doing maths with it.

Intuitive Explanation

A computer stores everything as patterns of bits, so it needs to know how to read each pattern. The data type is that instruction. The same symbol 5 can be the number five, which you can add and multiply, or the character '5', which is just a piece of text like 'A'. The quotation marks in code tell you which: 5 is an integer, '5' is a string.

The type decides what each operation means. For integers, + adds: 5 + 3 = 8. For strings, + joins: 'ice' + 'cream' = 'icecream', and '5' + '3' = '53'. For booleans, the operations are AND, OR, and NOT. Some mixtures are refused: in Python, '5' + 3 is an error, because the language will not guess whether you meant to add or to join. Division can surprise you too: in Java or C, 7 / 2 with two integers gives 3, dropping the remainder, while 7.0 / 2 gives 3.5.

A value that looks like a number is not always one. A phone number such as 0412 555 019 is stored as a string, because nobody adds phone numbers and a number type would drop the leading 0. Typed input usually arrives as a string too, so age = input() followed by age + 1 fails or misbehaves until you convert it with int(age). Ask what a value is for, not just what it looks like.

Think of it like this. Data types are like the units on a measuring jug and a ruler. Both show numbers, but adding 3 cups to 3 centimetres makes no sense, so you check what each number measures before combining them. A computer checks types the same way. The comparison stops at strings: text measures nothing, and joining two pieces of text just sets one after the other.

Core idea. Operations depend on type: you can add numbers, concatenate strings.


When to Use

Use data types when code mixes numbers and text, when you must predict the result of +, /, or a comparison, when user input feeds a calculation, or when choosing how to store a value: age as an integer, price as a float, name as a string, a yes-or-no answer as a boolean. If the question is only about where a value is stored and how it changes, use Variable; if it is about combining true and false values, use Boolean Logic.

Tip. Ask: Does the result depend on what kind of value it is, such as a number, text, or true/false?

How to Recognize It

Before predicting what an operation will do, look at the kind of value on each side of it.

  1. Is a value written in quotes, like '42', even though it looks like a number?

    Yes means it is a String, so + joins it as text, and arithmetic will not work until you convert it.

  2. Does the answer change depending on whether a number is whole or has a decimal part?

    Yes means you need to tell an Integer from a float, for example when dividing or when working with money.

  3. Does the operation make sense for this kind of value, unlike multiplying two names together?

    No means a type error: the operation is not allowed for that type. Yes means check what result that type gives.

  4. Is the question only about where a value is kept and how it changes, not what kind it is?

    Yes points to Variable. Data types are about the kind of value inside the box.

Data Types vs Variable vs Integer vs String

These words get swapped because every variable holds a value of some type. Ask whether you are talking about the idea of kinds of value, the box, or one particular kind.

Data Types

Meaning
Data types are the categories of value, such as integer, float, string and boolean, and each type allows its own operations.
Key test
Which kind is the value, and what does that kind allow?
Formula
T=(VT,OT)
Example
len('cat') gives 3 because text has a length, but len(5) is an error in Python.

Variable

Meaning
A variable is a named box that holds one value of some type.
Key test
The name and the box, whatever type is inside.
Example
points = 0 makes a box called points holding the integer 0.

Integer

Meaning
An integer is the data type for whole numbers, positive, negative or zero, with no decimal part.
Key test
Whole numbers that you can do arithmetic on.
Example
-3, 0 and 250 are integers, but 2.5 is not.

String

Meaning
A string is the data type for text, a sequence of characters written in quotes.
Key test
Quotes around it, and + joins rather than adds.
Example
'12' + '3' gives '123', not 15.

Formula & Notation

Worked Examples

Example 1 — Predict five results and their types

Easy

Using Python's rules, give the result and its type for each expression: (a) 7 + 2, (b) '7' + '2', (c) 7 / 2, (d) 7 // 2, (e) 7 > 2.
Show the steps and the answer
  1. (a) 7 + 2 = 9, an integer.

    Both values are integers, so + adds.

  2. (b) '7' + '2' = '72', a string.

    Both values are strings, so + joins the text.

  3. (c) 7 / 2 = 3.5, a float.

    In Python, / always does true division and returns a decimal.

  4. (d) 7 // 2 = 3, an integer.

    // is integer division: it keeps the whole-number part and drops the 0.5.

  5. (e) 7 > 2 is True, a boolean.

    A comparison does not produce a number; it answers yes or no.

Answer: (a) 9, integer; (b) '72', string; (c) 3.5, float; (d) 3, integer; (e) True, boolean.

Takeaway: Before evaluating an expression, write down each value's type; the types tell you what each operator will do.

Example 2 — Digits that should stay text

Standard

A school gives students IDs such as 00731 and 01452. Should an ID be stored as an integer or as a string? What goes wrong with an integer?
Show the steps and the answer
  1. Store 00731 as an integer: it becomes 731.

    Leading zeros have no value in a number, so the integer type drops them.

  2. Compare: the IDs 00731 and 731 would now look identical.

    If both exist, two students end up sharing one ID.

  3. Ask what IDs are used for: comparing and displaying, never adding or averaging.

    Adding two IDs, such as 731 + 1452 = 2183, gives a meaningless result.

  4. Choose a string: '00731' keeps every digit exactly as written.

    A string stores the characters themselves, which is what an ID really is.

Answer: Store IDs as strings; as integers, 00731 becomes 731 and loses its leading zeros.

Takeaway: Digits are not automatically a number; choose the type by what you will do with the value.

Example 3 — Find the mistake: adding typed input

Application

Ines writes a program that asks for two quantities and adds them: a = input('First: '); b = input('Second: '); print(a + b). She types 2 and 3, and the program prints 23. Find the mistake and correct it.
Show the steps and the answer
  1. Check the types: input() returns whatever was typed as a string, so a = '2' and b = '3'.

    The keyboard sends characters; the program does not know they are meant as numbers.

  2. Evaluate a + b with strings: '2' + '3' = '23'.

    For strings, + joins the text instead of adding.

  3. Fix: convert before adding: a = int(input('First: ')); b = int(input('Second: ')).

    int() turns the text '2' into the integer 2.

  4. Trace the fix: 2 + 3 = 5, so it prints 5.

    With integer values, + now means addition.

Answer: Convert the inputs with int(); the program then prints 5.

Takeaway: Typed input arrives as a string; convert it to a number type before doing maths with it.

Where You'll See It

  • Sorting recycling bins

    Paper, plastic, and glass go in different bins because each is handled differently; data types are like those bins, deciding which operations are allowed on each kind of value.

  • A phone number you don't do math on

    Even though it's made of digits, you store a phone number as text (a string) because you never add or multiply phone numbers, showing why the type matters.

  • A light switch (on/off)

    A switch is naturally a boolean: only two values, true or false, just like the data type that powers yes/no decisions in code.

Common Mistakes

  1. The slip: The string '5' and the number 5 are the same thing.

    Why it's tempting: They both look like five on the screen.

    Instead: '5' is text and 5 is a number; '5' + '3' may give '53' (joined text) while 5 + 3 gives 8, so you must convert the type before doing math.

  2. The slip: Dividing two integers like 7 / 2 gives 3.5 in every language.

    Why it's tempting: That's the true math answer, so it feels like what the computer returns.

    Instead: In C and Java, dividing two integers truncates: 7 / 2 gives 3, dropping the fraction, so write 7.0 / 2 if you need 3.5. Python 3 and JavaScript give 3.5, and Python's 7 // 2 gives 3.

  3. The slip: User input that looks like a number can be used in math right away.

    Why it's tempting: It reads as a number on screen, so it seems ready for calculation.

    Instead: Keyboard/user input usually arrives as a string; convert it to a number type before adding or comparing, or the calculation will be wrong or fail.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. In Python, what does each give: `3 + 4`, `"3" + "4"`, `"3" * 2`?

    Hint: Quotes make text. Think about what `+` and `*` do to text.

  2. Name the data type of each value: `42`, `3.14`, `"hello"`, `True`, `[1, 2, 3]`.

    Hint: Look for quotes, decimal points and square brackets.

  3. In Python, what are `7 / 2`, `7 // 2` and `7 % 2`?

    Hint: One gives a decimal, one keeps only the whole part, and one gives the remainder.

  4. `age = input("Age? ")` and the user types 13. Then `print(age + 1)` crashes. What type is `age`, and how do you fix the line?

    Hint: What type does `input` always give back?

  5. A program stores phone numbers such as 0412 555 019. Should they be integers or strings?

    Hint: Will you ever do arithmetic with a phone number? What happens to a leading 0?

  6. A student writes `total = "5" + 5` and expects 10. What actually happens, and how is it fixed?

    Hint: Can Python add text to a number directly?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why does 0.1 + 0.2 not give exactly 0.3?

Decimals are stored as floats, which use binary, and most decimals such as 0.1 cannot be written exactly in binary, just as 1/3 cannot be written exactly as a decimal. The computer stores a value extremely close to 0.1, and the tiny errors add up: in Python and JavaScript, 0.1 + 0.2 shows 0.30000000000000004. That is why money is often stored in whole cents, as integers.

How do I convert a value from one type to another?

Most languages have built-in conversion functions. In Python, int('42') turns the text '42' into the number 42, float('2.5') gives 2.5, and str(42) turns the number back into the text '42'. Converting can fail: int('hello') stops with an error because 'hello' is not a number. And int(3.9) gives 3, because it drops the decimal part instead of rounding.

Is a boolean really just 1 and 0?

Inside the computer it is stored as a bit pattern, and in some languages true and false behave like 1 and 0: in Python, True + True gives 2. But in your program a boolean is its own type with only two values, made for conditions. Treating it as a number usually makes code harder to read, so keep booleans for yes-or-no questions.

Do all languages make me say the type of a variable?

No. In languages such as Java and C, you declare the type when you create a variable, as in int age = 12, and that variable can then hold only integers. In Python and JavaScript you just write age = 12, and the language works out the type from the value; the same name can later hold text. Either way, every value still has a type.

Learning Path

← Before

Variable
Data Types

You are here

You need Variable first: a named box that holds a value. Data types add the kind of value in the box, which decides which operations make sense on it. Next, Integer, String, and Boolean study the most common types one at a time, and Array shows how to store a whole list of values, such as every score in a game, under a single name.

See Also