<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ankur's Blog]]></title><description><![CDATA[Learning JS, Python, Linux and Physics]]></description><link>https://blog.ankurgajurel.com.np</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 14:20:07 GMT</lastBuildDate><atom:link href="https://blog.ankurgajurel.com.np/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Functions in Python]]></title><description><![CDATA[Python Functions are the block of code that will return particular data. Functions only run when we call them. We can call the function many times to run the same block of code instead of writing the code itself.
Syntax of Python Functions:
def funct...]]></description><link>https://blog.ankurgajurel.com.np/functions-in-python</link><guid isPermaLink="true">https://blog.ankurgajurel.com.np/functions-in-python</guid><category><![CDATA[PythonFunctions]]></category><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[functions in python]]></category><category><![CDATA[python core]]></category><dc:creator><![CDATA[Ankur Gajurel]]></dc:creator><pubDate>Sat, 08 Apr 2023 09:24:35 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1680945758957/da849340-3500-431c-81db-e9cad892d167.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Python Functions are the block of code that will return particular data. Functions only run when we call them. We can call the function many times to run the same block of code instead of writing the code itself.</p>
<h2 id="heading-syntax-of-python-functions">Syntax of Python Functions:</h2>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">function_name</span>(<span class="hljs-params">parameters</span>):</span>
    block of code

    <span class="hljs-keyword">return</span> data

function_name(arguments)
</code></pre>
<p>In the syntax, the terms used are:</p>
<ul>
<li><p><code>def</code> - Python keyword used to define the function</p>
</li>
<li><p><code>function_name</code> - User-defined name of the function</p>
</li>
<li><p><code>parameters</code> - We can only use the parameters as variables in the scope of the function.</p>
</li>
<li><p><code>block of code</code> - They are statements that run when we call the function.</p>
</li>
<li><p><code>return</code> - Returns values while calling the function so that we can use the function as a variable.</p>
</li>
</ul>
<h4 id="heading-example-ordinary-python-functions"><em>Example (ordinary Python functions):</em></h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func_name</span>(<span class="hljs-params">name</span>):</span>
    print(name)

your_name = <span class="hljs-string">"Ankur"</span>
func_name(your_name)
</code></pre>
<p>Here, the func_name() function has one parameter name. The function only prints the passed argument.</p>
<h2 id="heading-parameters-and-arguments">Parameters and Arguments</h2>
<p>Data can be passed into the function while calling it. While defining a function, parameters can be specified. The syntax is:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_function</span>(<span class="hljs-params">parameters</span>):</span>
    <span class="hljs-keyword">return</span> expression

my_function(arguments)
</code></pre>
<h4 id="heading-example-parameter-and-arguments"><em>Example (parameter and arguments):</em></h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">eligible_to_vote</span>(<span class="hljs-params">age</span>):</span>
    <span class="hljs-keyword">if</span>(age &gt; <span class="hljs-number">18</span> <span class="hljs-keyword">or</span> age == <span class="hljs-number">18</span>):
        <span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>

your_age = <span class="hljs-number">17</span>
print(eligible_to_vote(your_age))
</code></pre>
<p>The above function will take the integer "age" as an argument. It returns True if the integer "age" is greater or equal to 18. But, returns False in any other case.</p>
<h3 id="heading-1-default-arguments">1. Default Arguments:</h3>
<p>Default arguments are the parameters whose value is pre-declared when the function is defined. They are optional since the interpreter won't throw an error if we don't pass them while we call a function.</p>
<h4 id="heading-syntax">Syntax:</h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">function_name</span>(<span class="hljs-params">parameter=value</span>):</span>
    <span class="hljs-keyword">return</span> expression
</code></pre>
<p>Here, the value is pre-defined but if we pass an argument while calling the function, we can use a custom value for that parameter.</p>
<h4 id="heading-example-default-argument"><em>Example (default argument):</em></h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">power</span>(<span class="hljs-params">num, exp=<span class="hljs-number">2</span></span>):</span>
    <span class="hljs-keyword">return</span> num ** exp

print(power(<span class="hljs-number">2</span>))
</code></pre>
<p>The above program will return the square of 2. However, if we want the exponent to be 4, we can write the following code.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">power</span>(<span class="hljs-params">num, exp=<span class="hljs-number">2</span></span>):</span>
    <span class="hljs-keyword">return</span> num ** exp

print(power(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>))
</code></pre>
<h3 id="heading-2-keyword-arguments">2. Keyword Arguments</h3>
<p>We can use keyword arguments if we don't want to mess with the order of the parameters. The syntax is:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_func</span>(<span class="hljs-params">parameter</span>):</span>
    <span class="hljs-keyword">return</span> exp

my_func(paramerter=value)
</code></pre>
<h4 id="heading-examplekeyword-arguments"><em>Example(keyword arguments):</em></h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">power</span>(<span class="hljs-params">num, exp=<span class="hljs-number">2</span></span>):</span>
    <span class="hljs-keyword">return</span> num ** exp

print(power(num=<span class="hljs-number">10</span>))
</code></pre>
<p>The above program will print the square of 10. The exponent has a default value and the number has a keyword argument.</p>
<h3 id="heading-arbitrary-argument-args">Arbitrary Argument (*args)</h3>
<p>When the developer does not know how many arguments will be passed into the function, we can handle it with an arbitrary function.</p>
<p>We can use an asterisk or symbolical "*" to declare an arbitrary argument. The syntax is:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_function</span>(<span class="hljs-params">*args</span>):</span>
    code

my_function(arguments)
</code></pre>
<p>Keep in mind that Python will take the *args as a tuple of the arguments passed while calling the function.</p>
<h4 id="heading-example-arbitrary-arguments"><em>Example (arbitrary arguments)</em>:</h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">product</span>(<span class="hljs-params">*arguments</span>):</span>
    prod = <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> arguments:
        prod *= i

    <span class="hljs-keyword">return</span> prod

product(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
</code></pre>
<p>The above program will print the product of the numbers 1, 2, 3, and 4.</p>
<h3 id="heading-arbitrary-keyword-argument-kwargs">Arbitrary Keyword Argument (**kwargs):</h3>
<p>In the arbitrary functions, only positional arguments were passed. However, for **kwargs or arbitrary keyword argument, we should pass the key and value as the argument.</p>
<p>To be specific, the interpreter takes it as a dictionary, just like args were taken as a list.</p>
<h4 id="heading-example-keyword-arguments"><em>Example (keyword arguments)</em>:</h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_func</span>(<span class="hljs-params">**kwargs</span>):</span>
    <span class="hljs-keyword">for</span> name, age <span class="hljs-keyword">in</span> kwargs.items():
        print(<span class="hljs-string">f"<span class="hljs-subst">{name}</span> is <span class="hljs-subst">{age}</span> years old"</span>)

print(my_func(ankur=<span class="hljs-number">17</span>, romisha=<span class="hljs-number">18</span>, aakriti=<span class="hljs-number">23</span>))
</code></pre>
<p>The above program will print the following.</p>
<pre><code class="lang-python">ankur <span class="hljs-keyword">is</span> <span class="hljs-number">17</span> years old
romisha <span class="hljs-keyword">is</span> <span class="hljs-number">18</span> years old
aakriti <span class="hljs-keyword">is</span> <span class="hljs-number">23</span> years old
</code></pre>
<h2 id="heading-recursive-python-functions">Recursive Python Functions</h2>
<p>Recursion is defining something within itself and hence it runs itself indefinitely unless programmed correctly.</p>
<h4 id="heading-example-recursive-functions"><em>Example (Recursive Functions):</em></h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum</span>(<span class="hljs-params">x</span>):</span>
    <span class="hljs-keyword">if</span> x == <span class="hljs-number">1</span>:
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> (x + sum(x<span class="hljs-number">-1</span>))

num = <span class="hljs-number">3</span>
print(sum(num))
</code></pre>
<p>The above program prints the sum of digits from 1 to the given number. So, in this program, the sum of digits from 0 to 3 is 6.</p>
<h2 id="heading-lambda-python-functions">Lambda Python Functions</h2>
<p>Python has lambda functions that don't need a function name. We can compare it with the anonymous function in JavaScript if you're familiar with the language.</p>
<p>We can directly store the return value of the function in a variable.</p>
<h4 id="heading-example-lambda-functions"><em>Example (lambda functions):</em></h4>
<pre><code class="lang-python">age = int(input(<span class="hljs-string">"Enter the age: "</span>))
eligibility = <span class="hljs-keyword">lambda</span> age : <span class="hljs-literal">True</span> <span class="hljs-keyword">if</span> age &gt;= <span class="hljs-number">18</span> <span class="hljs-keyword">else</span> <span class="hljs-literal">False</span>  

print(eligibility(age))
</code></pre>
<p>The above program will input an integer from the user and check if the age is greater or equal to 18 and print True else False.</p>
<h2 id="heading-type-hints-in-python-functions">Type Hints in Python Functions</h2>
<p>Type Hints in Python is a way of declaring the data type to accept and return in functions.</p>
<p>We can declare the data type of the parameter in a function. Its syntax is:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">function_name</span>(<span class="hljs-params">parameter: data_type</span>):</span>
    <span class="hljs-keyword">return</span> data_type
</code></pre>
<p>Here, data_type is pretty explanatory.</p>
<h4 id="heading-exampletype-hints"><em>Example(type hints):</em></h4>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">square</span>(<span class="hljs-params">num: int</span>):</span>
    <span class="hljs-keyword">return</span> num ** <span class="hljs-number">2</span>
</code></pre>
<p>The above function will take the "num" integer as an argument. Then, it returns the two-exponent or "square" of the argument "num".</p>
<p>If we pass an argument of a different data type, a TypeError will be raised as usual.</p>
<p>We can also declare the type of data to return when a function is called. The syntax is:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">cube</span>(<span class="hljs-params">num: int</span>) -&gt; int:</span>
    <span class="hljs-keyword">return</span> num ** <span class="hljs-number">3</span>
</code></pre>
<p>The above function will return an integer which is the cube of an integer "num".</p>
<p>The Official Python Documentation of Function: <a target="_blank" href="https://docs.python.org/3/library/functions.html">Click</a>.</p>
<p>You can learn more about other core Python functionalities on our <a target="_blank" href="https://findkathmandu.com/">website</a>.</p>
]]></content:encoded></item></channel></rss>