Source: https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-3-1-data-types

[Revision notes](https://senecalearning.com/en-gb/revision-notes.md) › [GCSE](https://senecalearning.com/en-gb/revision-notes/gcse.md) › [Coding](https://senecalearning.com/en-gb/revision-notes/gcse/coding.md) › [National Curriculum](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum.md) › Data Types

# Data Types

Coding: Introduction to Python v3, topic 1.3.1

## Data Types

Every piece of data that is stored has a type. The type determines how it is stored and what you can do with the data.

### Integer

![Integer](https://image-v2.cdn.app.senecalearning.com/2018-08/aacf844e-cf35-40d2-a56b-b14419c63a84/numbers-.jpg)

- Integers are whole numbers.
 - E.g. 12
 - E.g. 0
 - E.g. -142

### Float

![Float](https://image-v2.cdn.app.senecalearning.com/2018-07/f94ca99e-2d75-4ec4-8b2d-ee2b333d89b4/Resolution-Numbers-Words-Complicated.jpg)

- Floats are fractional numbers.
 - E.g. 0.42
 - E.g. 1.00

### String

![String](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA%20New%20Modules/1.1.2/string%20force.jpg)

- Strings are text data.
 - E.g. "Hello, world!"
 - E.g. "07123 456 789"

### Boolean

![Boolean](https://image-v2.cdn.app.senecalearning.com/2018-09/7343f65e-30ec-4adb-a9bb-02d7679aa914/on-off-switch.png)

- A True or False value.
- Examples:
 - True.
 - False.

## Casting

Casting is the process of converting data from one type to another.

### Reasons for casting

![Reasons for casting](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA%20New%20Modules/1.1.2/string%20force.jpg)

- One of the most common reasons for casting is output.
- Output must be formatted as a string, and so we may need to convert a certain piece of data to a string.
- All input also comes as a string, and must then be converted to other data types.

### Casting functions

![Casting functions](https://image-v2.cdn.app.senecalearning.com/2018-08/6f2b13b6-c43f-4553-a7b0-3a5ad8b8b888/programming-1873854_1920.png)

- Casting to a string can be done using the str function. E.g:
 - str(3) gives "3".
- Casting to an integer can be done using the int function. E.g:
 - int(3.4) gives 3.
- Casting to a float can be done using the float function. E.g:
 - float("3.4") gives 3.4.

## Type Coercion

Type coercion allows us to operate on data of different types.

### Type coercion

![Type coercion](https://image-v2.cdn.app.senecalearning.com/2018-08/0b9abdcd-92f0-4907-94c7-8c3e47c3e19d/stack-chase-error-code-binary-.jpg)

- Usually, attempting to use incompatible data types in an operation (e.g. adding a string to a number) will cause an error.
- Type coercion allows one of the types to be automatically converted so that compatible operations can be completed.

### Integers and floats

![Integers and floats](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/1.1.3%20-%20Periodic%20table/floating-low-density.jpg)

- When working with a float and an integer, the final result will become a float.
 - E.g: 3 + 2.5 = 5.5

### Strings and integers

![Strings and integers](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA%20New%20Modules/1.1.2/string%20force.jpg)

- Python does not automatically coerce integers to strings when using +.
 - "Number = " + 3
 - TypeError: can only concatenate str (not "int") to str
- To join a string and a number, you must explicitly convert the number using str().
 - "Number = " + str(3)
 - Output: "Number = 3"

## Test yourself on Data Types

- What would be the most suitable type to store the number 2.5?
- Which of these data are strings?
- Which would be the most appropriate type to store a username?
- What is one of the most common reasons for casting?
- What is the data type of a calculation involving a float and an integer?

[Practise this topic for free](https://app.senecalearning.com/classroom/course/72d0364c-db0a-4630-8e0a-61f48cae0252/section/b3382286-72c3-4600-92aa-98a52ef5b702/session)

- Previous topic: [1.2.1 Input & Output](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-2-1-input-and-output.md)
- Next topic: [1.4.1 Variables & Constants](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-4-1-variables-and-constants.md)

## Coding: Introduction to Python v3 contents

### 1 Introduction to Python
- 1.1 Python
  - [1.1.1 Python](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-1-1-python.md)
- 1.2 Input & Output
  - [1.2.1 Input & Output](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-2-1-input-and-output.md)
- 1.3 Data Types
  - [1.3.1 Data Types](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-3-1-data-types.md)
- 1.4 Variables & Constants
  - [1.4.1 Variables & Constants](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-4-1-variables-and-constants.md)
- 1.5 Operators
  - [1.5.1 Arithmetic & Relational Operators](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-5-1-arithmetic-and-relational-operators.md)
  - [1.5.2 Boolean & Assignment Operators 2](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-5-2-boolean-and-assignment-operators-2.md)
- 1.6 Program Flow
  - [1.6.1 Selection](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-6-1-selection.md)
  - [1.6.2 Iteration](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-6-2-iteration.md)

### 2 Data Structures
- 2.1 Lists & Strings
  - [2.1.1 Lists](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-1-lists.md)
  - [2.1.2 Strings](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-2-strings.md)
- 2.2 File Handling
  - [2.2.1 File Handling](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-2-1-file-handling.md)

### 3 Modularity
- 3.1 Subroutines
  - [3.1.1 Subroutines](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-1-1-subroutines.md)
- 3.2 Data Flow
  - [3.2.1 Data Flow](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-2-1-data-flow.md)

### 4 Good Practice
- 4.1 Naming & Whitespace
  - [4.1.1 Identifiers](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-1-1-identifiers.md)
  - [4.1.2 Whitespace](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-1-2-whitespace.md)
- 4.2 Comments
  - [4.2.1 Comments](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-2-1-comments.md)
- 4.3 Exceptions
  - [4.3.1 Exceptions](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-3-1-exceptions.md)
