x = None
print(x)
print(type(x))None
<class 'NoneType'>
Every piece of information in a Python program has a type. The type tells Python what kind of data it is dealing with, and what you are allowed to do with it. For example, Python treats the number 5 very differently to the word "five" — even though they might mean the same thing to us, Python needs to know which one it is looking at before it can work with it.
Using data types in Python is straightforward: you assign a value to a variable, and Python automatically recognizes the type.
For example, x = 10 creates an integer, whereas x = 10.3 will be treated as what is called a ‘float’ because it has a decimal place.
In this lesson, we will look closely at the data types you will use most often. For each one, you will see:
The main data types we will go through are:
NoneType — represents “no value”bool — True or Falsestr — textint and float — numberslist and tuple — ordered collectionsset — a collection of unique itemsdict — key-value pairsPython also has a small number of more specialised data types — complex, range, frozenset, and the binary types bytes, bytearray, and memoryview. You will meet range later in the course. We will only mention them briefly on this page, without going through worked examples, as you are unlikely to need them yet.
It is also possible to define your own custom data types, and to use extra data types from external packages — for example, NumPy arrays and pandas DataFrames are extremely useful for working with data, and the decimal module is useful for money and other calculations that need exact rounding. We will meet these later in the course.
None is a special value in Python that represents “nothing” or “no value at all”. It is often used as a placeholder, for example when a variable exists but does not have a proper value yet.
Syntax:
type() function
The type() functions allows you to inspect the data type of an object. It can be very helpful.
A boolean can only ever be one of two values: True or False. Booleans are extremely useful for making decisions in your code, and are often produced automatically when you compare two things.
Syntax:
You do not always have to type True or False yourself — comparisons produce a boolean for you automatically:
A string stores text, and is written between either single quotes ('...') or double quotes ("..."). Strings are one of the most commonly used data types, since almost every program needs to display or process some text.
Syntax:
An integer is a whole number, with no decimal point.
Syntax:
A float is a number with a decimal point, used whenever you need more precision than a whole number.
Syntax:
Python also has a complex data type for complex numbers (numbers with a real and an imaginary part). This is rarely needed outside specialist mathematical or engineering work, so we will not cover it further here.
A list is an ordered collection of items that you can change after creating it — you can add, remove, or update items freely. Lists are written using square brackets [ ], with each item separated by commas.
Syntax:
['apple', 'banana', 'cherry']
<class 'list'>
Because lists can be changed, you can add a new item at any time. We will see more of this later.
A tuple is also an ordered collection, but unlike a list, it cannot be changed once it has been created. Tuples are written using round brackets ( ), and are useful for data that should stay fixed, such as coordinates.
Syntax:
(51.5074, -0.1278)
<class 'tuple'>
Python also has a range type, which represents a sequence of numbers (often used with loops later in the course). We will not cover it in detail here.
A set is an unordered collection that only ever keeps unique items — if you try to add a duplicate, Python simply ignores it. Sets are written using curly brackets { }.
Syntax:
Notice that even though we typed 2 and 3 more than once, each number only appears once in the set. This makes sets very useful for quickly removing duplicates from a collection.
Python also has a frozenset type, which behaves like a set but cannot be changed once created. We will not cover it in detail here.
A dictionary stores data as pairs of keys and values, so that you can look up a value using its key, rather than its position. Dictionaries are written using curly brackets { }, with a colon between each key and its value, and a comma between each pair.
Syntax:
{'name': 'Ada', 'age': 25}
Ada
Python also has three binary data types — bytes, bytearray, and memoryview — which are used for working directly with raw binary data, such as image or file data. These are more specialised and will not be covered in detail here.
As well as all of the built-in data types above, Python allows you to define your own custom data types with their own rules and behaviour. Many useful custom data types have already been created by others, so you will often be able to simply use one someone else has written, rather than building your own from scratch. Tomorrow we will briefly introduce you to NumPy, which is used to work with numerical arrays, and contains a custom data type for this.
You will often need to convert a value from one data type to another — for example, turning a number typed in as text into an actual number you can calculate with. Every built-in data type in Python has its own function that you can use to convert a value into that type.
It is not possible to convert between every pair of data types. Some conversions simply do not make sense, and others need extra information before Python can work out how to do it.
You can find more detail in the official Python documentation:
Here are some worked examples of converting between data types:
Converting int to float
Converting float to int
Converting int to str
Converting str to int
Converting str to float
Converting list to tuple
(1, 2, 3)
Converting tuple to list
[4, 5, 6]
Converting int to bool
Converting list to set
Converting a list to a set is a quick way to remove any duplicate values:
Now that you have seen how each data type works, here is a summary table you can use for reference. Each data type has its own strengths and weaknesses, so it is worth thinking about which one best fits what you are trying to do. As we work with the data types in the next few sections, this will start to make more sense.
| Category | Type | Common Use Case | Benefits | Disadvantages |
|---|---|---|---|---|
| None Type | NoneType |
Represents absence of value | Useful for optional values and placeholders | Can cause errors if operations expect a value |
| Boolean | bool |
Represents True or False | Simple and efficient for control flow | Limited to two states (True/False) |
| Text | str |
Text representation | Immutable, supports Unicode (multilingual text), rich manipulation methods | Inefficient for heavy modifications |
| Numeric | int |
Represents whole numbers | No precision issues | Limited to integers |
| Numeric | float |
Represents decimal numbers | Supports real numbers | Precision issues due to storage |
| Numeric | complex |
Represents complex numbers | Direct support for complex arithmetic | Rarely needed in general programming |
| Sequence | list |
Mutable sequence of items | Flexible, dynamic size, rich methods | Slower resizing operations |
| Sequence | tuple |
Immutable sequence of items | Memory-efficient, safe | Cannot change values once set |
| Sequence | range |
Sequence of numbers | Memory-efficient | Less flexible than lists |
| Mapping | dict |
Key-value pairs | Fast lookups, flexible types | Higher memory usage due to hashing |
| Set | set |
Unordered collection of unique items | Fast membership testing | Unordered in the Python specification; no indexing |
| Set | frozenset |
Immutable set | Hashable, can be used as dictionary keys | Cannot modify after creation |
| Binary | bytes |
Immutable binary data | Efficient, compact for binary data | Not human-readable, cumbersome for some operations |
| Binary | bytearray |
Mutable binary data | Allows modification of binary data | Consumes more memory than bytes |
| Binary | memoryview |
Efficient access to binary data without copying | No data duplication | More complex to use |
Level:
What is the syntax of a dictionary? Write a couple of your own dictionaries.
A dictionary is written using curly brackets, with a colon between each key and its value. See the examples below:
{'language': 'Python', 'number': 2}
Level:
State the best data type to use in Python for each of the following:
list — Ordered and mutable collection of names.set — For fast membership tests with unique elements, where order is irrelevant.tuple — Immutable sequence, perfect for storing fixed data like geographic coordinates.int — Used for counting and working with whole numbers.float — If precision is essential, consider decimal.Decimal, but float is fine for most cases.dict — For key-value pairs (e.g., storing data about a person).bytearray — Mutable sequence of bytes, useful for modifying binary data.bool — Represents binary conditions (True or False).frozenset — Immutable version of a set.None — Represents the absence of a value, useful for functions that don’t return anything.float — Floating-point numbers are used for measurements and continuous values.str — For handling textual data with built-in manipulation methods.Which data type you choose affects memory usage and the speed of computations, which may matter in larger programs. For further reading, investigate why sets are generally faster than lists or tuples for finding unordered elements (membership tests).
Level:
What happens when you convert 3.5 to an integer?
The integer displayed is 3 — notice that this is not rounded to the nearest whole number.
The output of both is 3. This is because int() does not round a number — it simply cuts off (or “truncates”) everything after the decimal point.
If you want proper rounding instead, Python’s round() function rounds a float to the nearest integer. However, if the decimal is exactly 0.5, round() rounds to the nearest even number, which is Python’s default behaviour and can catch beginners out. If you need more control than this, a library such as the decimal module is a good alternative.
Level:
What data types are used in expenses_calculator.py?
Level:
What data types can be used as Dictionary keys in python?
In python an immutable data type can be used as a key for a dictionary. It must be hashable. This includes string , numerical data types, and even tuples.
There are many built-in data types in Python. We will explore several of them further in the next few sections. It is important to use the correct data type for your use case, rather than reaching for the same one every time.