Python · Variables and types
Types and Casting
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Introduction to data types
Like you saw earlier, variables can hold different types of data. Here is a really short introduction to the most basic ones.
There are 4 basic types. - Two are numbers (integer and float) - One is a boolean (bool) - The last one is a string (str).
The following examples will be about numbers because we talked about them in the previous chapter. We will talk about booleans and strings a bit later.
Exercise 2 — Getting the type of a variable
First, how do we see the type of a variable in Python?
This is pretty easy, you can use the
type()
function and put the variable name inside the parentheses.
level = 1 # we declare an int number print(level, type(level)) weight = 70.5 # we declare a float number print(weight, type(weight))
If you run this code, you will see the type of each variable printed in the console. It should looks like this for the first line :
1 <class 'int'>
So, what does this mean? The
1
is easy, it's the value of the variable. The
<class 'int'>
part tells you that this variable class is an integer (int).
It works the same way the second line with the float variable! (and with every other type of variable you can create in Python)
Exercise 3 — Changing type of a number
Now that you know the basis of type, let's see how to change the type of a number.
Now, let's say you want to change the type of one of your variables. For example, go from a float to an integer.
Doing that is called type casting.
Here is how to do this with numbers: -
int()
to convert a number to an integer -
float()
to convert a number to a float
Let's see how to use them for real in the next exercise!
Exercise 4 — Int to Float
Let's try to convert an
int
to a
float
using
float()
.
Run the code to see the result!
level = 1 # we declare an int number print(level, type(level)) level = float(1) # we declare 1 and cast to float print(level, type(level))
You can see that the type of the variable has changed from
int
to
float
and its value is now
1.0
.
As you can see, the value didn't change much, it added a .0 at the end. This is what happens when you convert an integer to a float, it adds a decimal point.
Exercise 5 — Float to Int
Now let's do the opposite: convert a
float
to an
int
using
int()
.
Run the code to see the result!
level = 1.5 # we declare a float number print(level, type(level)) level = int(1.5) # we declare the value and cast to int print(level, type(level))
You can see that the type of the variable has changed from
float
to
int
and its value is now
1
. It basically removed everything after the decimal point.
Exercise 6 — What's the difference ?
Does the final value differs between
int(9.5)
and
float(9.5)
?
Options
• Yes, because ```int(9.5)``` will return 9 and ```float(9.5)``` will return 9.5
• Yes, because both will return 9.5, but different types
• No, because ```int(9.5)``` will return 9.5 and ```float(9.5)``` will return 9
• No, because both will return 9.5
Hint : Remember: usually casting an int to a float doesn't change the value but casting a float to an int will often change the value because it removes the decimal part.
Exercise 7 — Practice casting!
Match each calculation of the left with its result on the right.
Items to match
• int(9.2)
• float(9)
• int(9)
• float(9.2)
Definitions
• 9
• 9.0
• 9
• 9.2
Exercise 8 — Practice time!
Can you guess what the following code will print? Try to run it and see if you were right!
print(1+1) print(1-1) print(1+1.0) print(1-1.0)
Calcul with numbers
If you encounter a calculation with a
float
and an
int
, Python will always return a
float
. Try to keep in mind that it will always try to keep the most precise type possible.
Exercise 9 — What's the result ?
What would be the result of
5 + 3.0
?
Options
• ```8.0```
• ```8```
• We can't compute a float and an int together
• It will raise an error
Hint : Remember: When mixing integers and floats in operations, Python chooses the most precise type.
Exercise 10 — Cast yourself!
Create a variable named
level
containing a float value. Then cast it to an integer because a level can't be a decimal number. Print the value and its type before and after the casting.
Starter code
# Code here
Back to the course