2.1 Learn about types - Video Tutorials & Practice Problems
Video duration:
5m
Play a video:
Video transcript
<v ->So now that we're starting lesson two,</v> let's go over some of the general programing concepts that we'll cover in the rest of this course. Here's a list of them, we'll do about half of them in lesson two and the other half in lessons three and four. Before I dive straight in to talking about types, let's look at some other piece of code. We can write a string of characters by using these quotes and saying 'Hello World' or whatever we want. We can actually find out how long a string is, how many characters it has, by using a function called len. So then we can put 'Hello World' into here and it'll spit out 11 characters. So that's one, two, three, four, five, six, that space, seven, eight, nine, ten, eleven. So that's a useful function to have. But now what happens if we want to use that len function, or blanks, on a number? It gives us an error. So here's a Traceback and it says inside of this file on this line I found a TypeError, and it says object of type 'int' has no len function. So why is that? Well, it's because the number nine and the string 'Hello World' are different types. So what are types? Well, just like dogs have different breeds or different types of dog, they have different characteristics and attributes, and mostly I just wanted to have a photo of a bunch of dogs in my slideshow. Data in Python has to be stored on the computer in different ways, so some data types are going to use up less space and some are gonna use up more. You can also do different things with them, they have different properties and attributes, and so let's look at some of these different types. So we've already seen two in this example, and I can find the type of a piece of data by using a new function called a type. So it's called type, I call it by, I say I want to run this function by using these parentheses, and then inside I'll put in the object that I want to analyze. So 'Hello World' and it says it has a class of S-T-R, str, which is short for string. I can check for the type of nine and that's 'int' which is short for integer, so it's a whole number. I'm pressing up to put the last command back on the line. And then if I put a dot on there, it's now of type 'float' because floats have decimal places. And nine dot is the same as 9.0. So those are three different types, string, int, and float. There's a few more that we haven't talked about yet, one is Booleans, which are true or false. So if you remember, that data is all stored in binary on a computer, this would be the same true or false is equal to one or zero, and in Python it has to have the capital T True, or capital F in False. If I use lower case true, I get a different kind of error, and it says NameError, name 'true' is not defined. So in lesson 2.3, we're gonna talk more about errors and debugging and figuring out what these error messages mean, and for now we'll just say that it's expecting true to be a variable, and you haven't defined it yet, and it is not equivalent to this true, which is a Boolean. So I can check the type of True, and it says 'class bool.' And lastly, there's one called None, and that has class 'NoneType.' So here is an overview of the different built-in types that we've seen, so Strings, Integers, Floats, Booleans, and NoneType. So in other languages, none might be nil or null, but in Python it's None with a capital N.