{"id":21156,"date":"2022-01-19T11:46:39","date_gmt":"2022-01-19T10:46:39","guid":{"rendered":"https:\/\/firmbee.com\/?p=21156"},"modified":"2022-01-27T09:56:29","modified_gmt":"2022-01-27T08:56:29","slug":"python-functions","status":"publish","type":"post","link":"https:\/\/firmbee.com\/python-functions","title":{"rendered":"Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts"},"content":{"rendered":"<p><strong>This article will help the reader understand the basic Python functions along with some basic applications in the real world. We will be using Visual Studio Code as our code editor. If you have not installed Visual Studio Code, the instructions are given in the <a href=\"https:\/\/firmbee.com\/part-1-python-course-in-10-blog-posts\/\" target=\"_blank\" rel=\"noopener\">first blog.<\/a><\/strong><\/p>\r\n\r\n<h2>Python functions &#8211; table of contents:<\/h2>\r\n<ol>\r\n<li><a href=\"#firstparagraph\">Python functions<\/a><\/li>\r\n<li><a href=\"#secondparagraph\">Python functions as objects<\/a><\/li>\r\n<li><a href=\"#thirdparagraph\">Storing Python functions in Data Structures<\/a><\/li>\r\n<\/ol>\r\n\r\n<h2 id=\"firstparagraph\">Python functions<\/h2>\r\n<p>Python functions are objects that means the functions can be used as return value for other functions, can be stored in a variable, can be stored in data structures, or can be used as an argument in other functions.<\/p>\r\n<p>Python functions are defined using \u201cdef\u201d keyword following the function name. Then inside these brackets \u201c()\u201d , the arguments are defined. The basic syntax of Python functions is illustrated below.<\/p>\r\n<p>For Example:<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# Create a function\r\n# def keyword\r\ndef functioname(): \r\n\r\n\r\n\r\n<\/pre>\r\n<p>Note:<\/p>\r\n<p>Function name is also having the same norms as the variable declaration.<\/p>\r\n\r\n<p>Let\u2019s write our first function<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# first function\r\n\r\ndef sum(a,b):\r\n\treturn a+b\r\n\r\n\r\n\r\n<\/pre>\r\n\r\n<p>In the above code block, we have written a function that gives us the sum of two numbers. As you can see, we have used \u201cdef\u201d keyword, a and b are the arguments which in our case would be the numbers we want the sum for. Now, we have used a keyword here called \u201creturn\u201d which is used to return the desired value or string from the function after performing the desired task. The values which are returned by using returned keywords can be further assigned to other variables or can be used in functions as an argument.<\/p>\r\n<p>Let\u2019s now see, how to use this function on our desired numbers.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# first function\r\n\r\ndef sum(a,b):\r\n\treturn a+b\r\n\r\nsum(6,7)\r\n\r\nx=sum(6,7)\r\nprint(x)\r\n<\/pre>\r\n\r\n<p>As you can see if we just use the function, the function will not show any value, but when we store the functions return value in another variable and print it, it gives the desired result.<\/p>\r\n<p>Let\u2019s run the program and see the output<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# Output\r\n\r\n13\r\n\r\n<\/pre>\r\n<p>We have got the output as 13, which is the sum of 6 and 7. Let\u2019s write another function which gives us fullname given firstname and lastname.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# second function\r\ndef fullname(fn,ln):\r\n\treturn fn+ln\r\n\r\nx=fullname(\u201cpython\u201d,\u201dlanguage\u201d)\r\nprint(x)\r\n<\/pre>\r\n<p>As you can see, we have just defined the function fullname and gave it parameters firstname and lastname. We are returning fullname using \u201c+\u201d which is a concatenation operator in string which we learnt in the variables blog.<\/p>\r\n<p>Let\u2019s explore the output<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n#Output\r\n\r\npythonlanguage\r\n<\/pre>\r\n<h2 id=\"secondparagraph\">Python functions as objects<\/h2>\r\n<p>Most of the data in <a href=\"https:\/\/pl.wikipedia.org\/wiki\/Python\" target=\"_blank\" rel=\"noopener\">Python <\/a>is represented in the form of objects. In Python strings, modules, functions are all represented in the form of objects. Let\u2019s see how we can use functions as objects.<\/p>\r\n<h3>Assigning functions to a variable<\/h3>\r\n<p>As function is an object, it can be assigned to a variable. Example is illustrated below.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# first function\r\n\r\ndef sum(a,b):\r\n\treturn a+b\r\n\r\nsumab=sum\r\n<\/pre>\r\n<p>In the above example, we can see that assigning it to new variable doesn\u2019t call the function  instead it just assigns the function to the variable \u201csumab\u201d. The actual meaning of the above example is that the variable \u201csumab\u201d takes the sum function object as a reference and the \u201csumab\u201d now points to that object. Hence the sumab can also be used as a function now. Example is illustrated below.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# New function\r\n\r\ndef sum(a,b):\r\n\treturn a+b\r\n\r\nsumab=sum\r\n\r\ns=sumab(7,8)\r\nprint(s)\r\n<\/pre>\r\n<p>Output:<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n#output\r\n\r\n15\r\n<\/pre>\r\n<p>Note:<\/p>\r\n<p>The function name which we give in the declaration and the function objects work very differently. Even if we delete the original function name, if there is another name pointing to that reference function object, still the function will work. Example is illustrated below.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# New function\r\n\r\ndef sum(a,b):\r\n\treturn a+b\r\n\r\nsumab=sum\r\n\r\ndel sum\r\n\r\nsum(8,7)\r\n<\/pre>\r\n<p>Output:<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n#Output\r\n\r\nNameError: \u201cname \u2018sum\u2019 is not defined\u201d\r\n<\/pre>\r\n<p>But when we use the sumab function, then the result is illustrated below.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n# New function\r\n\r\ndef sum(a,b):\r\n\treturn a+b\r\n\r\nsumab=sum\r\n\r\ndel sum\r\n\r\nsumab(8,7)\r\n<\/pre>\r\n<p>Output:<\/p>\r\n\r\n<p> 15<\/p>\r\n\r\n\r\n<h2 id=\"thirdparagraph\">Storing Python functions in Data Structures<\/h2>\r\n<p>As the functions are objects in Python, we can store them in data structures in the same way we store our variables and constants. The syntax changes a little bit but it\u2019s like how we stored elements in the datatypes.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n#function storing in datastructures\r\n\r\nStoredfunctionslist=&#x5B;len,str.upper(),str.strip(),str.lower()]\r\n\r\nStoredfunctionslist\r\n\r\n<\/pre>\r\n\r\n\r\n\r\n<p>Iterating through functions is just like iterating objects. Example illustrated below.<\/p>\r\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n#function storing in datastructures\r\n\r\nStoredfunctionslist=&#x5B;len,str.upper(),str.strip(),str.lower()]\r\n\r\nfor fun in Storedfunctionslist:\r\n\u00a0 \u00a0 print(fun, fun('Hello'))\r\n<\/pre>\r\n\r\n\r\n\r\n<p>In this blog, we have covered some basics Python functions, the further detailed topics on functions will be covered in the <a href=\"https:\/\/firmbee.com\/advanced-functions-in-python\/\" target=\"_blank\" rel=\"noopener\">next blog post.<\/a><\/p>\r\n\r\n\r\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/firmbee.com\/wp-content\/uploads\/Python-functions-800x200.jpg\" alt=\"python_functions\" width=\"800\" height=\"200\" class=\"alignnone size-medium wp-image-21161 img-fluid\" title=\"\" srcset=\"https:\/\/firmbee.com\/wp-content\/uploads\/Python-functions-800x200.jpg 800w, https:\/\/firmbee.com\/wp-content\/uploads\/Python-functions-2000x500.jpg 2000w, https:\/\/firmbee.com\/wp-content\/uploads\/Python-functions-900x225.jpg 900w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/>\r\n\r\n<p><strong>You may also like our <a href=\"https:\/\/firmbee.com\/blog\/javascript-course\/\" target=\"_blank\" rel=\"noopener\">JavaScript Course from Beginner to Advanced.<\/strong><\/p><\/a>\r\n\r\n","protected":false},"excerpt":{"rendered":"This article will help the reader understand the basic Python functions along with some basic applications in the real world. We will be using Visual [&hellip;]","protected":false},"author":8,"featured_media":21162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[5,491,1],"tags":[],"class_list":["post-21156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-python-course","category-other"],"acf":{"faq":null,"seria_wpisow":"Python Course From Beginner to Advanced in 11 blog posts","tytul_w_serii":"Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts","":"","naglowek_spisu_tresci":"","spis-tresci-lista":null},"_links":{"self":[{"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/posts\/21156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/comments?post=21156"}],"version-history":[{"count":9,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/posts\/21156\/revisions"}],"predecessor-version":[{"id":21740,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/posts\/21156\/revisions\/21740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/media\/21162"}],"wp:attachment":[{"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/media?parent=21156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/categories?post=21156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/firmbee.com\/wp-json\/wp\/v2\/tags?post=21156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}