Search This Blog

Sunday, March 16, 2014

Assign default values for function arguments

 def are_we_going (what_time, go_to_movie="Yes") : print(go_to_movie + ". We are going  at " + what_time)are_we_going("five")

>>> Yes. We are going at five


def are_we_going (what_time, go_to_movie="Yes") :

In the above declaration, we have assigned the value of go_to_movie as Yes. This will be the default value, if no value is passed to this argument while calling this function.

Another important thing to note is that the default arguments should follow the non-default arguments. If this rule is not followed, you will get the following error:

SyntaxError: non-default argument follows default argument


    

No comments:

Post a Comment