What are common data types for python?
A brief introduction into data types for python.
Data types are a really important concept in programming. They help the system that's doing the work to understand what the data actually is and what can potentially be done with that data. To better illustrate this concept, let's consider the following questions:
- What's the square root of the number 9?
- What's the square root of the word "encyclopedia"?
Did you notice how your brain approached the first question vs the second? The first question was pretty straight-forward. It's purely a math exercise; the square root of 9 is 3. Cool, got it. What about the other question though? If you're like me, you probably thought: What the heck is a square root of a word?! That's ridiculous and makes no sense!
This brings me back to my point: our options for handling data depend on the data type. So with that silly (but hopefully resonating) example, let's go over some common types of data. For this article, we'll be focusing on data types in python but a lot of these principals and mindsets apply to other programming languages as well.
Strings
A string is basically just a sequence of characters. Those characters can be a LOT of different things including letters, numbers, symbols, and spaces. Here are some examples:
"This is an example of a string"
'This is another example of a string and includes a symbol at the end!'
'987654321'
Did the last bullet example confuse you? That's a string of characters that happen to be numbers, but not actually the mathematical number 987654321. The way we tell python that something is a string is by placing the data in single quotes or double quotes.
Integers
So now, we know that we can represent text data as strings. What if we actually want the data to be handled like a mathematical number? One option would be to use integers.
Integers are basically just data points to help with counting and representing whole numbers. They can be positive (1, 6, 3333333). They can be negative (-3, -7, -999999). It's also totally fine for an integer to be zero (0). The important thing to keep in mind though, is that integers are whole numbers. Because of that, integers typically represent whole, countable quantities.
Here's an example use case: let's say I'm building an web analytics platform to count the number of ad clicks that users perform while visiting a site. Is it possible for a single user to perform 1.25 clicks while on a site? Not really – a user will either click on an ad, or they won't.
Here's another example: let's say I'm building an inventory application for a grocery store that's tracking produce in a warehouse. If I'm tracking fruit inventory, can I have 1.37563 oranges? Not really (at least not for anything relevant to inventory management). Having decimals for number of oranges wouldn't really be important for what I need the system to do.
Floats
So if integers are only used for whole numbers, what if we have some scenarios where we DO want to track data down to decimal points? That's where floats come in. Floats are short for "floating point numbers" and essentially mean that the data involves decimals and the decimals can move around depending on the value we're tracking. For example:
1.021
-10.21
102.1
I'll skip examples for this since it's probably self-explanatory.
Boolean
Booleans another popular data set but you need to think of them in a completely different way. Up until now, we've been giving python very specific values that can be wildly different. Things like: 12345, bob, -1454.24329472.
Booleans only ever have two possible values:
True
False
Huh. So if booleans are only ever "true" or "false", how is that useful? Why would we want to use booleans? Well, keep in mind that computers and programming languages follow instructions. At certain points, a program may need to make a decision. For example:
- Input validation: Does a submitted username contain valid characters?
- State Management: Is a user logged in? Does a video game character have a quest item?
- Application preference toggles: Does the user have setting XYZ enabled in their application preferences?
- Checking for file: Does a file exist in the file system?
All of these examples really just have two states: true or false. Consider something like the file check operation: either a file exists (true), or it does not exist (false). There's no situation where a file would both exist and not exist.
Summary
This was just a very brief preview into data type concepts but hopefully it resonated. More to come soon. Thanks!