Data types are the classification of data items. Data types represents a kind of value which determines what can be done to that data.
What are the different types of data in Python?
Data Types | Examples | Explanation | Mutable/Immutable? |
---|---|---|---|
Strings | "Hello!", "23.34" | Text - anything between " " becomes string |
Immutable |
Integers | 5364 | Whole numbers | Immutable |
Floats | 3.1415 | Decimal Numbers | Immutable |
Booleans | True, False | Truth values that represent Yes/No | Immutable |
Lists | [1,2,3,4,5] | A collection of data, sits between [ ] |
Mutable |
Tuples | (1,2,3,4,5) | A collection of data, sits between ( ) |
Immutable |
Dictionaries | {"a":1, "b":2, "c":3} | A collection of data, sits between { } |
Mutable |
Data types are set when you assign a value to a variable.
Examples | Type | Explanation |
---|---|---|
ex_1 = "Hello World" | string | The data assigned sits in between " " |
ex_2 = 254 | integer | The data assigned does not sit in between " " , and is a whole number |
ex_3 = 25.43 | float | The data assigned does not sit in between " " , and is a decimal number |
ex_4 = ["Anna", "Bella", "Cora"] | list | A list of strings - Data assigned sits in between " " , within [ ] |
Use the type( ) function to check data types.
type(x) determines and returns what is the type of the input x
Click the triangle button to run the codes and see the output:
To convert variables from one type to another (i.e. integers to floats), we use type conversions as follows:
Data Type | Syntax |
---|---|
strings | str() |
integer | int() |
floats | float() |
lists | list() |
Application
1. Determine and print the type of the following variables
2. Convert the following variables print the result