Python: Introduction to Methods
Methods are basically actions, they act on objects. For example lets say I wanted to teach you python, I might type:
you.TeachPython()
In this case you would be the object, and TeachPython would be the action (the method).
Lets get onto some real code, to start off with lets make a list:
List = [22, 63, 723, 212]
What if we wanted to add something to the end of this list? Well we could use the "append" method.
What is basically does is add something onto the end of the list - but how does it know what to add onto the end of the list?
Well we have to give the method a number to add on, this number is called an argument or a parameter. To add 22 onto the end of our list we could use this:
List.append(22)
In the above case "List" is our object, and "append" is our method.
Another useful method is the "count" method, this counts how many times a certain phrase (or element) occurs in the list.
For example if we wanted to count the number of 22s in our list we could use:
List.count(22)
The above would ouput 2 as there are two 22s in List.
The last method I want to show you is extend. What is does is extend the function by another list.
We can write the list we want to extend onto the end by hand (something like [33, 72, 19]) or we can use another list variable (for example if we made a ListTwo list variable).
List.extend([33, 72, 19])
The above would correctly add "33", "72" and "19" to the end of List.
Back to Python

