1 00:00:00,490 --> 00:00:01,450 In this lesson, 2 00:00:01,460 --> 00:00:06,789 we'll explore a built in function called type which returns the type of an object. 3 00:00:06,800 --> 00:00:09,199 Python has lots of different data types. 4 00:00:09,210 --> 00:00:13,189 We've explored strings and floats and integers and booleans so far. 5 00:00:13,199 --> 00:00:14,869 And all of these are objects. 6 00:00:14,880 --> 00:00:15,869 Each data type, 7 00:00:15,880 --> 00:00:20,030 each object serves a different purpose and supports different operations. 8 00:00:20,299 --> 00:00:22,780 So we know that everything in Python is an object. 9 00:00:22,790 --> 00:00:26,360 We've heard that phrase a lot before and we're going to hear a lot more in the future. 10 00:00:26,450 --> 00:00:28,399 Here's a new phrase to memorize 11 00:00:28,610 --> 00:00:31,840 an object is an instance of a class. 12 00:00:31,850 --> 00:00:35,849 Let me repeat that an object is an instance of a class. 13 00:00:35,919 --> 00:00:39,680 There's two words there that may be confusing class and instance 14 00:00:40,150 --> 00:00:47,110 a class is a blueprint or a template or a schematic for creating objects. 15 00:00:47,139 --> 00:00:49,130 If you can think about blueprints in real life, 16 00:00:49,139 --> 00:00:51,380 blueprints are usually those big sheets of blue paper 17 00:00:51,389 --> 00:00:55,610 that specify how something like a house or a sky 18 00:00:56,169 --> 00:00:59,049 or a computer or a motorcycle will be built. 19 00:00:59,060 --> 00:01:00,409 They are the schematic, 20 00:01:00,419 --> 00:01:02,830 they kind of describe the details of something 21 00:01:02,840 --> 00:01:05,500 that will be created from that blueprint. 22 00:01:05,620 --> 00:01:09,120 So in technical terms in Python, the class represents the blueprint, 23 00:01:09,129 --> 00:01:11,589 it's the template from which objects are made. 24 00:01:11,889 --> 00:01:16,040 An instance is just an object that's created from a class. 25 00:01:16,050 --> 00:01:20,260 So in real life, multiple houses can be constructed from the same blueprint, right? 26 00:01:20,330 --> 00:01:25,110 So in this example, the house represents an instance of the blueprint, 27 00:01:25,120 --> 00:01:29,550 much like an object represents an instance of a class in Python, right? 28 00:01:29,559 --> 00:01:33,000 And of course, multiple objects can be constructed from the same class, 29 00:01:33,010 --> 00:01:35,879 much like multiple houses in the same neighborhood can be 30 00:01:36,099 --> 00:01:39,309 from the exact same schematic from the exact same blueprint. 31 00:01:39,500 --> 00:01:42,410 I'm very well aware that this is a complex topic. 32 00:01:42,419 --> 00:01:43,519 So we're going to dive into this in a 33 00:01:43,529 --> 00:01:45,910 lot greater detail as we progress throughout the course. 34 00:01:45,919 --> 00:01:48,849 For now, all I want you to memorize is just a sentence, 35 00:01:48,860 --> 00:01:51,470 an object is an instance of a class. 36 00:01:51,480 --> 00:01:54,300 And just keep in mind that a class represents a blueprint. 37 00:01:54,309 --> 00:01:56,809 We're going to continue exploring these ideas over and over again 38 00:01:56,819 --> 00:01:59,949 in more detail and they're gonna click eventually in the future. 39 00:02:00,209 --> 00:02:00,760 OK? 40 00:02:00,849 --> 00:02:02,379 So back to the type function, 41 00:02:02,389 --> 00:02:06,410 the type function returns the class of whatever argument you pass in. 42 00:02:06,550 --> 00:02:08,440 So whatever object you pass in, 43 00:02:08,449 --> 00:02:12,100 it's going to tell you what blueprint that object is constructed from 44 00:02:12,220 --> 00:02:15,110 it tells you what kind of object you're dealing with. 45 00:02:15,240 --> 00:02:18,539 So let's begin with a simple example. I'm going to put a print statement here 46 00:02:19,259 --> 00:02:20,979 and I'm going to invoke the type function, 47 00:02:20,990 --> 00:02:23,160 I'm going to invoke it right within the 48 00:02:23,169 --> 00:02:25,660 parenthesis that I provide to print and that's totally 49 00:02:25,669 --> 00:02:27,679 fine because we know whatever is inside the 50 00:02:27,690 --> 00:02:29,940 print parenthesis is going to be evaluated first. 51 00:02:29,949 --> 00:02:32,660 In this case, it's just another function evaluation. 52 00:02:32,779 --> 00:02:37,160 So in here, I'm just going to pass in a value of five that is a random integer, right. 53 00:02:37,270 --> 00:02:38,600 So when I execute this, 54 00:02:39,160 --> 00:02:42,429 we're going to see on the right. The syntax that says class int 55 00:02:42,570 --> 00:02:44,729 the important thing to recognize is all that this is 56 00:02:44,740 --> 00:02:47,570 saying is that this is made from a special blueprint. 57 00:02:47,580 --> 00:02:49,520 Again, class just means blueprint, 58 00:02:49,550 --> 00:02:52,940 a special blueprint that Python calls int which makes 59 00:02:52,949 --> 00:02:55,500 sense because five is an example of an integer. 60 00:02:55,509 --> 00:02:57,970 That's what I and T or int stands for here on the right. 61 00:02:57,979 --> 00:03:00,130 It means it's an integer class, 62 00:03:00,139 --> 00:03:04,255 it's a class or blueprint that's used to make integers almost like a factory. 63 00:03:04,386 --> 00:03:07,886 So if I find out the type of any other integer in Python, for example, 64 00:03:07,895 --> 00:03:10,235 here is the type of the number 10 65 00:03:10,406 --> 00:03:13,555 and I execute this, we're going to get the exact same result. 66 00:03:13,565 --> 00:03:16,966 We have two different objects with two different values five and 10. 67 00:03:17,005 --> 00:03:19,555 But they're both made from the same blueprint right? 68 00:03:19,565 --> 00:03:22,235 From the same kind of schematic or template. 69 00:03:22,246 --> 00:03:25,065 They are both examples of integers, right? 70 00:03:25,126 --> 00:03:27,501 So these are both objects for instances made from 71 00:03:27,511 --> 00:03:29,852 the class int which is short for engineers, 72 00:03:29,861 --> 00:03:34,891 they are two separate objects made from the same blueprint from the same class, 73 00:03:34,901 --> 00:03:37,212 from the int class and objects made 74 00:03:37,222 --> 00:03:40,121 from the same class share identical functionality. 75 00:03:40,141 --> 00:03:41,811 That's why we're able to, for example, 76 00:03:41,822 --> 00:03:45,651 add these two numbers to any other numbers because they have a similar design, 77 00:03:45,662 --> 00:03:49,162 they have a similar structure, they are made from the same template. 78 00:03:49,960 --> 00:03:53,279 What if we instead pass in a floating point number to type? 79 00:03:53,500 --> 00:03:55,080 So if I do print type 80 00:03:55,320 --> 00:04:00,389 and here I pass in something like 3.8 we're going to see that a object of a float. 81 00:04:00,399 --> 00:04:03,539 3.8 is actually constructed from a different class. 82 00:04:03,550 --> 00:04:06,220 And that class that blueprint is actually called float. 83 00:04:06,229 --> 00:04:07,559 We see that on the right here. 84 00:04:07,570 --> 00:04:11,460 So it is not the same class as the one that constructs an integer object. 85 00:04:11,490 --> 00:04:15,460 And of course, if I put in any other float in here, for example, 5.0 86 00:04:15,940 --> 00:04:18,100 we're going to see that the object that stores a value 87 00:04:18,108 --> 00:04:21,540 of 5.0 comes from the same class from the same blueprint. 88 00:04:21,548 --> 00:04:23,480 And that class is called float, right? 89 00:04:23,519 --> 00:04:25,010 So we have two different classes that we've 90 00:04:25,019 --> 00:04:27,579 introduced so far to blueprints int and float. 91 00:04:28,019 --> 00:04:30,910 Let's now pass in strings to the type function. 92 00:04:30,920 --> 00:04:33,119 I'm sure you can predict what result we're going to get, 93 00:04:33,170 --> 00:04:34,880 we're just going to get a different class. 94 00:04:34,890 --> 00:04:37,040 So if I do print and I want to find out 95 00:04:37,049 --> 00:04:40,500 the type of a random arbitrary string like let's say computer, 96 00:04:40,609 --> 00:04:43,730 I'm gonna find out that computer is an object, a string object 97 00:04:44,005 --> 00:04:48,725 that is made from or constructed from a class or a blueprint called string. 98 00:04:48,734 --> 00:04:52,894 In this case, STR is just obviously a shortened version, but it's short for string. 99 00:04:53,054 --> 00:04:54,954 And if I put any other string in there, 100 00:04:54,964 --> 00:04:58,755 if I put type and then literally put any string in the world here, for example, 101 00:04:58,765 --> 00:04:59,454 laptop, 102 00:05:00,390 --> 00:05:03,089 we're also going to get a class of string because the strings, 103 00:05:03,100 --> 00:05:06,570 computer and laptop are made from the same blueprint, right? 104 00:05:06,579 --> 00:05:08,470 From the same class. 105 00:05:08,570 --> 00:05:10,140 So in the previous lessons, 106 00:05:10,149 --> 00:05:13,149 we introduced the equality operator which we can use to 107 00:05:13,160 --> 00:05:16,559 compare the equality of two objects or two values. 108 00:05:16,649 --> 00:05:20,670 Well, we can also use the operator to compare two classes, right? 109 00:05:20,679 --> 00:05:24,290 Because classes also represent objects, they are just blueprints. 110 00:05:24,299 --> 00:05:25,480 So for example, 111 00:05:25,489 --> 00:05:28,269 I'm just going to write out the syntax and then it'll make sense I 112 00:05:28,279 --> 00:05:32,790 can say is the type of five equal to the type of 10. 113 00:05:33,070 --> 00:05:36,989 Notice that I'm not asking for the equality of the numbers five and 10. 114 00:05:37,119 --> 00:05:40,750 I'm asking for the equality of the blueprints that create 115 00:05:40,899 --> 00:05:43,390 an object of five and an object of 10. 116 00:05:43,540 --> 00:05:47,700 The blueprint that is used to create an object of five and an object of 10 is the same. 117 00:05:47,709 --> 00:05:50,190 It is the class called integer. So 118 00:05:50,290 --> 00:05:53,609 when I execute this, we're going to get back a true because behind the scenes, 119 00:05:53,619 --> 00:05:56,239 Python is simply comparing this thing class int with 120 00:05:56,250 --> 00:05:58,420 class int I'm gonna get back the exact same 121 00:05:59,059 --> 00:06:01,410 if I do something like type of computer 122 00:06:01,570 --> 00:06:04,089 is equal equal to type laptop. 123 00:06:04,100 --> 00:06:07,670 Remember it is two equal signs for the equality operator not one. 124 00:06:07,859 --> 00:06:09,690 So is the string computer and the 125 00:06:09,700 --> 00:06:11,910 string laptop constructed from the same blueprint. 126 00:06:11,920 --> 00:06:13,049 Yes, that is true. 127 00:06:13,059 --> 00:06:16,950 That comes from the string blueprint which we see output right here. 128 00:06:17,100 --> 00:06:20,040 What happens if I compare the types of two different objects? 129 00:06:20,049 --> 00:06:23,709 For example, the type of five is that equal to 130 00:06:24,279 --> 00:06:26,010 the type of five point? Oh 131 00:06:26,299 --> 00:06:26,660 Well, 132 00:06:26,670 --> 00:06:29,299 we saw in the previous lesson that when we compared the equality 133 00:06:29,309 --> 00:06:32,730 of five versus 5.0 we found out that they were considered equal. 134 00:06:32,739 --> 00:06:35,390 But this time around we're not comparing the two values. 135 00:06:35,399 --> 00:06:39,299 We're comparing the blueprints or the classes from which those values are made. 136 00:06:39,529 --> 00:06:44,940 So the type of five is a class of integer and the type of 5.0 is a class of float. 137 00:06:44,950 --> 00:06:47,475 Those are two different classes that are not equal. 138 00:06:47,484 --> 00:06:51,734 So we're going to get back false because those two are not equal. Same deal applies. 139 00:06:51,744 --> 00:06:55,274 If I compare, for example, the type of a number five to 140 00:06:55,515 --> 00:06:59,554 the type of a string like five. Again, we're comparing two different classes here. 141 00:06:59,565 --> 00:07:01,054 We're not comparing a number to a string. 142 00:07:01,065 --> 00:07:03,744 We're just comparing two different classes and an int 143 00:07:03,755 --> 00:07:05,994 class is not the same as a string class. 144 00:07:06,005 --> 00:07:07,714 So we're going to get back false. 145 00:07:08,109 --> 00:07:12,049 Here's another example of type in action I almost skipped over this one. 146 00:07:12,059 --> 00:07:14,500 What about our favorite friend? The boolean? 147 00:07:14,510 --> 00:07:19,109 Well, I can also pass in a boolean into my type function like true. 148 00:07:19,130 --> 00:07:22,130 In fact, you can pass in any valid Python object into type. 149 00:07:22,140 --> 00:07:25,609 It's just going to tell you what class that object is made from. So true. 150 00:07:25,619 --> 00:07:27,450 Of course, comes from a class called 151 00:07:27,649 --> 00:07:30,040 bool, a blueprint called bool, which of course stands for boolean. 152 00:07:30,619 --> 00:07:31,209 And 153 00:07:31,390 --> 00:07:32,769 if I find I have to type 154 00:07:32,876 --> 00:07:33,526 of false, 155 00:07:33,536 --> 00:07:36,955 we're going to find out that it is of course also made from the boolean class. 156 00:07:36,976 --> 00:07:40,536 And of course, if I find out is the class that makes a true, 157 00:07:40,825 --> 00:07:45,585 the same class that makes a false object. The answer is of course true as well, right? 158 00:07:45,596 --> 00:07:47,906 Same class, same boolean class. 159 00:07:47,915 --> 00:07:48,205 Now, 160 00:07:48,216 --> 00:07:49,825 I'm also going to give you a few examples 161 00:07:49,835 --> 00:07:51,876 with some objects that we haven't introduced yet. 162 00:07:51,885 --> 00:07:52,496 In the course, 163 00:07:52,506 --> 00:07:54,585 you don't have to worry about the syntax and you 164 00:07:54,596 --> 00:07:56,455 don't have to worry about what these objects do. 165 00:07:56,466 --> 00:07:57,596 This is just to demonstrate that 166 00:07:57,861 --> 00:08:01,992 that the type function is dynamic enough to be able to accept any input, 167 00:08:02,002 --> 00:08:04,302 any argument, any object that you want to give it. 168 00:08:04,312 --> 00:08:07,782 And it's going to tell you where the object comes from, what kind of type it has. 169 00:08:07,902 --> 00:08:10,402 So for example, here is a type we haven't seen before. 170 00:08:10,411 --> 00:08:12,772 It's a list again, don't worry about the syntax, 171 00:08:12,782 --> 00:08:15,071 but we're going to see that this type of data structure, 172 00:08:15,082 --> 00:08:17,772 this type of object is made from the class list. 173 00:08:17,782 --> 00:08:19,981 Here is another example with another type we'll talk 174 00:08:19,992 --> 00:08:22,361 about later in the course called a dictionary. 175 00:08:22,721 --> 00:08:24,372 Again, don't worry about the syntax. 176 00:08:24,940 --> 00:08:28,959 But the point here is when I execute this, we're going to see this one is oops 177 00:08:29,459 --> 00:08:31,959 made from the class called dictionary, right? 178 00:08:32,099 --> 00:08:34,409 Two different objects that we'll talk about a little bit later. 179 00:08:34,429 --> 00:08:36,909 But the point is type accepts whatever you give it. 180 00:08:36,919 --> 00:08:40,349 And it's going to tell you the class that it's built from the template or the 181 00:08:40,359 --> 00:08:43,200 schematic or the factory or the designation or 182 00:08:43,210 --> 00:08:46,859 the specification from which those objects are created. 183 00:08:46,869 --> 00:08:47,440 And then of course, 184 00:08:47,450 --> 00:08:50,599 we can always use the equality operator to compare any two objects 185 00:08:50,609 --> 00:08:54,159 or even any two classes from which those objects are made. 186 00:08:54,400 --> 00:08:57,679 That's all there's to cover in this lesson. So I will see you in the next one.