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.
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.