Saturday, March 2, 2013

Getting Your Feet Wet With Python



Many programmers parrot the phrase, "the best way to learn a computer language is by diving in". But it wasn't until recently that I fully understood the importance of this. You see, unlike spoken languages there is no real starting point. With a spoken language you can learn an alphabet and maybe some fancy words to impress your friends with. But chances are, using these methods as a starting point will confuse the average person when they take syntax and language logic into account (oddities of the English language, anyone?). With programming you can watch tutorials, examine code and experiment before you even learn to print "Hello world!" to a console window.  Not only does this allow one to pick up on concepts, it also enhances understanding of the logic within languages and computers in general. All without the tediousness of learning spoken languages. How cool is that?

Today I want to detail a concept within Python: that of Date time. But first, let's examine how to use this functionality. Otherwise you'll be cursing at me for not explaining it.
To use datetime, we simply use:

We're importing module datetime from a class called datetime. Easy enough, right? Now we can work with it..

So we've assigned the variable todaysDate to datetime.now(). We could run this program as is but let's assume for a minute you're picky. You want the standard m/d/yyyy format. How do you accomplish this? Enter concatenation. Concate-what, you say? Concatenation is just a fancy word for combining two strings --> ("I'd give my " + 'left arm to be ambidextrous').

Quick tip: Note the usage of ' and ". If ever your shell returns a syntax error because you've used an apostrophe in a string, use double quotes around the string. Or you can insert half quotes around the character. Either way is correct, but double quotes allow for easier reading.

Ok, great. So we've discovered concatenation. Hey, this is easy! Mission accomplished! Not so fast. Because concatenation only works with strings, we need to do what's called explicit type conversion. Explicit type conversion means what it says. In this case: we convert our integers to a string in order to concatenate them. Accomplishing this is relatively simple. We just use the str(x) syntax, where x is our method.

Now we know about combining two strings and we know about type conversation. How do we piece the two together? We just take each individual piece of our date (month/day/year) and write them within our string conversion:

Notice that we're adding the variable todaysDate to our method and that we're concatenating our /'s separately. If we don't do these separately, our IDE throws a syntax error. Another important point to remember is that only our /'s are commented out. This is because it's seen as a division operator otherwise. We want a literal, so we use quotations (or half quotes).

End result:

Now you have a feel for not only how to invoke classes but also how to call them, convert input to a string and format them using concatenation.

We can even expand on this by subtracting dates:

If the above sounds confusing (and it can be), let's demystify this puppy:

  • We open with str(), which explicitly converts any input within to a string.
  • Our next set of parenthesis (date closes after date.today(). The parenthesis on date.today allow it to be called on. Without it Python tells us, "Well hold on just a minute there, sucka. I don't know what you're talking about". (Okay, not really)
  • .days takes the sum of birthday - date.today() and displays it strictly in days. Then our  str() function closes.
 Walla! You now have a simple program that can subtract two dates and display the remainder.

Final note: The order in which statements are executed depends on their placement. Try running this program without tabbing either of the print lines.
Think of the print statements in this scenario as being hidden. The only way they can be accessed is if the conditions above them are met. This allows for better readability (and who wouldn't want that?).

No comments:

Post a Comment