{"id":77803,"date":"2020-03-31T15:02:49","date_gmt":"2020-03-31T09:32:49","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77803"},"modified":"2020-03-31T15:02:49","modified_gmt":"2020-03-31T09:32:49","slug":"python-function-arguments","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/","title":{"rendered":"Python Function Arguments &#8211; Learn the 3 types of Arguments"},"content":{"rendered":"<p>Python, like many other programming languages, allows you to <strong>break large code<\/strong> into <strong>smaller components<\/strong>. We do this by <strong>creating functions<\/strong>.<\/p>\n<p>But before we go ahead and discuss the <strong>types of arguments<\/strong> a Python <strong>function<\/strong> can take, let\u2019s quickly learn what a <strong>Python function<\/strong> looks like.<\/p>\n<h3>Python Functions<\/h3>\n<p>A <strong>function<\/strong> is basically a <strong>group of statements<\/strong> that performs some task. A function can:<\/p>\n<ul>\n<li>accept some <strong>arguments<\/strong> ( if needed),<\/li>\n<li>perform some <strong>functionality<\/strong>,<\/li>\n<li>and lastly can return a <strong>result<\/strong>.<\/li>\n<\/ul>\n<p>Below is a function that takes an argument and <strong>calculates its square<\/strong>:<\/p>\n<p><strong>Function definition:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def calcSquare(x):\n    result = x ** 2\n    return result<\/pre>\n<p><strong>Function call:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">num = 5\nprint(\"The square of\", num, \"is\", calcSquare(num))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The square of 5 is 25<br \/>\n&gt;&gt;&gt;<\/div>\n<p>We see here that we are passing a <strong>value (through function call)<\/strong> to the function <strong>calcSquare<\/strong>, and the function is <strong>receiving value (in the function definition)<\/strong>.<\/p>\n<p>Let us define these two types of values more formally:<\/p>\n<ul>\n<li><strong>Arguments:<\/strong> The values being <strong>passed<\/strong>, here <strong>num<\/strong>.<\/li>\n<li><strong>Parameters:<\/strong> The values being <strong>received<\/strong>, here <strong>x<\/strong>.<\/li>\n<\/ul>\n<h3>Types of Python Arguments<\/h3>\n<p>We are going to talk about the values being passed, that is, the <strong>Arguments<\/strong> in this article.<\/p>\n<p>Python supports the following types of arguments :<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/03\/types-of-python-arguments.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77822\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/03\/types-of-python-arguments.jpg\" alt=\"\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h4>1. Python Default Arguments<\/h4>\n<p>Python allows <strong>function parameters<\/strong> to have<strong> default values<\/strong>. This is useful in case a <strong>matching argument<\/strong> is <strong>not passed<\/strong> in the <strong>function call statement<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def sayhello(name = \"World\"):\n    print(\"Hello,\", name)\n\nsayhello()\nsayhello(\"Techvidvan\")<\/pre>\n<p>Here, the <strong>\u201csayhello\u201d<\/strong> function\u2019s <strong>header<\/strong> contains a <strong>parameter \u201cname\u201d<\/strong> which has its <strong>default value<\/strong> set to the <strong>string \u201cWorld\u201d<\/strong>.<\/p>\n<p>As you can see in the <strong>function call<\/strong>, the function prints <strong>\u201cHello, World\u201d<\/strong> when <strong>no argument<\/strong> is <strong>passed<\/strong>. Whereas, if we do <strong>pass<\/strong> a <strong>value<\/strong> for the <strong>argument<\/strong>, the function will print <strong>\u201cHello\u201d<\/strong> followed by the <strong>argument value<\/strong>.<\/p>\n<p><strong>The output looks like this:<\/strong><\/p>\n<div class=\"code-output\">Hello, World<br \/>\nHello, Techvidvan<br \/>\n&gt;&gt;&gt;<\/div>\n<p>One very important thing to remember while dealing with <strong>default arguments<\/strong> is :<\/p>\n<p>In a <strong>function\u2019s definition<\/strong>, a <strong>parameter<\/strong> cannot have a <strong>default value<\/strong> unless all the <strong>parameters<\/strong> to its right have their <strong>default values<\/strong>.<\/p>\n<h4>2. Python Keyword Arguments<\/h4>\n<p><strong>Python<\/strong> offers a way to <strong>write<\/strong> any <strong>argument<\/strong> in <strong>any order<\/strong> if you <strong>name<\/strong> the <strong>arguments<\/strong> when<strong> calling <\/strong>the <strong>function<\/strong>. This allows us to have <strong>complete control<\/strong> and <strong>flexibility<\/strong> over the <strong>values<\/strong> sent as <strong>arguments<\/strong> for the <strong>corresponding parameters<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def multiply(a, b):\n    return a*b\n\nprint(multiply(a = 10, b = 5))\nprint(multiply(b = 20, a = 9))<\/pre>\n<p>In the <strong>first<\/strong> function <strong>call<\/strong>, <strong>a<\/strong> gets value 10 and <strong>b<\/strong> gets value <strong>5<\/strong>. In the <strong>second<\/strong> function <strong>call<\/strong>, <strong>a<\/strong> gets value 9 and <strong>b<\/strong> gets value <strong>20<\/strong>.<\/p>\n<p><strong>The output is:<\/strong><\/p>\n<div class=\"code-output\">50<br \/>\n180<br \/>\n&gt;&gt;&gt;<\/div>\n<h4>3. Python Arbitrary Arguments<\/h4>\n<p><strong>Arbitrary arguments<\/strong> come in handy when we don\u2019t know <strong>how many arguments<\/strong> the <strong>function will take<\/strong>.<\/p>\n<p>Often at the time of <strong>defining the function<\/strong>, we cannot determine the <strong>number of arguments<\/strong> our <strong>function<\/strong> is going to <strong>accept<\/strong>. We place an <strong>asterisk ( * )<\/strong> before the <strong>parameter<\/strong> to <strong>denote<\/strong> that the <strong>function<\/strong> can take an <strong>arbitrary number<\/strong> of <strong>arguments<\/strong>.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def summation(*numbers):\n    sum1 = 0\n    for number in numbers:\n        sum1 += number\n\n    return sum1\n\nprint(summation(10,20,30))<\/pre>\n<p>The function summation takes <strong>multiple arguments<\/strong>.<\/p>\n<p>This outputs to the following:<\/p>\n<div class=\"code-output\">60<br \/>\n&gt;&gt;&gt;<\/div>\n<p>You can now give <strong>any number<\/strong> of <strong>arguments<\/strong> while <strong>calling<\/strong> the <strong>function summation<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>Here we come to the end of our article.<\/p>\n<p>In this article, we learned about the <strong>different arguments<\/strong> <strong>supported<\/strong> by a <strong>Python function<\/strong>. We have <strong>multiple types<\/strong> of <strong>arguments<\/strong> at our <strong>disposal<\/strong> in <strong>Python<\/strong>. This article will surely help you in your <strong>future Python Career<\/strong>.<\/p>\n<p>Now it\u2019s upon you to decide what kind of <strong>argument<\/strong> will best <strong>suit<\/strong> your <strong>function\u2019s requirements<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python, like many other programming languages, allows you to break large code into smaller components. We do this by creating functions. But before we go ahead and discuss the types of arguments a Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":77822,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2168,2169,2170,2171,2172,2173,1233,2174],"class_list":["post-77803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-arguments-in-python","tag-function-argument-in-python","tag-python-arbitrary-arguments","tag-python-arguments","tag-python-default-arguments","tag-python-function-arguments","tag-python-functions","tag-python-keyword-arguments"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Function Arguments - Learn the 3 types of Arguments - TechVidvan<\/title>\n<meta name=\"description\" content=\"Make yourself aware with Python Function Arguments in detail. Also, learn the 3 types of Python Arguments with some coding examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Function Arguments - Learn the 3 types of Arguments - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Make yourself aware with Python Function Arguments in detail. Also, learn the 3 types of Python Arguments with some coding examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-31T09:32:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/03\/types-of-python-arguments.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Function Arguments - Learn the 3 types of Arguments - TechVidvan","description":"Make yourself aware with Python Function Arguments in detail. Also, learn the 3 types of Python Arguments with some coding examples.","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:\/\/techvidvan.com\/tutorials\/python-function-arguments\/","og_locale":"en_US","og_type":"article","og_title":"Python Function Arguments - Learn the 3 types of Arguments - TechVidvan","og_description":"Make yourself aware with Python Function Arguments in detail. Also, learn the 3 types of Python Arguments with some coding examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-03-31T09:32:49+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/03\/types-of-python-arguments.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Function Arguments &#8211; Learn the 3 types of Arguments","datePublished":"2020-03-31T09:32:49+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/"},"wordCount":545,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/03\/types-of-python-arguments.jpg","keywords":["arguments in python","function argument in python","Python Arbitrary Arguments","python arguments","Python Default Arguments","Python Function Arguments","python functions","Python Keyword Arguments"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/","url":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/","name":"Python Function Arguments - Learn the 3 types of Arguments - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/03\/types-of-python-arguments.jpg","datePublished":"2020-03-31T09:32:49+00:00","description":"Make yourself aware with Python Function Arguments in detail. Also, learn the 3 types of Python Arguments with some coding examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/03\/types-of-python-arguments.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/03\/types-of-python-arguments.jpg","width":802,"height":420,"caption":"types of python argument"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-function-arguments\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Function Arguments &#8211; Learn the 3 types of Arguments"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77803"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/77822"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}