

{"id":5695,"date":"2018-01-06T07:46:53","date_gmt":"2018-01-06T02:16:53","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5695"},"modified":"2026-04-28T11:07:09","modified_gmt":"2026-04-28T05:37:09","slug":"python-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-function\/","title":{"rendered":"Python Function &#8211; Type of Functions in Python"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2173,&quot;href&quot;:&quot;https:\\\/\\\/en.wikibooks.org\\\/wiki\\\/Python_Programming\\\/Functions&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251009114617\\\/https:\\\/\\\/en.wikibooks.org\\\/wiki\\\/Python_Programming\\\/Functions&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-11 01:27:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-14 18:11:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-24 13:28:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-30 12:08:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-04 10:57:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-14 07:16:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-19 04:43:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-22 06:55:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-27 07:54:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-30 08:00:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-02 11:12:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-07 08:58:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-11 07:55:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-18 14:13:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-21 15:26:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-27 18:17:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-05 04:44:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-09 06:13:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-12 15:13:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 20:13:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-25 09:29:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-30 17:32:22&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-04-03 06:47:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-08 09:04:22&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-04-11 15:47:56&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-15 05:45:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-18 08:52:54&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-06 05:56:16&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-10 06:40:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-15 11:32:53&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-20 11:02:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-24 08:52:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-05 03:41:45&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-05 03:41:45&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In our tutorial, we discussed\u00a0<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-dictionaries\/\">dictionaries in python<\/a>.\u00a0<\/strong>Now, we move forward to deeper parts of the language, let&#8217;s read about Python functions. Moreover, we will study the different types of functions in Python: Python built-in functions, Python recursion function, Python lambda function, and Python user-defined functions with their syntax and examples.<\/p>\n<p>So, let&#8217;s start the Python Function Tutorial.<\/p>\n<div id=\"attachment_5698\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5698\" class=\"wp-image-5698 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions.jpg\" alt=\"Python Function Tutorial - Type of Functions in Python(With Example)\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-5698\" class=\"wp-caption-text\">Python Function Tutorial &#8211; Type of Functions in Python(With Example)<\/p><\/div>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/range-function-in-python\/\">Range Function in Python \u2013 Range() in Python<\/a><\/strong><\/p>\n<h3>An Introduction to Function in Python<\/h3>\n<p>A function in Python is a block of reusable code that carries out a single well-defined job. Instead of writing the same lines again and again, you wrap them inside a named function. When your program reaches that name, Python runs the saved steps and comes back with a result. This habit keeps programs short, tidy, and easy to fix.<\/p>\n<p>Well-planned functions boost readability and teamwork. When code is split into clear tasks, bugs hide less. Libraries full of neat functions also load faster because each file holds only what is needed. This structure is one big reason why Python is loved for web apps, data analysis, and automation alike.<\/p>\n<h3>User-Defined Functions in Python<\/h3>\n<p>For simplicity purposes, we will divide this lesson into two parts. First, we will talk about user-defined functions in Python. Python lets us group a sequence of statements into a single entity, called a function. A Python function may or may not have a name. We\u2019ll look at functions without a name later in this tutorial.<\/p>\n<h4>a. Advantages of User-defined Functions in Python<\/h4>\n<p>1. This Python function helps divide a program into modules. This makes the code easier to manage, debug, and scale.<\/p>\n<p>2. It implements code reuse. Every time you need to execute a sequence of statements, all you need to do is to call the function.<\/p>\n<p>3. This Python function allows us to change functionality easily, and different programmers can work on different functions.<\/p>\n<h4>b. Defining a Function in Python<\/h4>\n<p>To define your own Python function, you use the \u2018def\u2019 keyword before its name. And its name is to be followed by parentheses, before a colon(:).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def hello():\r\n          print(\"Hello\")<\/pre>\n<p>The contents inside the body of the function must be equally indented.<\/p>\n<p>As we had discussed in our article on <strong><a href=\"https:\/\/data-flair.training\/blogs\/python-syntax-semantics\/\">Python syntax<\/a><\/strong>, you may use a docstring right under the first line of a function declaration. This is a documentation string, and it explains what the function does.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def hello():\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"\"\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  This Python function simply prints hello to the screen\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"\"\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \u00a0 print(\"Hello\")<\/pre>\n<p>You can access this docstring using the __doc__ attribute of the function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func1():\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"\"\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0  This is the docstring\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \"\"\"\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0  print(\"Hello\")<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; func1.__doc__\r\n'\\n\\tThis is the docstring\\n\\t'<\/pre>\n<p>However, if you apply the attribute to a function without a docstring, this happens.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum.__doc__\r\n&gt;&gt;&gt; type(sum.__doc__)\r\n&lt;class 'NoneType'&gt;\r\n&gt;&gt;&gt; bool(sum.__doc__)<\/pre>\n<p>False<\/p>\n<p>If you don\u2019t yet know what to put in the function, then you should put the pass statement in its body. If you leave its body empty, you get an error \u201cExpected an indented block\u201d.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def hello1():\r\n       pass\r\n&gt;&gt;&gt; hello1()<\/pre>\n<p>You can even reassign a function by defining it again.<\/p>\n<p><strong>Learn <a href=\"https:\/\/data-flair.training\/blogs\/methods-in-python-programming\/\" target=\"_blank\" rel=\"noopener\">Methods in Python \u2013 Classes, Objects, and Functions in Python<\/a><\/strong><\/p>\n<h4>c. Rules for naming Python function (identifier)<\/h4>\n<p>We follow the same rules when naming a function as we do when naming a variable.<\/p>\n<p>1. It can begin with either of the following: A-Z, a-z, and underscore(_).<\/p>\n<p>2. The rest of it can contain either of the following: A-Z, a-z, digits(0-9), and underscore(_).<\/p>\n<p>3. A reserved keyword may not be chosen as an identifier.<\/p>\n<p>It is good practice to name a Python function according to what it does.<\/p>\n<p><strong>Learn <a href=\"https:\/\/data-flair.training\/blogs\/python-dictionaries\/\" target=\"_blank\" rel=\"noopener\">Python Dictionaries with Methods, Functions and Dictionary Operations<\/a><\/strong><\/p>\n<h4>d. Python Function Parameters<\/h4>\n<p>Sometimes, you may want a function to operate on some variables and produce a result. Such a function may take any number of parameters. Let\u2019s take a function to add two numbers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sum(a,b):\r\n            print(f\"{a}+{b}={a+b}\")<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum(2,3)<\/pre>\n<p>2+3=5<\/p>\n<p>Here, the function sum() takes two parameters- a and b. When we call the function, we pass numbers 2 and 3. These are the arguments that fit a and b respectively. We will describe calling a function in point f. A function in Python may contain any number of parameters, or none.<\/p>\n<p>In the next example, we try adding an int and a float.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sum2(a,b):\r\n         print(f\"{a}+{b}={a+b}\")\r\n&gt;&gt;&gt; sum2(3.0,2)<\/pre>\n<p>3.0+2=5.0<\/p>\n<p>However, you can\u2019t add incompatible types.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum2('Hello',2)<\/pre>\n<p>Traceback (most recent call last):<\/p>\n<p>File &#8220;&lt;pyshell#39&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>sum2(&#8216;Hello&#8217;,2)<\/p>\n<p>File &#8220;&lt;pyshell#38&gt;&#8221;, line 2, in sum2<\/p>\n<p>print(f&#8221;{a}+{b}={a+b}&#8221;)<\/p>\n<p>TypeError: must be str, not int<\/p>\n<p><strong>Learn <a href=\"https:\/\/data-flair.training\/blogs\/python-strings\/\" target=\"_blank\" rel=\"noopener\">Python Strings with String Functions and String Operations<\/a><\/strong><\/p>\n<h4>e. Python return statement<\/h4>\n<p>A Python function may optionally return a value. This value can be a result of its execution. Or it can be something you specify- an expression or a value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func1(a):\r\n         if a%2==0:\r\n               return 0\r\n          else:\r\n                return 1\r\n&gt;&gt;&gt; func1(7)<\/pre>\n<p>1<\/p>\n<p>As soon as a return statement is reached in a function, the function stops executing. Then, the next statement after the function call is executed. Let\u2019s try returning an expression.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def sum(a,b):\r\n         return a+b\r\n&gt;&gt;&gt; sum(2,3)<\/pre>\n<p>5<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; c=sum(2,3)<\/pre>\n<p>This was the Python Return Function<\/p>\n<h4>f. Calling a Python function<\/h4>\n<p>To call a Python function at a place in your code, you simply need to name it, and pass arguments, if any. Let\u2019s call the function hello() that we defined in section b.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; hello()<\/pre>\n<p>Hello<\/p>\n<p>We already saw how to call a Python function with arguments in section e.<\/p>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/python-operators\/\" target=\"_blank\" rel=\"noopener\">Python Operators with Syntax and Examples<\/a><\/strong><\/p>\n<h4>g. Scope and Lifetime of Variables in Python<\/h4>\n<p>A variable isn\u2019t visible everywhere and alive every time. We study this in functions because the scope and lifetime for a variable depend on whether it is inside a function.<\/p>\n<p><strong>1. Scope<\/strong><\/p>\n<p>A variable\u2019s scope tells us where in the program it is visible. A variable may have local or global scope.<\/p>\n<ul>\n<li><strong>Local Scope- <\/strong>A variable that\u2019s declared inside a function has a local scope. In other words, it is local to that function.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func3():\r\n        x=7\r\n        print(x)\r\n&gt;&gt;&gt; func3()<\/pre>\n<p>7<\/p>\n<p>If you then try to access the variable x outside the function, you cannot.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; x<\/pre>\n<p>Traceback (most recent call last):<\/p>\n<p>File &#8220;&lt;pyshell#96&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>x<\/p>\n<p>NameError: name &#8216;x&#8217; is not defined<\/p>\n<ul>\n<li><strong>Global Scope- <\/strong>When you declare a variable outside python function, or anything else, it has global scope. It means that it is visible everywhere within the program.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; y=7\r\n&gt;&gt;&gt; def func4():\r\n          print(y)\r\n&gt;&gt;&gt; func4()<\/pre>\n<p>7<\/p>\n<p>However, you can\u2019t change its value from inside a local scope(here, inside a function). To do so, you must declare it global inside the function, using the \u2018global\u2019 keyword.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func4():\r\n      global y\r\n      y+=1\r\n      print(y)\r\n&gt;&gt;&gt; func4()<\/pre>\n<p>8<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; y<\/pre>\n<p>8<\/p>\n<p>As you can see, y has been changed to 8.<\/p>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/\" target=\"_blank\" rel=\"noopener\">Python Variables and Data Types with Syntax and Examples<\/a><\/strong><\/p>\n<p><strong>2. Lifetime<\/strong><\/p>\n<p>A variable\u2019s lifetime is the period of time for which it resides in the memory.<\/p>\n<p>A variable that\u2019s declared inside a Python function is destroyed after the function stops executing. So the next time the function is called, it does not remember the previous value of that variable.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func1():\r\n         counter=0\r\n         counter+=1\r\n         print(counter)\r\n&gt;&gt;&gt; func1()<\/pre>\n<p>1<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; func1()\r\n<\/pre>\n<p>1<\/p>\n<p>As you can see here, the function func1() doesn\u2019t print 2 the second time.<\/p>\n<h4>h. Deleting Python function<\/h4>\n<p>Till now, we have seen how to delete a variable. Similarly, you can delete a function with the \u2018del\u2019 keyword.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func7():\r\n        print(\"7\")\r\n&gt;&gt;&gt; func7()<\/pre>\n<p>7<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del func7\r\n&gt;&gt;&gt; func7()<\/pre>\n<p>Traceback (most recent call last):<\/p>\n<p>File &#8220;&lt;pyshell#137&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>func7()<\/p>\n<p>NameError: name &#8216;func7&#8217; is not defined<\/p>\n<p>When deleting a function, you don\u2019t need to put parentheses after its name.<\/p>\n<p>Different Python Function Explained Below.<\/p>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/python-installation-tutorial\/\" target=\"_blank\" rel=\"noopener\">Python Installation Tutorial \u2013 Steps to Install Python on Windows<\/a><\/strong><\/p>\n<h3>Python Built-in Functions<\/h3>\n<p>In various previous lessons, we have seen a range of built-in functions in Python. This Python function applies to constructs like int, float, bin, hex, string, list, <a href=\"https:\/\/data-flair.training\/blogs\/python-tuples-syntax-examples\/\">tuple<\/a>, set, dictionary, and so. Refer to those lessons to revise them all.<\/p>\n<h3>Python Lambda Expressions<\/h3>\n<p>As we said earlier, a function doesn\u2019t need to have a name. A lambda expression in Python allows us to create an anonymous Python function, and we use the \u2018lambda\u2019 keyword for it. The following is the syntax for a lambda expression.<\/p>\n<p>lambda arguments:expression<\/p>\n<p>It\u2019s worth noting that it can have any number of arguments, but only one expression. It evaluates the value of that expression and returns the result. Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; myvar=lambda a,b:(a*b)+2\r\n&gt;&gt;&gt; myvar(3,5)<\/pre>\n<p>17<\/p>\n<p>This code takes the numbers 3 and 5 as arguments a and b respectively, and puts them in the expression (a*b)+2. This makes it (3*5)+2, which is 17. Finally, it returns 17.<\/p>\n<p>Actually, the function object is assigned to the identifier myvar.<\/p>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/python-function-arguments\/\">Python Arguments with Types, Syntax and Examples<\/a><\/strong><\/p>\n<p>Any Doubt yet in Python Function? Please Comment.<\/p>\n<h3>Python Recursion Function<\/h3>\n<p>A very interesting concept in any field, recursion is using something to define itself. In other words, it is something calling itself.<\/p>\n<p><strong>Types of Recursion in Python:<\/strong><\/p>\n<ul>\n<li><strong>Direct Recursion:<\/strong> A function calls itself directly.<\/li>\n<li><strong>Indirect Recursion:<\/strong> If one function calls another function, then that function will call the first one back.<\/li>\n<li><strong>Tail Recursion:<\/strong> If the function calls itself at the end, then python will not help to save memory or improve performance.<\/li>\n<\/ul>\n<p>In Python, recursion is when a function calls itself. To see how this could be useful, let\u2019s try calculating the factorial of a number. Mathematically, a number\u2019s factorial is:<\/p>\n<p>n!=n*n-1*n-2*\u2026*2*1<\/p>\n<p>To code this, we type the following.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def facto(n):\r\n\tif n==1:\r\n\t\treturn 1\r\n\treturn n*facto(n-1)\r\n&gt;&gt;&gt; facto(5)<\/pre>\n<p>120<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; facto(1)<\/pre>\n<p>1<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; facto(2)<\/pre>\n<p>2<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; facto(3)<\/pre>\n<p>6<\/p>\n<p>This was all about recursion function in Python<\/p>\n<p>Isn\u2019t this handy? Tell us where else you would use recursion. However, remember that if you don\u2019t mention the base case (the case that satisfies the condition), it can result in an infinite execution.<\/p>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/python-closure\/\" target=\"_blank\" rel=\"noopener\">Learn Python Closure \u2013 Nested Functions and Nonlocal Variables<\/a><\/strong><\/p>\n<p>This was all about the Python Function<\/p>\n<h3>Conclusion<\/h3>\n<p>It is important to revise to retain information. In this lesson, we learned about the Python function. First, we saw the advantages of a user-defined function in Python. Now we can create, update, and delete a function. And we know that a function may take arguments and may return a value. We also looked at the scope and lifetime of a variable. Hope you like the Python Function Tutorial.<\/p>\n<p>Don\u2019t forget to revise the various built-in functions supported by Python. Refer to our tutorials for the same.<\/p>\n<p><strong><a href=\"https:\/\/en.wikibooks.org\/wiki\/Python_Programming\/Functions\">Reference<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our tutorial, we discussed\u00a0dictionaries in python.\u00a0Now, we move forward to deeper parts of the language, let&#8217;s read about Python functions. Moreover, we will study the different types of functions in Python: Python built-in&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":35816,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[36641,4972,10551,10633,10903,11440,15225],"class_list":["post-5695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-built-in-function-in-python","tag-functions-in-python","tag-python-functions","tag-python-lambda-expressions","tag-python-tutorial","tag-recursion-function-in-python","tag-user-defined-functions-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Function - Type of Functions in Python - DataFlair<\/title>\n<meta name=\"description\" content=\"Let&#039;s read about Python functions, different types of functions in Python, and how they help to write reusable and clear code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Function - Type of Functions in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s read about Python functions, different types of functions in Python, and how they help to write reusable and clear code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-06T02:16:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T05:37:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Function - Type of Functions in Python - DataFlair","description":"Let's read about Python functions, different types of functions in Python, and how they help to write reusable and clear code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Function - Type of Functions in Python - DataFlair","og_description":"Let's read about Python functions, different types of functions in Python, and how they help to write reusable and clear code.","og_url":"https:\/\/data-flair.training\/blogs\/python-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-06T02:16:53+00:00","article_modified_time":"2026-04-28T05:37:09+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Function &#8211; Type of Functions in Python","datePublished":"2018-01-06T02:16:53+00:00","dateModified":"2026-04-28T05:37:09+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-function\/"},"wordCount":1647,"commentCount":11,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1.jpg","keywords":["built in function in python","functions in python","python functions","Python lambda expressions","python tutorial","recursion function in python","user defined functions in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-function\/","url":"https:\/\/data-flair.training\/blogs\/python-function\/","name":"Python Function - Type of Functions in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-function\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1.jpg","datePublished":"2018-01-06T02:16:53+00:00","dateModified":"2026-04-28T05:37:09+00:00","description":"Let's read about Python functions, different types of functions in Python, and how they help to write reusable and clear code.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-function\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Functions-1.jpg","width":1200,"height":628,"caption":"Python Function Tutorial - Type of Functions in Python(With Example)"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Function &#8211; Type of Functions in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5695"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5695\/revisions"}],"predecessor-version":[{"id":147975,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5695\/revisions\/147975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/35816"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}