πŸ‘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.

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

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]
1
>>> l[1] = 5
>>> l 
[1, 5, 3]

Tuples

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)
True
>>> bool(0)
False

Last updated