Python: Functions with Optional & Named Arguments

Python allows functions to have default values for arguments. These are used when the function is called without the argument. But where it gets really interesting is when you realize that in Python it’s also possible to name the arguments and call them in any order, which can be very useful.

def shirt(styleID, size='Medium', color='White'):
    ...

For example, in the ‘shirt’ function above, style is a required argument (since it has no default value) but the other two arguments i.e. size and color are optional (since they do have default values assigned).

There are several ways to call this function:

>>> shirt(1)

styleID gets a value of 1 and size and color are assigned their default values.

>>> shirt(1, "Large")

styleID gets a value of 1 and size gets a value of “Large”, while color gets the default value.

>>> shirt(1, color="Black")

styleID gets a value of 1 and color gets a value of “Black”, while size gets the default value.

>>> shirt(size="Small", styleID=2)

size gets a value of “Small” and styleID gets a value of 2, while color gets the default value.

How To Reverse A String in Python

Unlike other languages such as Ruby, there is no built-in method to reverse a string in Python. Here are two possible ways to accomplish that: 

1) The 'Long' Way

This approach uses a for-loop with the join method to reverse a string:

def rev(s):
    return ''.join([s[i] for i in range(len(s)-1,-1,-1)])

>>> print rev("abcde")
edcba

2) The Simpler Way

This approach uses Python's 'slice notation' to accomplish the same result:

def rev(s):
    return s[::-1]
   
>>> print rev("abcde")
edcba

I think I read somewhere that #1 (for-loop) is faster, but I do love the simplicity of slicing. It looks a little strange at first, if you're not used to Python. But it really is pretty easy to use once you get the hang of it.

Learn Python the Hard Way

Over the last two years, I've spent considerable time learning several programming languages (PHP, C# and Ruby).  I started developing web applications with PHP, moved onto ASP.NET MVC using C# and then decided to experiment with Ruby on Rails.  I really loved the simplicity of Ruby and the 'magic' of Rails, but there was something about the Ruby language that just didn't grok with me — I still can't exactly put my finger on it.

A couple of months ago, I read Dharmesh Shah's post 'Why PHP is Fun and Easy But Python is Marriage Material'.  He made a compelling case for using Python and so a few weeks later I decided to take a closer look at Python myself. As sad as it sounds, I've finally found a programming language that I'm really excited about. It's simple and easy to get started with Python but it's also an extremely powerful language. And after I got over some of the initial 'weirdness' of Python e.g. using tabs for code-blocks, it really started to grow on me.

Learn Python The Hard Way

If you're interested, a great place to start learning Python is Zed Shaw's "Learn Python the Hard Way".  The online version of the book is available for free and there's also an  online class with screencasts at Udemy.com. The course is really designed for people with little or no programming experience. I consider myself to be a novice, but after a few years of programming in other languages, I realized that I actually know more than I thought I did. It was actually a very easy course for me, which I got through relatively quickly. But it was a great way to learn the fundamentals of Python. It might be too basic for a lot of people, but I still think it's worth taking a quick look at.

At the moment, I'm going through Mark Pilgrim's "Dive Into Python" book — which so far has been EXCELLENT. This is a book for experienced programmers and you will find a ton of useful information here. This book is available for free (published under the GNU Free Documentation License) and can be downloaded in a number of formats and languages. Along with this, I'm also reading Ayman Hourieh's 'Django 1.0 Website Development' book (Django is an excellent web framework for Python). I guess you could call it "Python on Django" but that doesn't quite roll off the tongue as "Ruby on Rails" does.