Computing
Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE

## Lesson Video 12

• Watch the Lesson video
Make notes on your notes sheet.
Make notes on your notes sheet.
• Complete the learning activities
Make any notes on your notes sheet.
• Check the Learning outcomes

## What do I need to Learn?

 I need to learn how to understand the concept of a data type. I need to learn how to understand and use an integer appropriately. I need to learn how to understand and use a real appropriately. I need to learn how to understand and use a Boolean appropriately. I need to learn how to understand and use a character appropriately. I need to learn how to understand and use a string appropriately.

## Key Terms

Data Typels Integer Float Boolean Character String Type mismatch error Casting.

### Data types

#### The purpose of data types within code

Data can be stored in many different forms and the proper term for these forms is ‘data types’. A computer uses special internal codes to keep track of the different types of data it processes. Most programming languages require the programmer to declare the data type of every data object. It is these forms that determine what actions, for instance searching, sorting or calculating, can be performed on the data when it is held within a program or field of a database or a spreadsheet.

The most common data types you will have used are called primitive data types. Primitive data types are predefined types of data that are supported by the programming language.

Table of the most common primitive data types

### Data types diagram

Data type Example Size
Integer (whole number) 4, 27, 65535 1 to 8 bytes
Real (decimal number) 4.2, 27.4, 5.63 4 to 8 bytes
Character a, F, 3, \$, £, # 1 byte
String abc, hello world Limited to the amount that can be stored in main memory
Boolean true or false 1 bit

### Integer

Integer data types deal with whole numbers, not decimal numbers, which use a different data type.

An integer is a whole number (not a fractional number) that can be positive, negative or zero.

An example of integers could be: -9, 3, 5, 8, 98, and 5103.

In python when we want to convert a variable (cast) to an integer data type we would do the following:

## variable = int(variable)

### Question 1

In mathematics which of the following are integers?

6, -7, 2.6, 10, 4.5, 17, 17.0

### Real

A real data type contains numeric data in a decimal form. It is used in situations where more accurate information is required than an integer can provide.

But real data types cannot store the actual measurement symbol (km, kg, etc.) or the units of measurement, for instance kilometres or metres per second. If you want to use a real data type, you must remember to add the measurement symbol separately and print the units after the field displaying the data type.

In python when we want to convert a variable (cast) to a real data type we would do the following:

## variable = float(variable)

### Question 2

What are the differences between real and integer data types?

### Boolean

The Boolean data type represents the values of true / false or yes / no. The primitive data type of a Boolean is logical. Boolean logic is a type of mathematical comparison. It is used to evaluate true or false. This may be new to you but it is not difficult to understand. Think about binary numbers. The lights could be on = 1 or off = 0.

Boolean logic evaluates every expression to either true or false.

### Question 3

Write the following sentences as a Boolean expression:

• When the door is open and it is cold outside I have to wear my coat.
• The central heating switches off when it is warmer than 22°C and on when it is cooler than 18°C.

### String

A string or text data type is capable of holding any alphanumeric character, whether it is text, a number or a symbol. It is also capable of storing non-printable characters such as carriage returns as well as punctuation characters and spaces. The data contained within a string data type can either be pure text or consist of a combination of letters, numbers and symbols.

It is important to note that when you use string data types to store numbers, you cannot perform any sort of mathematics on them. The sort of numbers that you would normally store as string data types would be things like telephone numbers where they often start with a zero - if you stored them as an integer, the zero would be deleted (remember an integer cannot have a point, so 07 is stored as 7). As you never want to perform mathematical calculations on a telephone number, the string option is fine.

In python when we want to convert a variable (cast) to a string data type we would do the following:

## variable = string(variable)

### Date / time

The date / time data type is obviously used to store dates and times. The tricky aspect of this data type is that both dates and times can appear in many different forms. Also, some countries have different methods of representing the date, such as:

• 11/06/2012 means 6th November 2012 in the USA
• 11/06/2012 means 11th June 2012 in the UK.

### Question 4

What data type would you use to store each of the following values?

• Steve
• 15
• 007
• B901LK
• A
• 20.2
• True
• 1
• 6/12/83

#### Extension question

Which of the above values could not be cast into the data type suggested when using python?