Imagine when every time after you flush the toilet, you have to manually fill the tank with water. Now that would suck. In Python, and almost all other languages, such processes can be automated. You can write a process that will refill the tank every time you flush. Every single time.
And it is the same idea for extracting links from a page. You do not only want the first link, you want all the links! There are two ways to retrieve all the links on a page:
The first way is to code it manually, search for the first occurrence of the
<a href=" tag, retrieve the string until the closing
", set the new starting position to search. Rinse and repeat. The problem is, how many times will you have the code this? It depends on the number of links in a page right? And every page will probably have different number of links! Its a mess.
The second way is by something known as procedures or functions. A procedure basically automates the entire process. You can also think of a procedure as an oven
When you feed some ingredients into this oven, you get a cake or a bread, depending on the ingredients, as well as the degree of heat and time and other factors of the oven. This oven will help you extract the links from the page. It will look at the page, extract the first link, and it will automatically start searching for the next link and extract it. You would not have to code for every single link in the page. That’s the magic of the oven.
In python, the way to
build an oven make a procedure is using the keyword
def (short for define):
<block># block of code stating how the oven will mix the ingredients and make a cake
# this will define a procedure named myMagicOven with the parameters flour, water and egg.
Remember during the lessons on strings you learned about parameters, which are basically inputs to your procedures. A procedure can have zero parameters or as many parameters as you would need, separated by a comma as seen above:
def myUselessOven(): # empty parentheses means that there are no parameters required
print "This oven does nothing, except print this useless statement"
Note that the block consists of instructions and expressions that you will code. Blocks are indented (four spaces by convention), and the indentations are important, because it will tell the Python interpreter where the block ends.
If you have baked before, I’m sure you know how difficult it is. First you’ll need the right ingredients, the correct type of flour, sugar, eggs; the correct quantity of each ingredient; the intense beating kneading to get it to mix; the fine control of temperature and heat. And finally you have a cake, or a loaf of bread, or cookies. It would be sad if the oven eats it all up and leaves you with nothing.
Python procedures don’t disappoint you, usually. You feed inputs into the procedures, and most of the time you can expect to get outputs from the procedures. These outputs are also known as returns, which I mentioned in Strings Part II.
goo = flour + water
dough = goo + egg
In this modified
myMagicOven procedure, you can see that it returns a cake (I hope you like cakes), and in the midst of returning it actually tasted goo and dough. A procedure can return multiple expressions (separated by commas).