Wednesday, June 23, 2010

Python: Defining a function.

Defining a function in python


Python function definitions are simple. Defining a function in python is very similar to many other languages out there. Lets try it out:




def hello(x) :
print "Hello", x

hello("Bill")


Output is as follows



Hello Bill


Notice we used print with a comma. This allows concatenation of strings in a single line statement.

An interesting property of functions in python is that you are able to assign them to variables.


variable1 = hello # assigns the variable "variable1" to the function we created above "hello"
variable1("Sarah")


Will output



Hello Sarah


That is all for now. Thanks and please add comments.

No comments:

Post a Comment