Difference between Positional , keyword, optional and required argument?
<p>I am learning about functions in python and found many good tutorials and answers about functions and their types, but I am confused at some places. I have read the following:</p>

<p>If function has <code>"="</code> then it's a keyword argument i.e <code>(a,b=2)</code>
If function does not have <code>"="</code> then it's positional argument i.e  <code>(a,b)</code></p>

<p>My doubts :</p>

<ul>
<li><p>What is the meaning of a required argument and an optional argument ? Is default argument also a keyword argument? (since because both contain "=")</p></li>
<li><p>Difference between the positional , keyword, optional and required
arguments?</p></li>
<li><p>python official documentation says that there are two types of arguments. If so, then what are <code>*args and **kargs</code> (I know how they work but don't know what they are)</p></li>
<li><p>how <code>*args</code> and <code>**kargs</code> store values? I know how <code>*args</code> and
<code>**kargs</code> works but how they store values? Does <code>*args</code> store values in
tuple and <code>**kargs</code> in dictionary ?</p></li>
</ul>

<p>please explain in deep.I want to know about functions because I am a newbie :)</p>

<p>Thanks in advance </p>

<h2>Default Values</h2>

<p>Let's imagine a function,</p>

<pre><code>def function(a, b, c):
    print a
    print b
    print c
</code></pre>

<p>A positional argument is passed to the function in this way.</p>

<pre><code>function("position1", "position2", "position3")
</code></pre>

<p>will print</p>

<pre><code>position1
position2
position3
</code></pre>

<p>However, you could pass in a keyword argument as below,</p>

<pre><code>function(c="1",a="2",b="3")
</code></pre>

<p>and the output will become:</p>

<pre><code>2
3
1
</code></pre>

<p>The input is no longer based on the <strong>position</strong> of the argument, but now it is based on the <strong>keyword</strong>.</p>

<p>The reason that <strong>b</strong> is optional in (a,b=2) is because you are giving it a default value. </p>

<p>This means that if you only supply the function with one argument, it will be applied to <strong>a</strong>. A default value must be set in the function definition. This way when you omit the argument from the function call, the default will be applied to that variable. In this way it becomes 'optional' to pass in that variable.</p>

<p>For example:</p>

<pre><code>def function(a, b=10, c=5):
    print a
    print b
    print c

function(1)
</code></pre>

<p>and the output will become:</p>

<pre><code>1
10
5
</code></pre>

<p>This is because you didn't give an argument for <strong>b</strong> or <strong>c</strong> so they used the default values. In this sense, <strong>b</strong> and <strong>c</strong> are optional because the function will not fail if you do not explicitly give them.</p>

<h2>Variable length argument lists</h2>

<p>The difference between *args and **kwargs is that a function like this:</p>

<pre><code>def function(*args)
    for argument in args:
        print argument
</code></pre>

<p>can be called like this:</p>

<pre><code>function(1,2,3,4,5,6,7,8)
</code></pre>

<p>and all of these arguments will be stored in a tuple called <strong>args</strong>. Keep in mind the variable name args can be replaced by any variable name, the required piece is the asterisk. </p>

<p>Whereas,</p>

<pre><code>def function(**args):
    keys = args.keys()
    for key in keys:
       if(key == 'somethingelse'):
           print args[key]
</code></pre>

<p>expects to be called like this:</p>

<pre><code>function(key1=1,key2=2,key3=3,somethingelse=4,doesnt=5,matter=6)
</code></pre>

<p>and all of these arguments will be stored in a dict called <strong>args</strong>. Keep in mind the variable name args can be replaced by any variable name, the required piece is the double asterisk. </p>

<p>In this way you will need to get the keys in some way:</p>

<pre><code>keys = args.keys()
</code></pre>

python python-2.7 python-3.x