- [Instructor] In our lives to video,
we took a look at some different data types,
but sometimes we might want one data type
to be converted into a different data type
because some data types don't play well together.
For example, if I said X equals four
to assign the value of four to the variable of X,
what if I wanted to say that the ID of something was four?
I could say, print, open parentheses
and ID is going to be a string.
I'll say ID colon, space, close quote,
I'll give a plus sign and say,
I want to add to that the number four which is X.
Is this going to work?
It's not, because the ID colon space in quotes,
it's a string, the X, it's value is an integer.
We cannot add an integer to a string
and it's going to come back and tell us
that we've got an error.
What we could do instead though,
is we could convert the X to a string value.
You see, I could use the string function str
and in parentheses if I say X,
it's going to come back and say, Oh yeah,
the string value is a four,
notice that puts it in single quotes,
which by the way brings up an interesting point.
We can use straight single quotes
or straight double quotes interchangeably.
So let's use this string function
nested within our print function.
I can say print open parentheses,
again, I'll do a quote ID colon space, close quote plus
and I want to give the string value of the variable X.
I'll say str and that parentheses all say X,
and then I'll close my parentheses
for the print function and press enter
and then it says ID colon space four, perfect.
We can also get the integer value
of a floating point data type.
We can say for example, int for integer open parentheses,
3.14159 close parentheses and press enter
and it's going to come back and say,
the integer value of a Pi that I'll put in there is three.
Now, here's what might not be obvious
when we do something like this,
It's obvious that the nearest integer to 3.14159 is three
but what if I said integer of 3.9
do you think it's going to round up to a four?
It does not.
When we say integer and give a floating point value
in parentheses, it's simply going to ignore
the decimal and everything after it.
Which begs the question,
how do I round to the nearest number?
Or Roundup if I'm right, exactly in the middle.
Here's a little trick, we can add a 0.5
to our floating point value.
For example, I could say integer,
and in parentheses 3.9 plus 0.5
and this will round to the nearest integer,
which is going to be four because that would add up
to four point four and we would ignore
the 0.4 leaving us with a four
and I could do an integer 3.1 plus 0.5
and obviously the nearest integer is three
and by only adding a 0.5 we get to 3.6
it's going to ignore the 0.6 and it's going to give us
the nearest integer of a three.
And there is one final example,
we could use the float function to say,
give me the floating point value
of let's say an integer like three.
What would the fording point value be?
Well, floating point values have a decimal,
well, that's simply going to be 3.0
and that's all it can have.
We can do some conversions between our data types.