StudyVault
Home Unit Overview Exam Technique Revision Techniques ← Previous Lesson Next Lesson →

← Back to Dashboard

Overlay:
0:00 / 0:00 Narration generated using AI text-to-speech.

Data Types & Structures

Computers do not really know the difference between a person’s age, a price in pounds, and the answer to a yes-or-no question — unless you tell them. A data type is the label a program attaches to a piece of data so the computer knows how to store it and what it is allowed to do with it. Choosing types well is one of the marks of a careful programmer.

The basic data types

A handful of types are used again and again. An integer is a whole number with nothing after a decimal point, so it suits counting things like lives, scores or the number of students in a class. A real number, sometimes called a float, has a decimal part, which makes it right for measurements and money, such as a height of 1.72 metres or a price of £19.99.

A Boolean can hold only one of two values, True or False. It is the natural choice for anything with a yes-or-no answer, such as whether a user is logged in or whether a game has been paused.

The last two deal with text. A character is a single symbol — one letter, digit, space or punctuation mark. A string is a sequence of characters joined together, so a name like “Ada Lovelace”, or even an empty piece of text, is a string. A string can contain digits, but if you store a phone number as a string you cannot do sums with it — it is text, not a number.

Key Fact

The quickest way to tell an integer from a real is the decimal point. Whole numbers with no decimal part are integers; numbers that need a decimal part, including most prices and measurements, are reals.

Casting: changing a value’s type

Casting means temporarily converting a value from one type to another. Functions such as int() and real() turn text into numbers, while str() turns a number back into text. The original value does not change type for good; casting simply gives you the value in the form you need at that moment.

Casting matters most when a program reads input. Whatever a user types at an input prompt arrives as a string, even when they type digits. To do arithmetic with it you must cast it first. So age = int(input("How old are you? ")) reads the text and converts it into an integer the program can add to or compare.

Imagine a program that asks for two numbers and adds them. If it reads them as strings and writes answer = first + second, then typing 2 and 3 gives “23”, because joining two strings sticks them end to end instead of adding them. Casting each input with int() first makes the program store whole numbers, so 2 and 3 correctly give 5. This is one of the most common bugs a new programmer meets.

Why the right data type matters

The type you choose decides three things. It decides which operations are allowed, because you can multiply two integers but not two Booleans. It affects how much memory is used, since a single character needs far less space than a long string. And it helps catch mistakes, because a program expecting an integer age can reject the text “banana” before it causes trouble later.

Key Fact

Choosing a suitable data type is not just tidy — it controls which operations you can perform, how much memory the data uses, and whether invalid data can be spotted early.

Data structures: arrays

Storing hundreds of values in separate variables would be a nightmare. An array solves this by holding many values of the same data type under a single name. Each value is reached by its index, a position number that starts at 0, so the first item is at index 0, the second at index 1, and so on. Arrays have a fixed length, meaning their size is set when they are created and does not change.

array colours[5]
colours[0] = "Blue"
colours[1] = "Pink"
print(colours[0])   // displays Blue

A one-dimensional (1D) array is a simple list. A two-dimensional (2D) array adds a second index so it behaves like a grid of rows and columns, which is ideal for a game board, a seating plan, or emulating the rows of a database table. You reach a cell with two numbers, one for the row and one for the column.

array board[3,3]
board[0,0] = "X"
board[1,2] = "O"
print(board[1,2])   // displays O

Suppose you need to store the marks of 30 students. Without an array you would need 30 separate variables and 30 lines just to add them up. With an array you can use a single loop that visits each index in turn, so the same few lines work whether there are 30 marks or 3000. This is why arrays and loops are so often used together.

Records

An array is perfect when every item is the same type, but real things often need several different pieces of information. A record groups related fields about one subject, and crucially those fields can be different data types. A single student record might hold a name as a string, an age as an integer, an average mark as a real, and whether fees are paid as a Boolean. You reach one field by its name, usually written with a dot.

student.name = "Ada"      // string
student.age = 15          // integer
student.average = 72.4    // real
student.feesPaid = true   // Boolean
print(student.name)       // displays Ada
Key Fact

The key difference is variety of type. An array holds many items of one single data type, whereas a record holds several fields about one thing and those fields can each be a different data type.

Put these ideas together and you can model almost any information. You might use a record to describe one student and an array to hold a whole class of those records — the right data type inside the right data structure. Whenever you plan a program, ask what kind of value each item is, and whether you are storing one of something or many.

Practice Questions

Test yourself on this lesson. Pick a question, write your answer, then get it marked.

AI feedback
AI feedback is guidance, not a final exam mark. If you disagree, your teacher's view is always the tie-breaker.
← Back to unit
Video

© 2026 StudyVault · About · FAQ · Copyright & IP · Privacy · Disclaimers

StudyVault Education Ltd · registered in England and Wales, company no. 17490559 · 71–75 Shelton Street, London WC2H 9JQ