Data types
- Integer: a whole number, such as 42. Real (also called float): a number with a decimal part, such as 3.14. Boolean: True or False.
- Character: a single letter, digit or symbol, such as 'A'. String: a sequence of characters, such as "Hello".
- Choosing the right type saves memory and avoids errors. A phone number is stored as a string, because it can start with 0 and is never used in calculations.
Casting
- Casting converts data from one type to another: int("42") gives the integer 42, and str(42) gives the string "42".
- Input from the keyboard is a string, so cast it to a number before doing maths with it.
Arrays
- An array stores several values of the same type under one name. Each value has an index, which usually starts at 0: names[0] is the first item.
- A 2D array is like a table, and needs two indexes, such as grid[row][column]. Check the question to see which index comes first.
- In many languages an array has a fixed size. Python uses lists, which can grow and shrink.
Records
- A record groups related data of different types into fields. A student record might have a name (string), an age (integer) and whether they have paid (Boolean).
Key terms
- Integer
- A whole number.
- Real
- A number with a decimal part (also called a float).
- Boolean
- A data type with only two values: True or False.
- Character
- A single letter, digit or symbol.
- String
- A sequence of characters, such as a word or sentence.
- Casting
- Converting data from one data type to another.
- Array
- A data structure that stores several values of the same type under one name.
- Index
- The position of an item in an array, usually counted from 0.
- Record
- A data structure that groups related fields of different types.
Quick questions on Data types, arrays and records
- Which data type should store 3.75?
- Real (float): It has a decimal part.
- Which data type stores True or False?
- Boolean: It only has two possible values.
- Why should a phone number be stored as a string?
- It can start with 0 and isn't used in calculations: Storing it as an integer would lose the leading 0.
- What is casting?
- Converting data from one type to another: For example, int("7") gives the integer 7.
- In most languages, what is the index of the first item in an array?
- 0: Indexes usually start at 0.
- names holds "Ali", "Bo" and "Cy", in that order. What is names[1]?
- Bo: Counting from 0: names[0] is Ali and names[1] is Bo.
- What does an array store?
- Several values of the same type under one name: Each value is reached using its index.
- What does a record store?
- Related data of different types in fields: For example, a name, an age and a paid flag.
- Which data type holds exactly one letter, digit or symbol?
- Character: A string is a sequence of characters.
- What does int("42") give?
- The integer 42: It casts the string to an integer.
- How is a 2D array best described?
- A table of rows and columns: It needs two indexes.
- Which data type best stores the number of students in a class?
- Integer: You can't have part of a student.
- What does this program print from the list?
- 19: Indexes start at 0: scores[0] is 12, scores[1] is 7 and scores[2] is 19.
- What does len() print after append?
- 4: append adds "Di" to the end of the list, so it now holds 4 items.
- What does this 2D list print?
- 4: grid[1] is the second row, [4, 5, 6]. Then [0] is its first item: 4.
- This program finds an average. What does it print?
- 5.0: The total is 15, and 15 / 3 is 5. In Python, / always gives a real (float) answer, so it prints 5.0.
- What does this program print after casting?
- 12: int(x) casts the string "4" to the integer 4, and 4 × 3 = 12. Without int, "4" * 3 would give 444.
- This program finds the biggest number. What does it print?
- 9: biggest starts as 5. Only 9 is bigger, so biggest becomes 9 and stays 9.
- This program counts letters. What does it print?
- 2: The loop looks at each letter in turn: l, e, v, e, l. There are two e's.
Data types, arrays and records: exam-style questions and mark schemes
Which data type is most suitable for storing whether a student has paid for a trip? [1 mark]
Answer: Boolean
Give the most suitable data type for: (a) a price such as 4.99 (b) a person's name (c) the number of pupils in a school. [3 marks]
- (a) Real (float) (1)
- (b) String (1)
- (c) Integer (1)
The array scores holds 12, 7, 15 and 9, in that order, with indexes starting at 0. State the value of scores[2] and explain why. [2 marks]
- 15 (1)
- Indexes start at 0, so index 2 is the third item (1)
Explain why a programmer might use an array rather than 30 separate variables to store 30 test scores. [2 marks]
- One name and an index can be used for all the scores, instead of 30 variable names (1)
- The scores can be processed with a loop, so the code is shorter and easier to maintain (1)
State what this program prints. [1 mark]
- 13: grid[0][1] is 8 and grid[1][0] is 5 (1)
The list temps holds seven temperatures, one for each day of a week. Write code that finds and outputs the highest temperature, without using a built-in max function. [4 marks]
- Sets a variable to the first item (or a very low number) before the loop (1)
- Loops through every item in the list (1)
- Compares each item with the highest so far and updates it when the item is bigger (1)
- Outputs the highest after the loop has finished (1)
A program stores details of 30 students: their name, their age and whether they have paid for a trip. Explain the data types and data structure you would use, and why. [6 marks]
- name: string, because it is text
- age: integer, because it is a whole number
- paid: Boolean, because it can only be true or false
- a record groups the different data types for one student together
- an array (of records) stores all 30 students under one name
- each student is found with an index, so a loop can process every student
- an array is better than 30 separate variables: less code, easier to change
- input is read as a string, so age is cast to an integer
How to answer them: exam technique and command words.
Data types, arrays and records: questions and answers
- Which data type holds exactly one letter, digit or symbol?
- A string is a sequence of characters.
- What does int("42") give?
- The integer 42. It casts the string to an integer.
- How is a 2D array best described?
- A table of rows and columns. It needs two indexes.