Data Types in Python

Data types! The types of data!
As we saw in the previous part, a variable can store all kinds of data.
a = 4
a = 4.0
a = "four"
In this section, we shall go ahead and talk about some of the different types of values.

Primitive Data Types

First, we begin by addressing the core data types — these are the smallest unit of data in a Python program.
the simplest kind of value one can have in Python. It can either be true or false.
True, False
rounded values in Python, and can accept any number value, negative or positive.
1, -5, 192849 etc.
decimal values in Python, can accept any value, negative or positive, ranging from negative infinity to positive infinity.
1.0, -5.46 Upper bound: float('inf') Lower bound: float('-inf')
a group of characters in Python, often representing a word.
"hello", 'greetings', "4"

Collection Data Types

There is one more type of data, that is essentially a collection of other data types. These, we shall call collection data types. We'll only work with two examples of collection data types for now — lists and tuples.


Lists are an editable ordered collection of other data.
l1 = [1, 2, 3]
l2 = ["hi", 4, True]
l3 = [l1, l2]
As seen above, a list can be all one kind of data, it can contain multiple kinds of data, and it can even contain other lists. It is important to note that in lists, order matters. [1, 2, 3] and [1, 3, 2] are different lists.
You can index into lists, and even edit them by doing so! Lists are "zero-indexed" — the first element is at zero, the second at 1 and so on and so forth.
>>> l = [1, 2, 3]
>>> l[0]
>>> l[1] = 5
>>> l
[1, 5, 3]


Tuples are like lists, but immutable. In the previous section, we saw how you can index into a list to change it. This is not the case with tuples. You cannot change a tuple once you've created it.
l1 = (1, 2, 3)
Except for this crucial difference, lists and tuples are quite the same. You can index into a tuple to retrieve a value, and you can put anything in a tuple — primitive data types, or even other lists or tuples.

Truth-y and False-y Values

In Python, some values are truth-y — they evaluate to the boolean True while others are false-y — they evaluate to the boolean False.
False-y: False, 0, None, [], ""
Truth-y: True, 1, 2, [1], "61a"
While we'll mess around with truth-y and false-y values a lot more later, one way of seeing this is to wrap any of these in a bool and see what that spits out.
>>> bool(1)
>>> bool(0)