Let's continue talking about methods and how they differ from functions.
So unlike functions, methods are associated with a certain type.
For example, strings have an upper method which will capitalize all the letters of strings.
Strings also have a capitalized method which capitalizes the first letter.
They have a title method which capitalizes the first letters of each word in the string.
And of course, methods can also be applied to the variable which is holding the object.
So in here we apply the method directly on the object itself, and here we apply it to the variable
which holds the object.
Either way, it's fine.
But normally you work with variables when building applications, so most commonly you'll do this year.
Now there are methods which return an output and methods which don't return an output.
And this is a concept similar to functions functions which return outputs and functions which don't
return an output.
This method here title is applied to the greeting variable which holds the string.
Hello there.
Now, when you execute this code in a python console, you will get this.
Hello there with h and PT capitalized.
So this means that.
This method is returning that string.
Or you can also have this.
Stored in a variable world is equal to a title and then print out the words and you'll get the same
thing.
Hello there.
So the output of title is stored in this variable.
In other methods, though, such as we have this append method which applies to the grocery list, so
this method does not return an output.
So if you assign a variable here, such as variable.
Equal to that variable will be equal to nothing because no value is returned from the append method.
However, the append method.
Modifies its object.
A method modifies its objects when the object is mutable.
So lists.
All lists are mutable.
Strings are not mutable.
So if I print out groceries here, you'll see that apples have been has been added to groceries.
So groceries was a list of three items up here.
But then here it became a list of four items.
Similarly, we can apply a sort method to groceries, so this sort method will not return an output,
but it will modify its object.
So this is not the same as the title method tied to the returns and output, but it does not modify
its object because the object here is a string and strings are immutable.
They cannot be modified.
If you really want to modify a string, you'd have to store the new outputs in new variable.
For example, creating new is equal to that, and then creating new will be the new string with H and
T capitalized.
But it's a new object, right?
You are not modifying the existing object which is currently connected to the greeting variable.
Right.
That's about methods which apply to mutable objects and methods which apply to immutable objects.
To get a list of methods for a particular object type, you can use the function.
For example, for strings, you can do SDR and you'll see that, for example, you have the lower methods
here.
The title method which we used, also the upper method, etc..
Alternatively, you can also apply deer to an instance of strings.
So hello is an instance right?
SDR is the class which represents all strings.
So a class is like, for example, let's say here we could say human.
And so the human is the class, right?
But then if you refer to a particular human which has a name and he lives somewhere or she lives somewhere.
He has an identity and so on.
That is an instance of human class.
So the person, one particular person is one particular instance and the abstract concept human is the
class.
So the same goes here as the R is an abstract concept and hello is one instance of that concept.
Of course, you can apply deer to other object types such as list.
This could be int as well.
Float, etc..
How to create new methods.
So for function, for example, we have functions which we access from Python, for example, the print
function of the input function.
But we can also create custom functions using the dev keywords.
Now, with methods we have these methods for example operand and title and append.
But how do you create new methods?
Well, since methods are connected to their object types, then first you need to know how to create
object types, right?
How to create a human blueprint.
For example, how to create string blueprints.
That is something you haven't learned yet.
But you learn that in the second half of the course, that's an advanced concept that the ultimate level
of python you can get.
So once you know how to create these types object types, then you also learn how to create methods
for these types.
So perhaps you can create an app, a booking system for airplane tickets, and you'd have like a ticket
class, A ticket type.
We should create ticket instances for customers, and then this ticket class could have a cancel method
which cancel the ticket or a delete method which deletes the PDF ticket or it deletes the ticket from
the database and so on.
Lists and tables.
So that's a list.
That's a table.
What's the difference?
Well, temples are immutable.
So, for example, you cannot have an append method for this value staple.
You'll get an error if you try to execute that because the append method is meant to modify.
The object it applies to.
But tablets are immutable, so this will not work.
Usually lists are used when you want to store homogeneous items.
For example, this list contains strings only.
Even though you can store various types, but usually you want to use lists for homogeneous items and
you also want to use lists when you foresee that during the course of the program.
In the program.
During the execution of the list may be changed.
New items can be added to the list and so on.
That is something that you will better understand when you build real applications throughout the course
and tuples.
Are more meant to store heterogeneous items, for example.
Let's say you have a web app where the users can upload an image and they want it resized and they want
it grayscale.
So they may input some numbers in input boxes in the web app interface.
And so in the backend in Python, you will get those numbers in a tuple, you might get the grayscale
string.
So the user is saying that I wanted grayscale and I wanted a jpg.
So you may store such data in a tuple.
Right indexing.
You can use indexing on strings, lists and tables.
For example, Grocery Zero will give you the first item off the grocery list.
Groceries, too, will give you the third item.
So 012.
That's at the third item.
The same goes for tuples, so that will give you great scale.
The third item for strings as well.
So.
And is the third item with index two.
And we also have negative indexing.
For example, minus two will give you a this because our is minus one, A is minus two, minus three,
minus four minus five and so on.
You might want to use negative indexing when you're accessing items which are closer to the end of an
array array, I mean at least to pull or string.
Also called sequence.
So these are also known as sequence types.
Of course, this is a string type.
This is a list type.
This is a type of type, but they fall in the category of sequences.
So that's how you access individual items, but you can also access a slice by using that column symbol.
So that will give you the items from index one up to index two.
So the item with index three is not included in the slides.
So you only get 1080, which is this.
So zero one index one and index to grayscale.
So that's the rule for indexing.
It works for strings and lists as well.
Then we have negative indexing.
Minus three, minus one means.
From here, which is minus three.
Up to here, which is before minus one.
So always the second index in the slides is not included in the slides.
So index three was not included here.
Index minus one JPG is also not included in this slides.
Lastly for this video, let's talk about dictionaries.
You should use dictionaries to store your data when your values are more heterogeneous and it's important
to have labels on them.
So the data here are, for example, John Smith for it.
And so these are heterogeneous because these are strings, right?
And this is a number and the labels are first name.
So this tells you that John is a first name.
Then we have last name.
So Smith is the last name.
And then we have age, which is 40.
So.
These are known as keys.
First name is a key.
Last name is a key.
And.
And age is also a key.
And the values of this dictionary are John.
Smith and Foley dictionaries can also be insight other items such as lists, for example.
Here, this is a list.
It starts in here and it ends in here.
You see the square brackets.
So this list has three items.
That's the first item.
That's the second item.
And there's the third item.
So each item is a dictionary because at least can have any type of object.
And dictionary is also an object, just like we have strings and integers and floats, etc..
So this structure here would be appropriate if you had multiple persons.
So you would store data about those persons and you'd have the same key in all the dictionaries but
different values, for example, John here Smith and the H, and you repeat that information but with
different values, right?
So that's one way to store these data, data about persons.
However, in certain applications, depending on the scenario, you may want to store those data differently.
For example, here we also have data about persons, but this will be a dictionary.
So it's one dictionary.
It starts here and it ends in here.
And then this dictionary has keys and values.
So the keys are the same.
They are just strings first name, but the value is a list.
So the value of each key now is a list instead of a string.
So this list here has the first names of all the persons.
And then we have another key, which has also the last names of the persons and the age.
Which one is better?
Well, there's no answer for that.
Whether this is better or that.
So it depends what you want to do with these data.
Do you simply want to print them out or do you want to modify them?
That is something that you realize as you build the application.
So nothing to worry about.
Now what is better and what is not?
And lastly, to access the values of a dictionary.
You use that syntax, so you refer to the variable which holds the dictionary.
John In this case.
And then in square brackets you refer to the key in quotes.
So that will give you Smith the value of the last name key of John Dictionary for this here, which
is a list you'd first have to do too.
So with that, you'd get this dictionary because this list has three items.
So that has an index of zero, that has an index of one and that has an index of two.
So with that, you get the last dictionary, and then from that dictionary you access the first name
key.
So that will give you some.
That's what we get here, SIM.
Since this is a dictionary, first you have to give a key.
So here you give a number because that was a list.
But here you have to give the key of the dictionary first name and then this up to here.
You will get the list as outputs and then out of that list you access index to.
So the item with index two, which corresponds to SIM.
So you get SIM as outputs.
That's above this video.
We have some more.
So you in the next one.