In the previous lesson, you saw that you can dynamically change the value of a variable in Python. 1 can turn into 2, but it can also turn into "Something Completely Different":
num = 1
num = 2
num = "Something Completely Different"
What are Types of Data
All of these values are different from each other, but you might agree that 1 and 2 are more similar to each other than "Something Completely Different". What 1 and 2 have in common, from Python's perspective, is their type. Both of them are numbers, or more precisely, integers.
Info: Python doesn't think of numbers as "numbers" but instead distinguishes a few different types of numbers. The most common ones are integers, such as 1, and floats, which you can think of as decimal numbers, e.g. 1.23.
The text "Something Completely Different", on the other hand, is not a number. Sequences of characters that make up words and text share a type that is called string in Python.
Type Checking in Python
While you might correctly guess the type of the example values above, doing so can be tricky in other situations. For example, what if all you get to see is the variable name: num?
Just by looking at it, you have no way of correctly inferring what value this variable holds. Therefore, you also have no way of knowing what type that value is. Luckily, there is a concise way of asking Python for the type of a variable:
type(num)
When you execute this in your Python interpreter, it'll show you the Python type of the value that this variable is pointing to. You can experiment with this in the code playground on the next page.
Type Operations
There are certain advantages that come with grouping values into types.
Operations Within the Same Data Type
In a nutshell, it allows you to perform shared operations on values of the same type. You will get to know about the different operations that you can perform on different data types in much more detail. A lot of them might already make sense to you because you have used them outside of programming in a similar way.
Think about what operations or use cases you might expect related to the following types:
- Numbers
- Text
Think about them outside in contexts other than programming. Write down your thoughts in your notebook.
When you think of numbers, you might think that you can, for example, add them together. Or you could perform other mathematical operations with them. When you think of text, you might think that you can change the capitalization, find a word in a text, or use it to display important information.
And indeed, a lot of this functionality translates right into programming, but there is also more on top of that! And there are other types that don't feel quite as familiar from experiences in the real world.
Common Data Types in Python
Below you can see the names and type representations of some of the most commonly used data types in Python:
- Integers:
int - Floats:
float - Strings:
str - Booleans:
bool - Lists:
list - Dictionaries:
dict
There are even more than that, and later on, you will be able to create your own types as well. However, with a good handle on the above-mentioned types, you will be able to fly very high with your Python skills. You will learn more about all of them---plus some others---throughout this course.
However, before you go there, you'll start with a special case. You'll start with Nothing.
Additional Resources
- Official Python documentation on Built-in Types
Summary: Python Data Types
- Values in Python are split into multiple different types
- Types specify the kind of value
- Type differences allow you to perform functions on similar types
Common Data Types in Python
- Integers:
int - Floats:
float - Strings:
str - Booleans:
bool - Lists:
list - Dictionaries:
dict