Saturday, 21 January 2017

Python Tutorial -16 (Keyword Arguments)

In this video tutorial we covered Keyword Argument in python which is very interesting

Hey guys...

And welcome to python tutorial part 16

In this tutorial we will learn keyword arguments

For this we will make a simple function 

def say_hi (one="how", two="are", three="you")
    print (one,two,three)

Here is have said that we will define a function called say_hi.

We have given default argument as one, two and three with keywords as "how are you" 

Simple right. Just like we did in the last tutorial on default arguments.

Then we have to call the function and tell python to run it.


If we print this out we will get "how are you"

Now we will add another call - 

say_hi ("i", "am", "fine")

If we run this we will get both the calls - 

"how are you"
"i am fine"

But why did python say "i am fine" when the default argument is "how are you"

That's because python assumed that one is 'i', two is 'am' and three is 'fine'

Cool right. you can add keywords which can replace the default once.

Now I will say -

say_hi(three="bro", two="up", one="whats")

If we run it again we will get "whats up bro"

But the order was bro up whats ...

WTF how ?

That's because python took the order of default argument.

So it displayed the original order.

This is a good example for keyword argument.

Ok guys that's it for this tutorial. You learned Keyword Arguments today.

Have a safe hack.

You may want to see my other tutorials - 





No comments:

Post a Comment