1 00:00:07,880 --> 00:00:11,175 Welcome back. So, suppose that we have 2 00:00:11,175 --> 00:00:14,370 some data and we want to use classes to represent that data. 3 00:00:14,370 --> 00:00:16,560 So, that data might come from a file, 4 00:00:16,560 --> 00:00:18,495 it might come from an internet source 5 00:00:18,495 --> 00:00:21,180 or in the case that I'm going to go through right now, 6 00:00:21,180 --> 00:00:24,315 it might come from something that we just hand coded. 7 00:00:24,315 --> 00:00:29,220 So in this case, I have a list of city names populations and state. 8 00:00:29,220 --> 00:00:33,270 So, I have city named Detroit with population 9 00:00:33,270 --> 00:00:44,340 680,250 in state Michigan and I have an Ann Arbor population 117,070, 10 00:00:44,340 --> 00:00:46,770 state Michigan et cetera. 11 00:00:46,770 --> 00:00:49,370 So, Pittsburgh this is the population. 12 00:00:49,370 --> 00:00:51,935 State is PA, Mars, 13 00:00:51,935 --> 00:00:54,290 small population in Pennsylvania, 14 00:00:54,290 --> 00:00:57,365 New York large population in New York. 15 00:00:57,365 --> 00:01:00,095 So, let's suppose that I have this data 16 00:01:00,095 --> 00:01:03,970 and let's suppose that I also zip these data together. 17 00:01:03,970 --> 00:01:07,970 So, zip is going to take these separate lists and create a list of tuples. 18 00:01:07,970 --> 00:01:12,095 So, if I print out what the value of city tuples. 19 00:01:12,095 --> 00:01:15,815 I'm going to see that I have a list of tuples where 20 00:01:15,815 --> 00:01:21,675 every city name is coupled with its population which is coupled with the state. 21 00:01:21,675 --> 00:01:26,045 So, suppose that I want to use classes to represent this data. 22 00:01:26,045 --> 00:01:28,340 The person that I would ask is, 23 00:01:28,340 --> 00:01:32,255 what data do I want to be able to put into every instance? 24 00:01:32,255 --> 00:01:34,925 So what should every instance have? 25 00:01:34,925 --> 00:01:43,110 So, what should instances store? 26 00:01:43,110 --> 00:01:47,045 So, in this case let's suppose that I care about all three of these things. 27 00:01:47,045 --> 00:01:49,940 So let's suppose that I care about the city name, 28 00:01:49,940 --> 00:01:51,830 the population and the state. 29 00:01:51,830 --> 00:01:59,460 So I want every instance to store the city name, population and state. 30 00:01:59,600 --> 00:02:05,135 Then I would ask, what do I want every class to you represent? 31 00:02:05,135 --> 00:02:08,465 In this case it's a little bit obvious every instance should represent 32 00:02:08,465 --> 00:02:12,800 an individual city and every individual city it's going to have a name, 33 00:02:12,800 --> 00:02:17,530 population and state, and I'm going to call my class city. 34 00:02:17,530 --> 00:02:20,115 So, let's define our city class. 35 00:02:20,115 --> 00:02:22,320 I'll do that by saying class, 36 00:02:22,320 --> 00:02:28,720 capital C City and in the constructor, 37 00:02:30,440 --> 00:02:33,525 I'm going to accept a name, 38 00:02:33,525 --> 00:02:37,355 population, state and I'm going to set instance variables. 39 00:02:37,355 --> 00:02:42,445 So self.name equals M, Self.population equals P, 40 00:02:42,445 --> 00:02:49,415 self.state equals S. Let's suppose that I want to have a str method, 41 00:02:49,415 --> 00:02:52,295 so def underscore str, 42 00:02:52,295 --> 00:02:59,040 and I want it to return the city name, 43 00:02:59,680 --> 00:03:05,170 state and then population. 44 00:03:05,170 --> 00:03:12,640 So, I'll do this.format, self.name, self.state, self.pop. 45 00:03:15,770 --> 00:03:19,370 Okay, so now that I have this list of 46 00:03:19,370 --> 00:03:23,170 city tuples and I have a class to represent every individual city, 47 00:03:23,170 --> 00:03:27,170 I want to take this list of city tuples and create a list of cities. 48 00:03:27,170 --> 00:03:30,370 So, let's suppose that I want to have a list of cities. 49 00:03:30,370 --> 00:03:31,830 So, I'll say cities. 50 00:03:31,830 --> 00:03:36,920 I'm going to start it out as an empty list and then I'm going to loop over every tuple. 51 00:03:36,920 --> 00:03:44,125 So, I'll say for city tuple in city tuples. 52 00:03:44,125 --> 00:03:47,475 For now I'll just print out what's the value of 53 00:03:47,475 --> 00:03:53,345 city tuple and I can see that I get every tuple on a separate line. 54 00:03:53,345 --> 00:03:56,435 If I wanted to, I could set name, 55 00:03:56,435 --> 00:04:00,900 population, state equal to city tuple. 56 00:04:00,900 --> 00:04:06,075 If by print these out name, population and state. 57 00:04:06,075 --> 00:04:08,395 I'll see I've the city name, 58 00:04:08,395 --> 00:04:11,720 population and state in different variables. 59 00:04:11,720 --> 00:04:15,615 If I want to create a new city with that name population and state. 60 00:04:15,615 --> 00:04:22,830 I'll say city equals a new city with name population and state, 61 00:04:22,970 --> 00:04:29,770 and now City is an instance of the city class. 62 00:04:29,770 --> 00:04:33,360 So if I print out what the value of city. 63 00:04:35,960 --> 00:04:38,150 Oops! 64 00:04:38,150 --> 00:04:47,340 I also need to do spell out population in my str method. 65 00:04:49,270 --> 00:04:53,660 Okay, so now, I'll see that I am 66 00:04:53,660 --> 00:04:58,305 actually calling this def underscore str method on my list of cities. 67 00:04:58,305 --> 00:05:01,420 If I wanted to add that city to my list of cities, 68 00:05:01,420 --> 00:05:09,275 I could say cities.append city and by the end of this for loop cities, 69 00:05:09,275 --> 00:05:15,330 it's going to be a list of city instances so I can say print cities. 70 00:05:17,380 --> 00:05:21,860 You can see I have a list of city instances. 71 00:05:21,860 --> 00:05:24,815 So, some shorter ways of doing this, 72 00:05:24,815 --> 00:05:27,980 one thing that I could do is I could use a list comprehension. 73 00:05:27,980 --> 00:05:31,470 So, rather than having all of these lines, 74 00:05:31,690 --> 00:05:38,825 I could use a list comprehension to say that cities equals a new city with name, 75 00:05:38,825 --> 00:05:42,395 population and state for every name, 76 00:05:42,395 --> 00:05:48,130 population, and state in city tuples. 77 00:05:49,460 --> 00:05:53,715 Now if I do this and actually print out cities, 78 00:05:53,715 --> 00:05:57,740 then I'll see that I get a list of city instances as well. 79 00:05:57,740 --> 00:06:04,620 One really short way of doing this is actually if I do T. I can 80 00:06:04,620 --> 00:06:08,090 actually add this star T. What star does is 81 00:06:08,090 --> 00:06:11,600 it takes this tuple and expands it into a list of arguments. 82 00:06:11,600 --> 00:06:14,450 So, this constructs the same thing. 83 00:06:14,450 --> 00:06:17,510 I'm going to prefer however to instead 84 00:06:17,510 --> 00:06:21,425 explicitly get out the name, population, and state. 85 00:06:21,425 --> 00:06:24,990 That's all for now until next time.