Monday, April 2, 2012

More About Functions

Hello everyone, in this post I will discuss something more about functions..

Okay till now we have seen how to make a function and set the arguments that would take Tuple or Dictionary.

Ex:

>>> def sample1(name, *girlFrnds, **ages):
           print name
           print girlFrnds
           print ages

   
>>>
>>> sample1('Whiskey', 'Aini', 'Sweety', 'Rose', Aini=22, Sweety=21, Rose=23)
Whiskey
('Aini', 'Sweety', 'Rose')
{'Rose': 23, 'Sweety': 21, 'Aini': 22}
>>>

A Short Program, that prints the user name, girl friends name, and their name with ages.

Ok, now lets see, how to pass a Tuple Or Dictionary to a Function Directly.

Lets check with Tuple First.

>>> def sample2(*names):
            print names

   
>>> girlfriends = ('Aini', 'Sweety', 'Rose')
>>> sample2(*girlfriends)
('Aini', 'Sweety', 'Rose')
>>>

Now lets see Dictionary

>>> def sample3(**ages):
           print ages

   
>>> girl_age={'Aini':22, 'Sweety':21, 'Rose':23}
>>> sample3(**girl_age)
{'Rose': 23, 'Sweety': 21, 'Aini': 22}
>>>

Its very simple, practice it, just remember the way how it works, because we will need them in future posts.

The main difference is, when we are speaking about Tuple in function, its a one star(*) and Dictionary has two star(**)

Thank You, And Don't Forget to check my next post.

0 comments:

Post a Comment