In this video, you learn to use Python methods.
We're going to need to use a method to add to do this to a list, because currently in our program,
all we do is get the two due from the user and then we print out that to do and that's all.
We are not storing the to dos in a list.
So I will delete these and add some code here which stores to dos in a list.
You remember we did have some codes previously where we wrote a list.
We created a list using brackets and then inside we add it to the one to do two and so on.
So now, of course, we don't have to do once and to the tools, we don't have those variables.
We have a to do variable.
So to do is one variable, to dos is another variable.
Two different variables to do now is added to the to do list.
So let's print out to dos in here and see what we get.
Run enter to do.
Clean through.
So this is the output we get.
Let's try to analyze this to understand the problem.
So while true executes this line first, so we get that output.
The user enters clean and clean, goes inside the list.
So to dos list is created and then it is printed out.
So we print out to DOS here.
So currently we have only the clean string inside that list.
So far, so good.
Then the loop goes back again to the first line of the loop.
It executes again inputs.
So we get this prompt again, we enter through and then.
The second line is executed.
To do so is equal to that list.
What happens here is that this variable will override the existing to those variable.
So the list we had with clean inside is overwritten by another list with through inside.
So this entire line here overrides that existing list.
And so we print out that new list with through inside.
So obviously this here is not the solution.
We want to delete that.
What we want to do is first create a list outside the loop.
To do is equal to this empty list, so Python will execute the first line.
It creates this string, it creates an empty list and then goes to the loop.
It asks the user for input.
The user enters a to do, and then we append that to do in the to do list in that list.
And we print out the to do list.
So let's rerun the code answer to do clean through neat.
So that's the list.
As you see, the list is being populated with items in every iteration of the loop.
So the program is working pretty well.
We want to improve it further, of course, and we'll do that in the next videos.
But now let's focus on the syntax we used here.
You see this dot and then this.
Key word is coming after that dot.
This is a method.
So methods are like functions.
You see, this function has this parentheses and an argument, and the method also has parentheses and
arguments, but methods are attached to data types to other objects.
In this case, this append method is part of this to dos.
Variable.
Actually, it is not part of the variable.
It is part of the object associated with that variable.
In this case, the object is a list.
You see, it's a list type and every object has its own methods.
So for example.
If you want to access the append method of the to to to do this variable is associated with a string,
not a list.
So if you try to apply append to that string, you're going to get an error.
Let's try this.
We run.
Enter it to do so.
Now we are here at this point.
So Python has not yet reached the point where this append line is executed.
So enter it to do like a clean press enter and you see that is an error.
It tells you where the error occurred.
So line seven, Line seven.
Is this one in here?
In part charm.
You can also just click that to go directly to line seven.
So that's a convenience.
And so the cursor is in line seven now.
You also get the line printed out in here, which has the error to do that the pants.
And then this is a type of the error attribute error.
HDR object.
So SDR is the type of object, the data type that to do is associated with.
So to do is associated with an SDR with a string object.
So an object has no attribute append.
So methods are also referred to as attributes.
So a string does not have that append method.
That's why we get this error.
So how do we fix this error?
Well, this is based on our experience as a programmer.
The error cannot be fixed automatically by Python, so you have to take a decision because Python here,
the interpreter which interprets our code is confused.
So it doesn't make sense to append something to a string.
It does make sense to append something to a list, but not to a string.
A string could have different methods.
Such as.
Capitalize.
Let's see what will happen now.
Enter to do clean all use a lowercase string.
Now press enter.
So clean was printed out by this, but we didn't get any print out from this.
So let me remove to reduce print to do so and execute again.
You see, again, we will not get any print out.
Why?
Well, because we haven't used a print function.
So if you want to see output, you won't use a print function as you see.
Now, I put this entire expression inside the parentheses of the print function.
Now, if I execute this enter to do clean, we will get clean with an uppercase C.
So this method, it's capitalized is the first letter of a string and it returns the string with the
first letter capitalized.
So as you see, the difference between methods and function is that in functions, if this was a function
which is not, it would get that to do as an input in here.
It would be capitalized to do inside.
But this is not the case and that is what makes a method a method.
So a method to refer to an object using that dot notation and methods can also take arguments.
So how do you know what arguments can a method take and how do you know what methods you can apply to
strings and what methods you can apply to lists?
How do you know what arguments you can pass to a function?
So these are all questions which I'll answer.
I'll give you the technique to answer all these questions by yourself so you can find the code you need
by yourself.
You'll see the list of methods you can apply to list, and you'll see all the methods available for
list.
You'll see all the methods that Python has available for string and so on.
And I'll show you all that information in the video programming tool or concepts of today.
So there is where you learn how to find the code you need.
And here we end this video.
So thanks a lot.
See you.