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:

Dict

 

 

 

 

 

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!!

Teaching myself Python – Day 1

I’m a Rubyist, I love that language. It is so fun and makes me happy. Regardless, I keep seeing jobs at companies that excite me but require or majorly emphasize Python. It has been getting me down and I finally (with the help of awesome YKat) decided, why don’t I just teach myself another language! So we begin today.

Python definitely beats RoR on install…my mac already was all setup for Python. Boom. Did I tell you I just had to re-setup my whole system because I spilt coffee on my computer and fried it? *cries*. RIP MacBook Air. Anyways, back to Python.

I’m following Google’s Python Class for people with some programming experience. It is FREE online. They do it as a 2-day intensive on campus. It includes written lessons, exercises, and lecture videos. It is a bit outdated but the core concepts should all be good.

Day 1 – Intro, Strings, Arra…I mean lists,

Google’s Python Class comes from a “you probably came from a C++/Java background” stance and even though it has been 10 years since I coded in those languages, it helps to get those pointers. I will mainly just be focusing on Python vs Ruby though. *Reminder to create/find Ruby/Python Cheat-Sheet*. Formatting is imperative with Python ie. the end of the line marks the end of the statement, indentations are used to express meaning, no need for semi-colons, etc. Concise and clean. Reminds me of CoffeeScript, which I enjoy. Mmm Coffee.

Strings-known as str. The len(str) function gets the length of the string and if you are doing the Google Python Class, be prepared to use it over and over. There are a bunch of string methods found here which you should keep handy until memorized. To me, I thought of all the string exercises as math “in a good way” and that made it pretty easy for me.  Speaking of exercises, you best get on to them!

Lists-So I see the example and am like, oh arrays, cool I just spent 2 weeks doing this in JavaScript..again. But then it says they are called Lists in Python. So LISTS. These work similarly to strings, awesome. One of the key things to learn are FOR and IN, I’m told to learn this syntax as I will use it a lot. FOR is a way to look at items in a collection such as WORD in WORDS. IN is a way to look if an item is in a collection.  Also learned about Range and While loops. Got those in Ruby. There are a bunch of list methods you can use, check them out.

Overall, I feel like I am getting it pretty fast especially compared to other languages.  The exercises went well, I had some formatting issues (to be expected) but otherwise I did pretty good. Excited to return to the Google Python Class tomorrow. Also, just saw that Team Treehouse JUST launched a Python course…I think I might do that next.