π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.
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.
Boolean
the simplest kind of value one can have in Python. It can either be true or false.
True
, False
Integers
rounded values in Python, and can accept any number value, negative or positive.
1
, -5
, 192849
etc.
Float
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')
String
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
Lists are an editable ordered collection of other data.
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.
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.
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.
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.
Last updated