Thursday, July 4, 2013

Def and Arguments

     Hopefully you still have the list from the last article, because we're doing a bit more with it!
     Alright, we have a list of lists here named traits. What if we want an item inside an item of the list? Okay, lemme just explain what I said, if you find it a bit too confusing; We have 3 lists in a list. What if we want a single item from 1 of those lists? We know that we can get an entire list (because each list is an item in traits) like this;

>>> print(traits[0])

['Blue', 'No', 'Yes', 'No']

     But what if we want only one of those items? Well, we add a second index.

print(traits[0] [3])

No

     There you have it. That's the last thing about tables I have to talk about, so let's move on to the next thing:  def.

     Def basically skips a block of code. You use it before a variable, just like for and while loops. Write this:

>>> def randomwords():
print("Hi")
print("Pears")
print("CHEESE")


>>> randomwords()
Hi
Pears
CHEESE

     We'll get to why there are parenthesis after randomwords in a second. When we used def, it skipped the entire block of code and did nothing. Doing this allowed us to activate the block whenever we wanted, just by repeating the variable after def. Of course we didn't type the colon (:), because that's not part of the variable. It tells Python you are making a block of code.
     Alright, now why are the parenthesis there? Well, without the parenthesis, you wouldn't be able to call (or activate) the block of code, because Python doesn't understand you if you just typed "randomwords." Python wouldn't know whether it's a string or an integer or anything, so you get an error. With the parenthesis, Python knows you're calling for the block of code and activates it. Also, as always, we might put things in parenthesis.
     A good thing about the def function is that you can print it over and over as many times as you like, like a loop. However, instead of printing it several times at once, you can print it anywhere you want in the program. But, that's not what we're focusing on in this paragraph. That statement set aside... What can we put in the parenthesis? A little something I like to call (okay, everyone calls it this), arguments!
     Not arguments like two people disagreeing, computers never argue. In programming, argument means a piece of information you give to a function. If you don't want a def function to be the same every time you activate it, you put an argument in the parenthesis. Arguments are variables, so you can call them whatever you want.
     Arguments are tricky to understand unless you get an example with a program. I'll change the first print statement in randomwords to an argument named randomword. Here's my example:

>>> def randomwords(randomword):
print(randomword)
print("Pears")
print("CHEESE")


>>> randomwords("Pie")
Pie
Pears
CHEESE

     When you put a variable in the parenthesis, then somewhere in your block of code, you can change that variable to something every time you call it. Pretty neat, huh? That way you don't have to print the same thing (with something slightly changed, of course) over and over again.
     In this article, you learned about def and arguments. What programs can you make now with you knowledge? After we get through all the functions, objects, and modules, we will be moving onto making our own computer games! But for now, bye!
     Oh, I almost forgot, happy 4th of July!

No comments:

Post a Comment