So far we have introduced different types of values. Before we go any further we will explore this a little more to ensure that we are clear about differences between the types of values and the variables where they are stored.
print()
.type
function¶We will use the function type
to identify how Python stores the following values.
print(type(24))
24
is recognised as a value of type 'int'
, where 'int'
as we might expect is short for integer
.
print(type(70.0))
70.0
is a 'float'
. Finally:
print(type("James"))
Hopefully there are no surprises in the types that Python assigns to the values above, but what about variables. Let's try run type
on a new variable, changeling
:
print(type(changeling))
As we found out at the end of the last episode, we cannot use a variable before it has been assigned a value. So let's assign a series of values and repeat:
changeling = 24
print(type(changeling))
changeling = 70.0
print(type(changeling))
changeling = "James"
print(type(changeling))
We can see that in Python variables do not have a well defined type but are mutable, depending upon the value stored in them
The type
of values determines what can be done with them for instance we have already seen that we can happily perform calculations e.g.:
print(5-3)
We have also seen that if we mix integers
and floats
, Python is able to work out what to do:
print(2+3.5)
Because integers
and floats
are ways of representing numbers mathematical operations involving their values are well defined.
As well as using the function type
to retrieve the type of a value there are also a set of functions which can be used to change the type of a value. These functions take the form of the value types int()
, float()
and str()
print("String:","2", "Integer:", int("2"), "Float:", float("2")) # Take the string "2", convert it to a integer, a float and print all three cases
print("Float", 2.5, "String:", str(2.5), "Integer:", int(2.5)) # Take the float 2.5, convert it to a string, an integer and print all three cases
print(1 + int("2"))
Alternatively it may have been our intentions to 'add' or combine the two strings
"1"
and "2"
in which case we would instead need:
print(str(1)+"2")
So far we have used the functions str
, int
and float
, to change the type of values. How does running these commands on a variable affect the type of the value assigned to them? When can investigate this with the following:
my_variable = "2" # First we assign the value, string "2", to the variable my_variable
print(my_variable) # Print the variable
print(my_variable*5) # Verify that the variable is a string but testing the behaviour with `*`
print( float(my_variable) ) # Convert the value in variable to a float and print it
print(my_variable*5) # Verify what the variable type is now by testing the behaviour with `*`
We see that this has not changed the type of the value stored in my_variable
. The commands only change the type
of the value at that point in the code. For example if we have the string "2"
, running int("2")
does not change the fact that "2"
is a string.
If we want the conversion to be reflected in the value stored in a variable, we must reassign the variable:
my_variable = "2" # First we assign the value, string "2", to the variable my_variable
print(my_variable) # Print the variable
print(my_variable*5) # Verify that the variable is a string but testing the behaviour with `*`
print( float(my_variable) ) # Convert the value in variable to a float and print it
my_variable = float(my_variable) # Now we explicitly reassign the my_variable
print(my_variable*5) # Verify what the variable type is now by testing the behaviour with `*`
We can now see that the variable has been explicitly reassigned to the value converted to a float, and this has been verified by checking that *
now performs the numerical operation we expect rather than the repeating the string multiple times.