Data types
We have previously created variables to store information. In this section we will examine what kinds of variables we can create, and what kinds of information a computer can handle, known as datatypes.
Similar but different
We can create (or define) three different variables like this:
kids_in_the_class = 25
temperature = 18.5
my_language = "python"
All these variables are created in the same way, with different and unique names, but they contain different datatypes.
The first two contain numeric data. In short, numbers in some form. The first one is a whole number, or also called an integer, because there are no decimals present. We often short the name of this type of data to just "int". The second variable with decimals are often called "floating-point number" in computer lingo, or just "float".
The last variable contains letters: It's a piece of text. We can also see it as a string of letters, and that datatype is therefore often called "string". A string must always be written in between single or double quotation marks (' or ").
A fourth data type we will use later on is called "boolean", which like a power switch only has two possible values: "True" or "False".
I_know_python = True
Which data type?
We can ask Python what type of data is stored in a variable using type:
Try to run the code, should prints:
<class 'int'> <class 'float'> <class 'str'> |
A program will only work properly if we as programmers keep track of what type of data is where. It's important, because that decides what we are able to do with a variable.
For example, the following code works:
# Example 1
print(kids_in_the_class + temperature)
But this crashes:
print(my_language + temperature) # Crashes!
Variables in the first example can be added, because both are numeric, but the variables in the second example cannot, because one contains a string and the other contains a float. How can we add "Python" to 18.5? It's not possible without some kind of conversion, and the result instead is that our program just crashes. Try it!
Some programming languages, for example Javascript, try to help out and converts variables into some form to allow what it thinks we want to do. But Python instead gives us an error message. A little later, we will learn to extract information from these error messages. But right now, we'll be satisfied with knowing that something went wrong.
Converting between types
These two variables seem to contain the same thing:
birthyear = 2010
year = "2010"
But it's important to note that they (in the computer world) are very different! The first variable is a number (int) and we can ask the computer to calculate our age:
print(2022 - birthyear)
But this would not work:
print(2022 - year)
Sure, there are only digits in the "year" variable, but it is a string, because they are contained within quotation marks. From the computer's perspective it's just like any other text. To make the calculation work we must convert to the right datatype, by telling the computer we actually can use the content as an int:
print(2022 - int(year))
Now that we understand different types of data, we are ready to make our applications interactive, which we will do in the next section.
In this video, we're looking at how different datatypes can be used together, and especially when they can't.
- Data type: What type of data a variable contains. This can be an integer \((5)\), a floating point number \((2.3)\), a string ("Marcus") or something else.
- Integer: A whole number, without decimals, like \(5\) or \(42\). Shortened to "int".
- Floating point number: A number with decimals, like \(3.14\), Shortened to "float".
- String: Various pieces of text is called a string. Shortened to "str".
- Conversion: Changing information from one datatype to another, for example from the string "5" to the integer \(5\). Also known as casting.