In Python, you are going to see two primary ways in which
you can change data.
One is called a function, and one is called a method.
Now, in Python, most things are considered an object, saying
that as an 'Object'.
Now, for now, and for a little while, this is going to be
a little bit of I don't really know how else to explain it,
but it's going to be a useless word to you while you're learning
about them, because an object in English, at least, is just a thing.
It's any physical thing, really.
And so if we did something like 'name = "Zephyr'",
that's one of my cats,
This name,
if we did type 'name', is a string.
But let's go ahead and put this into our Python interpreter
from our command line.
So I'll just type 'python', get in here,
and let's say 'name =', no let's do the other cat,
"Henry". Then I do 'type(name)',
we can actually see that it's a class.
It's a string, just like what we saw here,
but it's a class.
And with a class comes this thing called a method.
Now a method is a lot like a function,
just the way that you call it is a little bit different.
So a function looks like 'print'.
We put stuff in here.
That's a function because it has a function name, then parentheses,
and then it takes our arguments in here.
So in this case we could say stuff in here.
We could put a comma, we could put another argument in here, another
parameter, more stuff in here, and we could run this, and
it runs both of them together.
And that's because it's one function that takes one or more
arguments. Occasionally
these are also called parameters.
We've also used 'type', which we already used in this lesson as well.
And you put in some sort of variable name, and it gives you
exactly what that function is.
And I think I just said function, but what I meant was the
variable, it tells you what the variable is.
So you say 'type(name)', get 'str'.
You say 'print("Stuff in here", "More stuff in here")', and it's
going to say, "Stuff in hear More stuff in here".
But because 'name' is a class, and we can see that in our proper
Python interactive shell
in here, it's a class.
Now, classes have these kinds of functions as well, and behind
the scenes they look almost identical to writing a normal
function. However, the way we call them is different.
So 'name' we know is a string. It's a class.
If I hit '.', and then tab, you can see all the different
things that I can do with it.
I can check to see if it's a digit,
is it a decimal,
is it lower?
So this is just checking a bunch of stuff in here, but we
can also, we can split it multiple lines.
There's a lot of things we can do.
Let's do 'upper'.
And all this is going to do is take 'Z-e-p-h-y-r', and turn it all
into uppercase.
And that's it.
So the difference here, is with a function, you're giving
it an explicit parameter or multiple parameters.
With an object, our 'name' is being an object because it's a class,
think of classes as objects,
it already knows what that 'name' is.
That name is 'Zephyr'.
And so in order to make that upper case, you can say 'name',
which Python then points to "Zephyr", make it uppercase,
and that's exactly what it does.
So this one is called a method.
When it's attached to an object, when a particular function
is attached or pre-attached to any sort of object, like a
string, like our 'name' here,
then we call it a method, and it gets called with '.', and
then the method name, and then parentheses.
Now in the title here it's called 'String Properties and Methods'.
We now know what a method is.
Let's go ahead and take a look at a property.
A property.
So a property is a lot like a regular variable, like what
we defined up here, but it exists inside of an object.
For example, 'name' is "Zephyr".
How many letters is that?
Well, it's easy enough for us to count, there's six letters in there.
So if we said, "Hey, Python, tell us how many letters there are?"
It should return just the number 6.
And we can find that out by doing 'len', short for length, and
putting the 'name' in here.
Now we actually had to call a function, not a method, to
get this, because somewhere inside of this object, inside
of this 'name' called "Zephyr", Python said, "I know that there's
a Z-e-p-h-y-r, and that there are, in fact, 6 characters in there".
And it keeps track of that for us.
And so that is what a property is.
It's just a variable inside of it.
So you can sort of think about it this way, and maybe I can
do this in Markdown, is we have this variable called
'name = "Zephyr"'.
And then behind the scenes we have things like the total
number of letters is equal to 6.
And then we also have functions behind here, such as 'upper',
and that's just going to take whatever this value is here,
and it's going to make it all uppercase for us.
So it'll say, "ZEPHYR", something like that, and so you can sort
of think about it this way, is "Zephyr' has 'total_letters'. Behind the scenes
Python keeps track of it, and it's not just with length or
upper, there's a lot of other ones in there as well.
So when it comes to objects, really, you can type in any
sort of variable name, hit '.', and then tab, and you can
see what else is in here.
So let's say, does it end,
'endswith', let's say, does it 'endswith("r")'.
It does. But does it 'endswith("z")'?
It does not.
So it returns a Boolean.
So that's a method. Because it's a function, is running some
sort of logic. 'name.count'.
Here's a good one.
We can count the number of 'P' in here.
And remember when we were talking about sequences and strings,
how behind the scenes Python says, "Okay, I know you have
a sentence in here with Zephyr", and it's going to log all
of those individually, sort of behind the scenes.
It's exactly what it's doing here.
So it says 'name', run the method, the function on this 'name'
called 'count'.
Look for the number of 'p'.
And so when I hit 'Enter' on this, or 'Shift + Enter' in Jupyter
Notebook, this is going to say 1, just like that.
Now, if you ever want to test any of these out, by all means,
open up this notebook, hit or type, 'name.', hit tab,
snd then you can just explore any of these that you want.
Some of these are not going to be as intuitive as others.
And a lot of these we're going to cover sort of throughout
the rest of this course.
A good one here would be 'lower'.
Okay, it's a lot like 'upper', but just turns everything lower.
So that 'Z' is going to be a lower case 'z', little "zephyr".
So you can go ahead and test some of those out.
If you ever get stuck on one of them, you can always just
throw it into Google as well.
You can be like, Oh, Python 'count', method, Python 'find'
method, Python 'isascii' method, and it'll bring you to the
Python Docs, or a StackOverflow question, or a nice blog
post that will sort of help you understand.
There's a lot in here, we can't possibly go through every
single one, but there's quite a few in here that you're going to
end up using quite often.
So ones thought I tend to use as a web developer,
'count', 'endswith', 'find', 'format', 'join', 'lower', 'replace' or 'rsplit'
regular 'split', 'startswith', strip.
'rstrip', was in there too, right? Yup.
'rstrip', 'title' and 'upper'.
Those are the ones I usually use on a string
anyways. Just for funsies
let's see what 'swapcase' does.
Well, look at that.
It swapped the cases.
So that's kind of something fun that we can do.
Now, the big thing here, the big takeaway is really the difference
between functions and methods.
And just as a quick little recap, a function looks like
the function name, then it has parentheses,
and then you put some sort of parameter inside of it, like
'type' or 'print'. And a method looks like '.',
and then the function name or the method name, and then parentheses,
and it can take a parameter, might not take a parameter.
And a property is really just a variable inside of an object
like name.
A good example of that is 'len(name)'.
And Python said there were six characters in there. So somewhere
inside of Python it says, "Hey 'name' has an attribute, that is an
attribute, has a property, has an internal variable called
length, and that value is 6.