Data Types
A data type is a set of values and a set of operations that can be performed on these values. Let's first look at Python data types in terms of just their values.
Built-in Data Types
Python has a set of built-in data types. Built-in means that they are readily available to use in the Python language, without having to get them from external sources. We will only be discussing the main built-in data types, but do know that more exist. There are two main categories of built-in data types: numeric types and container types. Numeric types are numbers, while container types are used to group values together. Within the numeric types, we have int, bool, and float. Within the container types, we have another category called sequence types. Inside sequence types, we have list, tuple, and str. And the other container types are dict and set. Lastly, a type, which is not part of numeric or container types, is the None type. Now let's look at each type in more detail, starting with the numeric types.
int
The first numeric type is int. It represents positive integers, negative integers, and 0. Other programming languages, may have a restricted range of values for integer types. For example, in Java, its int type is restricted to around negative 2 billion to positive 2 billion. Values outside of this range are a different type, called a long, in Java. However, in Python the range for int is unlimited, and only restricted by the computer's memory. Therefore, all integers, whether they be very large or very small, all have a type of int. This is convenient for us because we don't have to deal with different integer types depending on the integer value.
bool
Within the int type, we have the bool type, which represents boolean values. Its set of values are True and False. Note that True and False must start with capital letters. bool is a subtype of int, because under the hood, True is 1 and False is 0.
float
The last numeric type is float. It represents numbers that have a decimal point. Unlike int, the range of possible float values in Python is restricted. But the range is large enough that we don't need to worry about it.
Sequence Types
Now let's look at the container types. There is a subcategory of sequence types within the container types. Here we have a group of ints and floats that make up a sequence. We call these values elements. The defining feature of a sequence is that it uses integer indices to order and access its elements. The singular form of indices is index. The indices of a sequence are pre-defined. They are always int values that start from 0 and go up by 1.
list
The first sequence type is list. It represents a list of values. Each element in the list has a corresponding index. The defining feature of lists is that after they are created, their elements can change. For example, we can add, replace, or delete elements. To create a list, we use a pair of square brackets. This code has a type of list and represents an empty list. If we add an int inside, this becomes a list with one element. To add more elements, we use a comma for separation and then write the next element. All container types, except for str, can contain any combination of types as elements.
tuple
The second sequence type is tuple. It represents a list of values, just like the list type. Each element in the tuple has a corresponding index. The difference between a list and a tuple is that the elements of a tuple cannot change after the tuple is created. Therefore, once we create a tuple like this, we cannot change its elements. To create a tuple, we use a pair of parentheses. This code has a type of tuple and represents an empty tuple. If we add an int inside, this code no longer has a type of tuple, and is instead an int. It represents an int value wrapped in parentheses, like how we use parentheses in math. To make it a tuple type, we have to add a comma. So this a special syntax for the one element case of a tuple that we have to keep mind. If we create a tuple with more elements, we use commas as normal.
str
The last sequence type is str. str is short for “string”, which means a sequence of text. Each character in a string has a corresponding index. Unlike other container types that can contain any type as elements, strings only ever have single characters as elements. To create a string, we use a pair of quotation marks. We can either use a pair of double quotes or a pair of single quotes. Both of these have a type of string and represent empty strings. In between the quotations, we can add any sort of text, which includes numbers, symbols, and spaces.
dict
Now for the other container types. We first have the dict type, which is short for dictionary. Unlike sequences which are indexed using pre-defined ints, dictionaries are indexed with any type. The index is called a key. And it corresponds to a value. This key-value pair is referred to as an item. Keys are how we access the values of the dictionary. Therefore, the keys we set must be unique. Although dictionaries are not indexed using ints, which have an inherent order, the items of the dictionary still preserve their order. For example, if we create a dictionary like this, then the item “apple” 5 will always be first in the dictionary. This means that the order of the dictionary is a feature that we can rely on. To create a dictionary, we use a pair of curly braces. This has a type of dictionary and represents an empty dictionary. A key-value pair is created by writing the key, a semi-colon to separate the key from the value, and then the value. The key-value pairs are separated with commas.
set
Next, we have the set type. A set is a group of unique elements that do not have indices. Therefore, unlike the previous container types, we cannot access specific elements of the set. The elements in the set are unique so there will never be duplicate elements. Even if we add duplicate elements to the set, the actual set in memory will not have duplicates. Set is the only container type that is unordered. This means that the order of the elements is not preserved. For example, if we create a set with elements in this order, the next time we access the set, we cannot guarantee that 1.5 will be the first value. This means that the order of elements is not a feature we should use or rely on for sets. To create a set, we use a pair of curly braces, just like we do when creating a dictionary. But if both dictionaries and sets use curly braces then what is the type of a pair of curly braces? The answer is that a pair of curly braces always has a type of dictionary and represents an empty dictionary. If we want to write an empty set in our code, we use the set() constructor function with no arguments. Let's go back to the curly brace notation. Once we add an element to the set, the code now has type set. What makes the code have type set instead of a dictionary is that the elements are single values and not key-value pairs. We can add more elements to the set and separate them with commas.
None
Lastly, we have the None type. Its set of values is a single value called None. None must be written with a capital N. The None value used to represent the absence of a value.