1.3.1
Data Types
Data Types
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.
![Illustrative background for Integer](https://image-v2.cdn.app.senecalearning.com/2018-08/aacf844e-cf35-40d2-a56b-b14419c63a84/numbers-,h_400,q_80,w_640.jpg)
![Illustrative background for Integer ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/aacf844e-cf35-40d2-a56b-b14419c63a84/numbers-,h_400,q_80,w_640.jpg)
Integer
Integer
- Integers are whole numbers.
- E.g. 12
- E.g. 0
- E.g. -142
![Illustrative background for Float](https://image-v2.cdn.app.senecalearning.com/2018-07/f94ca99e-2d75-4ec4-8b2d-ee2b333d89b4/Resolution-Numbers-Words-Complicated,h_400,q_80,w_640.jpg)
![Illustrative background for Float ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/f94ca99e-2d75-4ec4-8b2d-ee2b333d89b4/Resolution-Numbers-Words-Complicated,h_400,q_80,w_640.jpg)
Float
Float
- Floats are fractional numbers.
- E.g. 0.42
- E.g. 1.00
![Illustrative background for String](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
![Illustrative background for String ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
String
String
- Strings are text data.
- E.g. "Hello, world!"
- E.g. "07123 456 789"
![Illustrative background for Boolean](https://image-v2.cdn.app.senecalearning.com/2018-09/7343f65e-30ec-4adb-a9bb-02d7679aa914/on-off-switch,h_400,q_80,w_640.png)
![Illustrative background for Boolean ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/7343f65e-30ec-4adb-a9bb-02d7679aa914/on-off-switch,h_400,q_80,w_640.png)
Boolean
Boolean
- A True or False value.
- Examples:
- True.
- False.
Casting
Casting
Casting is the process of converting data from one type to another.
![Illustrative background for Reasons for casting](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
![Illustrative background for Reasons for casting ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
Reasons for casting
Reasons for casting
- 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.
![Illustrative background for Casting functions](https://image-v2.cdn.app.senecalearning.com/2018-08/6f2b13b6-c43f-4553-a7b0-3a5ad8b8b888/programming-1873854_1920,h_400,q_80,w_640.png)
![Illustrative background for Casting functions ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/6f2b13b6-c43f-4553-a7b0-3a5ad8b8b888/programming-1873854_1920,h_400,q_80,w_640.png)
Casting functions
Casting functions
- 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
Type coercion allows us to operate on data of different types.
![Illustrative background for Type coercion](https://image-v2.cdn.app.senecalearning.com/2018-08/0b9abdcd-92f0-4907-94c7-8c3e47c3e19d/stack-chase-error-code-binary-,h_400,q_80,w_640.jpg)
![Illustrative background for Type coercion ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/0b9abdcd-92f0-4907-94c7-8c3e47c3e19d/stack-chase-error-code-binary-,h_400,q_80,w_640.jpg)
Type coercion
Type coercion
- Usually, attempting to use data of different types in an operation (e.g. adding two numbers of different types) will cause an error.
- Type coercion allows one of the types to be automatically cast so that the operation can be completed.
![Illustrative background for Integers and floats](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/1.1.3 - Periodic table/floating-low-density,h_400,q_80,w_640.jpg)
![Illustrative background for Integers and floats ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/chemistry/1.1.3 - Periodic table/floating-low-density,h_400,q_80,w_640.jpg)
Integers and floats
Integers and floats
- When working with a float and an integer, the final result will become a float.
- E.g: 3 + 2.5 = 5.5
![Illustrative background for Strings and integers](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
![Illustrative background for Strings and integers ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/physics/AQA New Modules/1.1.2/string force,h_400,q_80,w_640.jpg)
Strings and integers
Strings and integers
- When working with a string and an integer, the final result will become a string.
- E.g: "Number = " + 3 = "Number = 3"
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
Jump to other topics
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered