Previously, we saw how the len() function gives us a TypeError 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, this is probably a health
warning,
I talk about food a lot.
So be prepared if you haven't eaten.
Now, let's say that we took the same machine that normally processes potatoes, and we decide to give
it a rock, and we just pass this through the function, then we're going to get an error.
Right?
This is basically what happened here.
This len() 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
TypeError.
Now if we hover over this yellow warning we also get the explanation here.
Expected type...something called, 'Sized', and got 'int' or an integer instead.
So this is already pretty clear where our mistake is even before we hit Run.
And the other helpful thing is, when you hover over any function in PyCharm, it gives you the definition
of it,
and in between the brackets, you can see the data type that that function expects.
So the len() function expects a data type with something that's called, Sized.
And we can actually click on this link to see the documentation on docs.python.org, which is the official
Python documentation.
Now I know previously I told you to try using something like W3Schools or using StackOverflow, and
the reason is because the documentation from the official Python organization is very, very short.
So sometimes it can be enough,
but very often I find that it's helpful to find another source to back up this very, very brief explanation.
But in our case, in this situation, it does help us.
For example, it tells us that len() will return the length, which is the number of items of an object,
and the argument, which is what you put in between the brackets may be a sequence such as a string,
byte, tuple, list or range, or a collection such as a dictionary set or frozen set.
Don't worry about these other data types that we haven't come across yet.
We're going to come across them in due time and we're going to be learning all about them.
But for now, you already see one that you recognize which is string.
So it can take string,
but nowhere in this list does it say it can take an integer or any other number data type.
So as the first pause exercise, I want you to fix this len() function so that we actually give it data
type that it recognizes.
And we fix this TypeError so that when we run it, it works as expected.
And you can put anything in there as long as it gets rid of the warnings and errors.
Pause the video now.
All right.
This should be relatively easy.
We can change this to any string, which we already know is a data type that this function can accept.
So we can simply change it to "Hello" or whichever string you can think of.
And now when we hit run, all our errors and warnings should now go away.
If you see something like "PEP 8 blank line at end of file," and it bothers you, don't worry about it too
much,
but it's basically saying that in an ideal situation, a Python file should only have one blank line
at the very bottom.
So if I hit Run again, that will clear up that warning. For everybody out there who has a little bit
of OCD like me, this can be a lifesaver.
So now that we've seen that in Python, each function expects to work with a specific set of data types,
so there's only certain data types that can go into the parentheses of a particular function.
Now here comes the question,
"Well, how would we know what data type a particular piece of data has?"
So how would we know what is the data type of
"Hello"?
Well it's very easy to visually see it because it has the double quotes around it,
and anything with double quotes around it, even if it's a number, is in fact a string.
But there is also one function in Python that allows us to check the data type of any piece of data
or variable.
So all we need to do is to use the keyword "type",
and then in between the parentheses we can put in any piece of data.
It can accept any data type.
And then it will give us in return the data type.
But as always in order to see it we have to wrap this inside a set of parentheses and use the print
statement to put this into the output area.
So let's run that.
And you can see it tells us that this is something called a class 'str'.
So this is basically saying it checked the data type of this piece of data
we gave it,
and it told us that it's actually a string which is what we expected as well.
So we can do this with various different data types.
Let's check it with the number.
Well in this case we gave it a whole number,
so no decimal places.
And it told us it's an integer.
We could do this with floats with booleans,
and it will always very, very helpfully tell us what is the data type.
And this is something called type checking.
So now it's time for another quick pause
check to make sure you understood what's going on.
I want you to write out four lines of code to print out the four different data types that we have already
learned about.
So it's going to be the string, the integer, the float, and the boolean.
And we want all four of these printed out in four lines in the output area, using what you've learned
and the correct data type to put in inside the parentheses before the type check.
So pause the video now and give this a go.
All right so we've got already the integer type.
So let's put the string one above it.
And we know that all strings go inside a set of double quotes.
And then we want a floating point number.
So we could do a 3.14.
And finally, we want a boolean type which we know is either True or False with a capital T and capital
F.
So now if I hit Run, we should see class str, int, float and bool.
The full collection of all the major primitive Python data types that we learned about in the last lesson.
If that was confusing at all, review the last lesson and hopefully this all makes perfect sense.
Now what if we aren't happy with the assigned data type?
What if we want to convert a piece of data into a different data type?
Well, then we would need to learn about something called type conversion, also known as type casting
in Python.
So let's go ahead and clear the board to make this less confusing.
And let's start out with a very simple string.
So we all know that this is a string because it's enclosed in a set of double quotes.
But what if I wanted to turn it into an integer?
Well, I can use one of the built-in functions called "int" and I can convert this into a number.
So whereas previously we saw that if we tried to print the string "123" + "456",
then we would end up with 1, 2, 3, 4, 5, 6.
And because both of these pieces of data are string data types, the plus sign does concatenation instead
of maths and adding these two together.
But now that we've converted this 1, 2, 3 into an integer, you can see that if we replace
this string with this type casted integer and we could do the same for 4, 5, 6.
Now if I hit Run, you'll see that it actually treats them as numbers and adds them together.
So this is the power of type conversion.
Now, one of the dangerous things about this is of course sometimes you can't convert things into a
different data type.
For example, if I tried to convert, "ABC" into a number, well, that doesn't really make sense.
If I gave that to you, what would ABC be as a number?
I don't know.
So if we run this, you can see we get something called a ValueError.
So I hope you're collecting these errors in a notebook somewhere because we're coming across lots of
them,
and you will come across more in your lifetime as a Python developer.
So this is saying that there is an invalid literal for the int, which is ABC, and so it doesn't really
make sense to convert this into a number is what it's trying to tell you.
So you have to be really careful when you're doing type conversions because sometimes you can end
up with errors and sometimes you can just simply lose data.
But we can use this method to convert things into all of the four major primitive types that we've learned
about. So we can convert to an integer, we can convert into a float, we can convert into a string,
and we can even convert into a bool.
So to check that you've understood how to convert between different data types and why we get different
type errors, I have a final pause challenge for you, which is to make this line of code run without
errors.
So I want you to go ahead and copy this line of code, paste it into your code file, and try to hit run.
Now go ahead and enter a name triggered by this input, and then you will see that you get a TypeError.
So read through the TypeError,
see if you can understand what it's trying to tell you, and fix this code so that it does what you
expect it to do, which is to print out the number of letters in the name that the user submits into
the input.
Pause the video.
Give this a go.
All right.
So in order to break this problem down, let's use some variables to simplify things and make it easier
to read.
Let's take this second part of this print statement, and let's take it out into separate variables.
So we know that we can get the name_of_the_user by using this input() function,
and I'm making these variable names extra wordy just so that when you read it, it makes it a little
bit more sense.
And I'm actually going to delete the rest of this as well, just to make everything easy to read.
So next we need the length_of_name,
and this is done by using the len() function,
and we pass in the name of the user that we got from line one.
So now finally, what we have here is the length of the name which we want to tag to the end of this print
statement.
Now what is this error trying to tell us?
It's saying you can only concatenate string to a string, not an integer, which is what it sees in
this line of code.
So where is the integer?
Well, we know that when we calculate the length of something it gives us a number.
It gives us a whole number.
So this is probably an integer.
well,
we can verify this by using a type check.
Right?
We can print the first part of this concatenation,
and we can put it inside a type function to check what is the data type of this first part.
And then we can also check the data type of the second part, which is now called, length_of_name.
So let's comment this out.
Let's go ahead and run this code.
Enter a name and you can see the first one is a string and the second one is an integer.
So this is a type str,
and this is a type int.
So now it makes sense that when we're printing, we can't simply concatenate a string to a number,
it doesn't know how to do that.
So how can we convert this length_of_name into the same data type as this first part, which is a string?
Well, you learnt how to do that already.
All we need to do is to wrap this inside a set of parentheses, which is the string type conversion.
And so now we have this part which is now a string,
and then we add it to another string.
And now if I run this code again then you'll see that it works without any issues.
Number of letters in your name: 6, printed out.
So hopefully, this makes sense to you and you'll be able to understand in the future how to do type conversions,
and how to understand when you have different type errors
what has gone wrong.