Previously we saw how the Len function gives us a type error when we give it a
number instead of a string.
So let's talk a little bit more about data types and functions.
And the way I think about functions is kind of like some sort of fancy machine
that you might see in a factory, right? So in this case,
we've got some sort of machine that's going to take potatoes into chips and we
don't really care how it does it,
but it's probably going to have to peel the potatoes, wash the potatoes,
cut it up, fry it,
and then finally return it to us as an output in the form of fries.
And if this is the first time that you're watching one of my tutorials, um,
this is probably a health warning. I talk about food a lot. Um,
so be prepared if you haven't eaten.
Now let's say that we took the same machine that normally processes potatoes and
we decided to get a rock and we just pass this through the function,
then we're getting an error, right? This is basically what happened here.
This length function doesn't like working with integers.
And by forcing this through,
we end up with an error and our code breaks and it gives us this thing, a type
error.
Now do you remember previously we had this challenge where we asked you to get
the um, get an input from the user, what is your name?
And then we use the length function to get the number of characters of their
name. And let's just save that to a variable. Let's call it, um,
num_char equals this.
And then we print your name has plus num_char, number of characters + characters.
So in this case you would expect it to prompt the user to enter their name.
Let's say I entered my name Angela,
and the length of that string would be 6
so this would be equal to six. And so this would get substituted in here.
And then with string concatenation, we end up printing
"Your name has six characters." But this is not what happens.
And I know that some of you might have tried this for the previous challenge and
you would have gotten a type error.
So it tells you that you can only concatenate strings,
not integers. So in this case,
we were concatenating a string to an integer.
This is what we get. We end up with an error.
So how can we prevent these type errors and how can we see the data type that
we're working with? Well,
we could use a function called type,
which basically will check whatever you put between the parentheses and give you
the type of data that it is. For example,
if I go ahead and just comment out this line of code
which breaks and go ahead and put num_char,
so this particular piece of data inside this type function,
and then I'm going to print the type of num_char.
And now if I hit run, let's enter my name.
And you can see it's telling you that this is an integer.
So adding a string to an integer doesn't make any sense,
which is why you got the type error.
Now every so often when you're writing code and you're not quite sure what the
type of data of something might be,
then you can simply just put it inside a type check function,
right? Like so. And then you'll get an answer.
Now in addition to type checking,
we can also do type conversion or you might hear it called type casting where we
change a piece of data from one particular data type to another.
So it, for example,
we know that this variable num_char has the data type of integer.
If we wanted to turn it into a string so that this line of code wouldn't break
anymore,
then we could convert it into a string by writing str
and then inside the parentheses, we put in the variable that we want to convert,
which is called num_char. And now we save this into a new variable,
we'll call it new_num_char.
And once we've converted this into a string and stored it under this name,
then we can use it inside our print statement, new_num_char,
and now it won't break anymore. And it tells us your name has six characters.
So in this case we've taken a integer data type,
put it inside the parentheses of a str function,
which takes a object in between the parentheses and converts it into a string,
which we then store inside this new_num_char
and we can now use it in our print statement because all the data types of all
the pieces of data that we're adding together have the same data type, string.
Now you're not limited to only converting numbers into strings.
You can convert a whole bunch of different data types.
So let me go ahead and comment out this code and I can show you that
let's say that we had a variable a, which we set two a number,
right? 123.
And if I do a type check on a and print this,
you'll see that as you would expect, it should give you integer, right?
A is an integer.
But now let's say I converted a into a string by wrapping it inside the string
function, then hit enter. You can see it's now of type string.
Now I can do this with other things as well. So for example,
I could turn my whole number here into a float and when I run my code,
now we see that the type of my variable is a float
and that's pretty straightforward, right?
But let me ask you two quick questions before we wrap up.
What will this line of code print? Alright, you guessed it,
this will print 170.5 but what's actually happening behind the scenes is we're
converting this string 100.5 into a floating-point number and then we're adding
70 to 100.5 and finally we're printing the results.
But what about this line? What will print here? Have a guess.
Well, this time we get 70100. So in summary,
you can use the type function to investigate the data type you're working with
and you can use functions like string, int, or float to convert to that data
type. In the next lesson, I've got a coding challenge for you.
So head over there and we'll get started.