Monday, February 24, 2014

Python Programming Tutorial #4

I finally found the time to make another tutorial...yaaaaay!
So, anyway, this time I'm gonna introduce to you another kind of "variable": lists.
Lists are like normal variables, except that they can store more that one value simultaneously. They are close to what is known -in other programming languages, like C- as arrays. Anyway, here's a simple definition: if variables are boxes, then lists are bookcases; they can hold many things.

Here are some simple examples of lists:
mylist = []
anotherlist = [1, 2, 3]
yetanotherlist = [True, "ff", "Hi, 2!", 4378]

Now you can store as many things as you wish in one variable. Cool, huh?
Imagine if you were making a game, then you could easily have the player's inventory in a list, rather than making many different variables (which are also more memory-expensive).

But, before continuing, let's see some other examples of lists:
x = 4
mylist = [x, 5, 15]
fancylist = [2, [3, 4], 5] # this is called a "2-dimensional list"...list-ception!

As you can see, you can put pretty much anything inside a list, including another list.
Hold on, though...this "list" thing is cool and all, but how can I access its elements? How can I add new ones, or delete the ones I don't need?

Well, that's quite simple! Let me show you:
mylist = [1, 2, 3]
print mylist
print mylist[0]
print mylist[2]

And here's my output: (the $ thing is just my command line prompt, pay attention to what comes after it)
(also, from now on I will call the file I'm working on "")
$ python
[1, 2, 3]

Whoah, just wait a second. What the hell happened at line 3? Why do I need to ask for the 0th element to get the first one?!
Well, as you saw, the syntax of getting a list element is "list[element_number]". The thing about lists, though, is that they are "zero-indexed". That means that we're naming the elements starting from the number 0 instead of 1. So, the 1st element is the 0th, the 2nd is the 1st, the 3rd is the 2nd and so on.
[Tip: To easily remember this, just subtract one from the position of list element you want to access. Example: element 3 - 1 = 2. Thus, the 3rd element is accessed by the command "mylist[2]".]

You can also add or change elements just by assigning values to certain positions in the list:
menu = ["eggs", "tomatoes", "chicken"]
print menu
menu[2] = "steak"
print menu # see the difference?
menu.append("potatoes") # let's add another element to the menu
print menu # woohoo!

Here's what you should get:
$ python
['eggs', 'tomatoes', 'chicken']
['eggs', 'tomatoes', 'steak']
['eggs', 'tomatoes', 'steak', 'potatoes']

So, there's some new stuff: apparently, to change the value of an element you just assign to it the value of something else (as you saw in line 3). Also, to add a new element to the list, you use a "list method". Methods are neat little functions (aka: bunch of code that is repeatedly used and does the same stuff) that can only be used on things like lists, strings etc. You can find all list methods and how to use them in the Python docs.

Anyway, all "append" does is adding another element to the list. What if you want to delete an element, though?
menu = ["eggs", "tomatoes", "chicken"]
print menu
menu.remove("eggs") # goodbye eggies
print menu
special_ingredient = menu.pop()
print "What's left: %r" % menu # %r is for printing in raw format (sometimes it's useful to know what data type is the output)
print "Special ingredient: %s" % special_ingredient

$ python
['eggs', 'tomatoes', 'chicken']
['tomatoes', 'chicken']
What's left: ['tomatoes']
Special ingredient: chicken

And that's how to delete elements from lists! Note that "pop()" not only removes the (last) element, but also returns it; that means we can store that element in a variable, as I did with "special_ingredient".
So, go ahead and try out some other methods! Get to know lists as much as you can!
Thank you for reading this tutorial! I'll see you next time, goodbye!

No comments:

Post a Comment