1 00:00:00,000 --> 00:00:04,920 In Python, you are going to see two primary ways in which 2 00:00:04,930 --> 00:00:06,210 you can change data. 3 00:00:06,660 --> 00:00:09,240 One is called a function, and one is called a method. 4 00:00:09,600 --> 00:00:13,980 Now, in Python, most things are considered an object, saying 5 00:00:13,990 --> 00:00:14,940 that as an 'Object'. 6 00:00:15,240 --> 00:00:19,050 Now, for now, and for a little while, this is going to be 7 00:00:19,060 --> 00:00:22,420 a little bit of I don't really know how else to explain it, 8 00:00:22,450 --> 00:00:25,420 but it's going to be a useless word to you while you're learning 9 00:00:25,420 --> 00:00:31,000 about them, because an object in English, at least, is just a thing. 10 00:00:31,350 --> 00:00:33,390 It's any physical thing, really. 11 00:00:33,500 --> 00:00:36,700 And so if we did something like 'name = "Zephyr'", 12 00:00:36,700 --> 00:00:37,700 that's one of my cats, 13 00:00:38,800 --> 00:00:39,800 This name, 14 00:00:39,830 --> 00:00:46,120 if we did type 'name', is a string. 15 00:00:46,130 --> 00:00:51,750 But let's go ahead and put this into our Python interpreter 16 00:00:51,750 --> 00:00:52,700 from our command line. 17 00:00:52,700 --> 00:00:54,300 So I'll just type 'python', get in here, 18 00:00:54,300 --> 00:00:57,900 and let's say 'name =', no let's do the other cat, 19 00:00:57,900 --> 00:01:00,240 "Henry". Then I do 'type(name)', 20 00:01:00,620 --> 00:01:02,900 we can actually see that it's a class. 21 00:01:03,020 --> 00:01:05,290 It's a string, just like what we saw here, 22 00:01:06,879 --> 00:01:07,990 but it's a class. 23 00:01:08,350 --> 00:01:11,230 And with a class comes this thing called a method. 24 00:01:11,640 --> 00:01:14,100 Now a method is a lot like a function, 25 00:01:14,640 --> 00:01:17,730 just the way that you call it is a little bit different. 26 00:01:18,800 --> 00:01:22,100 So a function looks like 'print'. 27 00:01:22,140 --> 00:01:25,460 We put stuff in here. 28 00:01:26,020 --> 00:01:29,840 That's a function because it has a function name, then parentheses, 29 00:01:29,850 --> 00:01:31,640 and then it takes our arguments in here. 30 00:01:31,740 --> 00:01:33,870 So in this case we could say stuff in here. 31 00:01:33,880 --> 00:01:36,750 We could put a comma, we could put another argument in here, another 32 00:01:36,760 --> 00:01:40,550 parameter, more stuff in here, and we could run this, and 33 00:01:40,560 --> 00:01:42,050 it runs both of them together. 34 00:01:42,300 --> 00:01:45,720 And that's because it's one function that takes one or more 35 00:01:45,730 --> 00:01:47,460 arguments. Occasionally 36 00:01:47,470 --> 00:01:48,900 these are also called parameters. 37 00:01:48,900 --> 00:01:52,400 We've also used 'type', which we already used in this lesson as well. 38 00:01:52,450 --> 00:01:54,870 And you put in some sort of variable name, and it gives you 39 00:01:54,880 --> 00:01:56,250 exactly what that function is. 40 00:01:56,540 --> 00:01:59,210 And I think I just said function, but what I meant was the 41 00:01:59,220 --> 00:02:00,860 variable, it tells you what the variable is. 42 00:02:00,870 --> 00:02:03,050 So you say 'type(name)', get 'str'. 43 00:02:03,280 --> 00:02:06,520 You say 'print("Stuff in here", "More stuff in here")', and it's 44 00:02:06,530 --> 00:02:08,199 going to say, "Stuff in hear More stuff in here". 45 00:02:08,199 --> 00:02:13,120 But because 'name' is a class, and we can see that in our proper 46 00:02:13,130 --> 00:02:15,040 Python interactive shell 47 00:02:15,050 --> 00:02:16,240 in here, it's a class. 48 00:02:16,600 --> 00:02:20,800 Now, classes have these kinds of functions as well, and behind 49 00:02:20,800 --> 00:02:23,300 the scenes they look almost identical to writing a normal 50 00:02:23,380 --> 00:02:25,740 function. However, the way we call them is different. 51 00:02:25,900 --> 00:02:28,100 So 'name' we know is a string. It's a class. 52 00:02:28,420 --> 00:02:32,440 If I hit '.', and then tab, you can see all the different 53 00:02:32,450 --> 00:02:34,570 things that I can do with it. 54 00:02:34,570 --> 00:02:37,500 I can check to see if it's a digit, 55 00:02:37,500 --> 00:02:38,500 is it a decimal, 56 00:02:39,300 --> 00:02:40,300 is it lower? 57 00:02:40,330 --> 00:02:43,140 So this is just checking a bunch of stuff in here, but we 58 00:02:43,150 --> 00:02:45,560 can also, we can split it multiple lines. 59 00:02:45,570 --> 00:02:46,970 There's a lot of things we can do. 60 00:02:46,980 --> 00:02:47,960 Let's do 'upper'. 61 00:02:48,100 --> 00:02:54,900 And all this is going to do is take 'Z-e-p-h-y-r', and turn it all 62 00:02:54,900 --> 00:02:55,900 into uppercase. 63 00:02:55,900 --> 00:02:57,350 And that's it. 64 00:02:57,360 --> 00:03:01,790 So the difference here, is with a function, you're giving 65 00:03:01,800 --> 00:03:05,300 it an explicit parameter or multiple parameters. 66 00:03:05,500 --> 00:03:10,100 With an object, our 'name' is being an object because it's a class, 67 00:03:10,120 --> 00:03:11,850 think of classes as objects, 68 00:03:12,120 --> 00:03:14,370 it already knows what that 'name' is. 69 00:03:14,380 --> 00:03:15,780 That name is 'Zephyr'. 70 00:03:17,400 --> 00:03:20,200 And so in order to make that upper case, you can say 'name', 71 00:03:20,290 --> 00:03:24,670 which Python then points to "Zephyr", make it uppercase, 72 00:03:24,680 --> 00:03:25,720 and that's exactly what it does. 73 00:03:25,880 --> 00:03:28,310 So this one is called a method. 74 00:03:28,320 --> 00:03:31,640 When it's attached to an object, when a particular function 75 00:03:31,650 --> 00:03:36,600 is attached or pre-attached to any sort of object, like a 76 00:03:36,930 --> 00:03:38,770 string, like our 'name' here, 77 00:03:38,780 --> 00:03:42,100 then we call it a method, and it gets called with '.', and 78 00:03:42,110 --> 00:03:43,870 then the method name, and then parentheses. 79 00:03:44,000 --> 00:03:48,100 Now in the title here it's called 'String Properties and Methods'. 80 00:03:48,140 --> 00:03:49,700 We now know what a method is. 81 00:03:49,700 --> 00:03:51,900 Let's go ahead and take a look at a property. 82 00:03:51,960 --> 00:03:54,990 A property. 83 00:03:55,000 --> 00:03:57,930 So a property is a lot like a regular variable, like what 84 00:03:57,940 --> 00:04:01,050 we defined up here, but it exists inside of an object. 85 00:04:01,680 --> 00:04:07,140 For example, 'name' is "Zephyr". 86 00:04:07,150 --> 00:04:08,220 How many letters is that? 87 00:04:08,220 --> 00:04:11,600 Well, it's easy enough for us to count, there's six letters in there. 88 00:04:11,610 --> 00:04:14,760 So if we said, "Hey, Python, tell us how many letters there are?" 89 00:04:14,770 --> 00:04:16,890 It should return just the number 6. 90 00:04:16,899 --> 00:04:20,339 And we can find that out by doing 'len', short for length, and 91 00:04:20,350 --> 00:04:21,329 putting the 'name' in here. 92 00:04:22,290 --> 00:04:26,670 Now we actually had to call a function, not a method, to 93 00:04:26,880 --> 00:04:31,230 get this, because somewhere inside of this object, inside 94 00:04:31,240 --> 00:04:35,840 of this 'name' called "Zephyr", Python said, "I know that there's 95 00:04:35,850 --> 00:04:42,140 a Z-e-p-h-y-r, and that there are, in fact, 6 characters in there". 96 00:04:42,150 --> 00:04:43,850 And it keeps track of that for us. 97 00:04:44,080 --> 00:04:46,750 And so that is what a property is. 98 00:04:46,760 --> 00:04:48,610 It's just a variable inside of it. 99 00:04:48,820 --> 00:04:52,030 So you can sort of think about it this way, and maybe I can 100 00:04:52,030 --> 00:04:55,400 do this in Markdown, is we have this variable called 101 00:04:55,400 --> 00:04:58,300 'name = "Zephyr"'. 102 00:04:58,370 --> 00:05:03,830 And then behind the scenes we have things like the total 103 00:05:03,840 --> 00:05:05,810 number of letters is equal to 6. 104 00:05:05,820 --> 00:05:10,590 And then we also have functions behind here, such as 'upper', 105 00:05:11,430 --> 00:05:14,370 and that's just going to take whatever this value is here, 106 00:05:14,380 --> 00:05:16,650 and it's going to make it all uppercase for us. 107 00:05:16,800 --> 00:05:20,040 So it'll say, "ZEPHYR", something like that, and so you can sort 108 00:05:20,040 --> 00:05:24,300 of think about it this way, is "Zephyr' has 'total_letters'. Behind the scenes 109 00:05:24,390 --> 00:05:27,950 Python keeps track of it, and it's not just with length or 110 00:05:28,280 --> 00:05:31,190 upper, there's a lot of other ones in there as well. 111 00:05:31,220 --> 00:05:33,860 So when it comes to objects, really, you can type in any 112 00:05:33,870 --> 00:05:36,230 sort of variable name, hit '.', and then tab, and you can 113 00:05:36,240 --> 00:05:37,460 see what else is in here. 114 00:05:37,700 --> 00:05:39,500 So let's say, does it end, 115 00:05:39,500 --> 00:05:42,380 'endswith', let's say, does it 'endswith("r")'. 116 00:05:43,800 --> 00:05:48,100 It does. But does it 'endswith("z")'? 117 00:05:48,100 --> 00:05:49,400 It does not. 118 00:05:49,500 --> 00:05:50,800 So it returns a Boolean. 119 00:05:50,800 --> 00:05:53,640 So that's a method. Because it's a function, is running some 120 00:05:53,650 --> 00:05:55,650 sort of logic. 'name.count'. 121 00:05:55,650 --> 00:05:56,700 Here's a good one. 122 00:05:56,700 --> 00:05:59,200 We can count the number of 'P' in here. 123 00:05:59,280 --> 00:06:01,800 And remember when we were talking about sequences and strings, 124 00:06:02,020 --> 00:06:05,740 how behind the scenes Python says, "Okay, I know you have 125 00:06:05,740 --> 00:06:10,370 a sentence in here with Zephyr", and it's going to log all 126 00:06:10,380 --> 00:06:12,260 of those individually, sort of behind the scenes. 127 00:06:12,360 --> 00:06:14,370 It's exactly what it's doing here. 128 00:06:14,370 --> 00:06:19,560 So it says 'name', run the method, the function on this 'name' 129 00:06:19,860 --> 00:06:20,910 called 'count'. 130 00:06:21,240 --> 00:06:23,700 Look for the number of 'p'. 131 00:06:23,910 --> 00:06:26,850 And so when I hit 'Enter' on this, or 'Shift + Enter' in Jupyter 132 00:06:26,860 --> 00:06:30,030 Notebook, this is going to say 1, just like that. 133 00:06:30,040 --> 00:06:33,900 Now, if you ever want to test any of these out, by all means, 134 00:06:33,910 --> 00:06:38,520 open up this notebook, hit or type, 'name.', hit tab, 135 00:06:39,030 --> 00:06:42,420 snd then you can just explore any of these that you want. 136 00:06:42,430 --> 00:06:45,270 Some of these are not going to be as intuitive as others. 137 00:06:45,500 --> 00:06:47,930 And a lot of these we're going to cover sort of throughout 138 00:06:47,930 --> 00:06:48,900 the rest of this course. 139 00:06:48,900 --> 00:06:51,100 A good one here would be 'lower'. 140 00:06:51,100 --> 00:06:53,900 Okay, it's a lot like 'upper', but just turns everything lower. 141 00:06:54,100 --> 00:06:58,700 So that 'Z' is going to be a lower case 'z', little "zephyr". 142 00:06:58,770 --> 00:07:01,050 So you can go ahead and test some of those out. 143 00:07:01,060 --> 00:07:04,060 If you ever get stuck on one of them, you can always just 144 00:07:04,070 --> 00:07:05,350 throw it into Google as well. 145 00:07:05,360 --> 00:07:09,240 You can be like, Oh, Python 'count', method, Python 'find' 146 00:07:09,300 --> 00:07:14,380 method, Python 'isascii' method, and it'll bring you to the 147 00:07:14,390 --> 00:07:17,500 Python Docs, or a StackOverflow question, or a nice blog 148 00:07:17,500 --> 00:07:19,470 post that will sort of help you understand. 149 00:07:19,480 --> 00:07:22,020 There's a lot in here, we can't possibly go through every 150 00:07:22,020 --> 00:07:24,400 single one, but there's quite a few in here that you're going to 151 00:07:24,400 --> 00:07:25,700 end up using quite often. 152 00:07:25,790 --> 00:07:28,810 So ones thought I tend to use as a web developer, 153 00:07:29,000 --> 00:07:36,450 'count', 'endswith', 'find', 'format', 'join', 'lower', 'replace' or 'rsplit' 154 00:07:36,740 --> 00:07:39,830 regular 'split', 'startswith', strip. 155 00:07:40,100 --> 00:07:42,500 'rstrip', was in there too, right? Yup. 156 00:07:42,540 --> 00:07:45,050 'rstrip', 'title' and 'upper'. 157 00:07:45,050 --> 00:07:46,800 Those are the ones I usually use on a string 158 00:07:46,800 --> 00:07:48,700 anyways. Just for funsies 159 00:07:48,700 --> 00:07:50,200 let's see what 'swapcase' does. 160 00:07:50,600 --> 00:07:51,600 Well, look at that. 161 00:07:51,600 --> 00:07:52,520 It swapped the cases. 162 00:07:53,630 --> 00:07:55,970 So that's kind of something fun that we can do. 163 00:07:56,270 --> 00:07:59,660 Now, the big thing here, the big takeaway is really the difference 164 00:07:59,670 --> 00:08:00,980 between functions and methods. 165 00:08:01,280 --> 00:08:05,680 And just as a quick little recap, a function looks like 166 00:08:05,680 --> 00:08:08,300 the function name, then it has parentheses, 167 00:08:08,300 --> 00:08:11,100 and then you put some sort of parameter inside of it, like 168 00:08:11,160 --> 00:08:14,390 'type' or 'print'. And a method looks like '.', 169 00:08:14,480 --> 00:08:17,750 and then the function name or the method name, and then parentheses, 170 00:08:17,750 --> 00:08:20,270 and it can take a parameter, might not take a parameter. 171 00:08:20,820 --> 00:08:25,930 And a property is really just a variable inside of an object 172 00:08:25,940 --> 00:08:27,360 like name. 173 00:08:27,400 --> 00:08:30,600 A good example of that is 'len(name)'. 174 00:08:30,600 --> 00:08:34,320 And Python said there were six characters in there. So somewhere 175 00:08:34,380 --> 00:08:38,870 inside of Python it says, "Hey 'name' has an attribute, that is an 176 00:08:38,870 --> 00:08:43,100 attribute, has a property, has an internal variable called 177 00:08:43,100 --> 00:08:46,600 length, and that value is 6.