Data types in programming
A data type tells a program what kind of value a piece of data is, which decides how it is stored and what you can do with it. The five basic data types in programming are integer (whole numbers), real or float (numbers with a decimal point), Boolean (true or false), character (one symbol) and string (text). You can add two integers, but adding two strings joins them, so the type changes what the same operation does.
Those five are the ones the AQA GCSE Computer Science specification lists, noting that languages may call them by other names. The table shows the names Python and JavaScript use.
The main data types
| Data type | Holds | Examples | Python | JavaScript |
|---|---|---|---|---|
| Integer | Whole numbers, positive or negative | 42, 0, −7 | int | number |
| Real (float) | Numbers with a fractional part | 3.14, −0.5, 2.0 | float | number |
| Boolean | One of two values | True, False | bool | boolean |
| Character | A single letter, digit or symbol | 'A', '7', '?' | str of length 1 | string of length 1 |
| String | A sequence of characters: text | "Hello", "SW1A 1AA", "" | str | string |
Integer
Use an integer for anything you count: scores, ages, the number of items in a basket. Integer arithmetic is exact. Python integers can be as big as memory allows, while many other languages give each integer a fixed number of bits, which limits how big it can get.
Real, or floating point
Use a real (called a float or double in most languages) for measurements such as height, temperature or speed. Reals are stored in binary floating point, which cannot represent most decimals exactly. Try this in Python or the Code Editor:
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // falseThe error is tiny, but it means you should never test two reals for exact equality, and you should not store money as a float. Store prices as a whole number of pence (or cents) in an integer instead: £19.99 becomes 1999.
Boolean
A Boolean has only two values, true and false, and is what every condition in an if statement or while loop produces. Name Boolean variables as yes or no questions, such as isLoggedIn or has_paid, so code reads clearly.
Character and string
A character is one symbol, stored as a number using a character set such as ASCII or Unicode: in both, "A" is 65. A string is a sequence of characters, so it has a length and you can pick out characters by position. Python and JavaScript have no separate character type; a character is just a string of length 1.
Choosing the right data type
Ask what values the data can take and what you will do with it. Here is a record for one student.
| Data | Best type | Why |
|---|---|---|
| Name | String | Text of any length |
| Age in years | Integer | A whole number you might compare or add to |
| Height in metres | Real | A measurement with a fractional part, such as 1.68 |
| Has paid for the trip | Boolean | Only yes or no |
| Form group letter | Character | Exactly one symbol, such as B |
| Phone number | String | It can start with 0, may contain + or spaces, and you never do arithmetic with it |
The phone number is the classic exam trap. It is made of digits, but storing it as an integer would drop the leading zero, and adding two phone numbers means nothing. The same goes for postcodes, PINs and card numbers.
Converting between data types: casting
Changing a value from one type to another is called type conversion, or casting when you do it on purpose. Anything typed by a user arrives as a string, so you often need to convert it before doing maths.
age = int(input("Age: ")) # "15" becomes 15
price = float("4.50") # 4.5
label = str(42) + "kg" # "42kg"
whole = int(3.9) # 3: int() cuts off the decimal part, it does not roundSome conversions happen automatically, which is called implicit conversion. Adding an integer to a real gives a real in most languages: 5 + 3.2 is 8.2. Explicit conversions such as int(3.9) can lose information, so check whether you want to cut off the decimal part or round it.
Primitive and composite data types
The five types above are primitive: single values built into the language. Composite types are built from them. An array or list holds many values in order, a record or object groups named fields (such as the student above), and in languages such as C a pointer holds the memory address of another value. Python and JavaScript treat strings as a basic type, while in C a string is an array of characters.
Static and dynamic typing
Languages also differ in when they check types. In a statically typed language such as Java, C or TypeScript, each variable has a fixed type, and mistakes are caught before the program runs. In a dynamically typed language such as Python or JavaScript, a variable can hold any type and change it, and type errors only show up when that line runs.
value = 10
value = "hello" # fine: the variable now holds a string
result = value + 5 # TypeError when this line runsSort some real languages by how they run and how they check types, then press Mark to check your answer in the course:
Sort languages into compiled/interpreted, then static/dynamic bins.
Press Mark to have your answer marked in the Programming Languages course.
Common mistakes
- Storing phone numbers, postcodes or IDs as integers. Leading zeros vanish and the symbols do not fit. Use a string.
- Forgetting that input is a string. Convert it with int() or float() before doing arithmetic.
- Using a float for money. Rounding errors creep in. Count pence in an integer.
- Comparing reals with ==. Check whether they are close enough instead.
- Thinking int() rounds. In Python, int(3.9) is 3. Use round() to round.
Common questions
What are the 5 main data types?
What is the difference between an integer and a real?
Is a string a primitive data type?
What data type should a phone number be?
What is casting in programming?
Does JavaScript have an integer type?
Revision cards
Say the answer first, then flip the card to check it.
Where to go next
- Solve it
Code Editor
Write and run code right in your browser
Open the tool - Learn it
Programming Languages
Compiled vs interpreted, paradigms and the evolution of how we talk to machines
Start the course - Test it
GCSE Computer Science
A new exam-style paper every day, with instant marking and a mark scheme
Start today's paper