Pythonin’ – Sortin’, Tuples, Dicts…

We are here in Week 1 of learning Python! So far, it is going well. I feel I am picking up things pretty quickly. Just a recap, in the last post we learned a little intro and about strings and lists. Today we are going to talk about sorting, tuples (??), dicts, & del.

Sorting

Sorting a list can easily be done with the sorted(list) function. FYI: This doesn’t actually change the original list. You can pass through arguments to say, reverse the list like this: print sorted(list, reverse=True). The Python Wiki will tell you more. You can also use custom sorting with key=. Basically you would first use a predefined function and pass it through: print sorted(list, key=MyFn).

Tuples

I’m going to take these first lines directly from the Google Python Class (thank you license agreement!) because it sums these tuples up well: “A tuple is a fixed size grouping of elements, such as an (x, y) co-ordinate. Tuples are like lists, except they are immutable and do not change size.” You can create a tuple by just using parentheses. Because it is fixed size, remember to have the appropriate holding spaces separated by commas or you will get some errors. You can deal with tuples similarly to list except for things like append or things that assign indices or slices. I’d read up on them on say WikiBooks or the Python Docs.

Dicts

Dict (Dictionary) is Python’s key/value hash table structure. Side note: I had a hard time with hash tables and arrays in Ruby. I kept confusing them. Probably my roughest hours were working with multiple APIs and thus hash tables in my first major class project and I seriously just wanted to throw out my computer. I’m at a better place now. BACK TO PYTHON DICT. I think this is easier to explain in code:

Screen Shot 2014-08-03 at 11.52.55 AM

 

 

 

 

There was a section on % formatting basically that for an integer if you say had hash[‘count’] = 27 and a string hash[‘fruit’] you could write it in in a string as string = ‘There are %(count)d %(fruit)s in the bucket’. Self explanatory.

Del

So that variable, list element, or dict entry is giving you some trouble and you just want to get rid of it. Del that thing!

Del

 

 

 

 

WHOOOHOO.

So we got through week 1 of Python! Over the last week we learned about Strings, Lists, Sorting, and Dicts plus the little parts that go along with them. At this point I am going to take a break from the Google Python Class to do Team Treehouse’s new Python Basic course as it goes over using Python, strings, numbers, lists, and control structures and functions. Plus I get to build a little game! FUN!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s