{"id":7267,"date":"2024-03-19T06:47:12","date_gmt":"2024-03-19T06:47:12","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7267"},"modified":"2024-03-19T06:47:12","modified_gmt":"2024-03-19T06:47:12","slug":"default-arguments-in-python","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/default-arguments-in-python\/","title":{"rendered":"Default arguments in Python"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-default-value\">What is a default value?<\/a><\/li><li ><a href=\"#what-are-the-examples-of-python-functions\">What are the examples of Python Functions?<\/a><ul><li ><a href=\"#example-1-calling-function-without-the-keyword-arguments\">Example 1: Calling Function without the Keyword arguments<\/a><\/li><li ><a href=\"#example-2-calling-function-with-keyword-arguments\">Example 2: Calling Function with Keyword arguments<\/a><\/li><li ><a href=\"#example-3-some-invalid-function-calls\">Example 3:  Some Invalid Function calls<\/a><\/li><\/ul><\/li><li ><a href=\"#default-argument-values-in-python\">\u00a0Default argument values in Python<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#default-arguments-in-python-fa-qs\">Default arguments in Python- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python will enable the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-variables\/\" data-type=\"post\" data-id=\"6932\">Python arguments <\/a>to have the <a href=\"https:\/\/www.skillvertex.com\/blog\/how-can-i-return-multiple-values-from-a-function\/\" data-type=\"post\" data-id=\"2071\">default values<\/a>. Whereas, if the function is defined without an argument. Then the argument will have a default value. Read this article to learn more about Default arguments in<a href=\"https:\/\/www.skillvertex.com\/blog\/python-interview-questions\/\" data-type=\"post\" data-id=\"3614\"> Python<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-default-value\">What is a default value?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-introduction\/\" data-type=\"post\" data-id=\"4304\">Python <\/a>has several ways of showing<a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\"> syntax<\/a> and the default value for function arguments. The default value will represent the function argument that has the value.<\/p>\n\n\n\n<p>However, if no argument value can be passed during the function call. Then the default value will be provided   with the help of the <a href=\"https:\/\/www.skillvertex.com\/blog\/assignment-operators-in-python\/\" data-type=\"post\" data-id=\"6970\">assignment<\/a> (=) operator of the form <a href=\"https:\/\/www.skillvertex.com\/blog\/generics-keyword-in-c\/\" data-type=\"post\" data-id=\"3708\">keywordname<\/a>=value<\/p>\n\n\n\n<p>Check out the example provided below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>student(firstname, lastname ='Mark', standard ='Fifth'):\n \n     print(firstname, lastname, 'studies in', standard, 'Standard')<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>John Mark studies in Fifth Standard\nJane Smith studies in Fifth Standard\nBob Johnson studies in Sixth Standard\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-examples-of-python-functions\">What are the examples of Python Functions?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1-calling-function-without-the-keyword-arguments\">Example 1: Calling Function without the Keyword arguments<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def student(firstname, lastname='Mark', standard='Fifth'):\n    print(firstname, lastname, 'studies in', standard, 'Standard')\n\n# Call the function with different combinations of arguments\nstudent(\"John\")\nstudent(\"Jane\", \"Smith\")\nstudent(\"Bob\", \"Johnson\", \"Sixth\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>John Mark studies in Fifth Standard\nJane Smith studies in Fifth Standard\nBob Johnson studies in Sixth Standard\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2-calling-function-with-keyword-arguments\">Example 2: Calling Function with Keyword arguments<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def student(firstname, lastname='Mark', standard='Fifth'):\n    print(firstname, lastname, 'studies in', standard, 'Standard')\n\n# Call the function with keyword arguments\nstudent(firstname=\"Alice\", standard=\"Sixth\")\nstudent(firstname=\"Charlie\", lastname=\"Brown\", standard=\"Fourth\")\nstudent(firstname=\"Eva\", lastname=\"Miller\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Alice Mark studies in Sixth Standard\nCharlie Brown studies in Fourth Standard\nEva Miller studies in Fifth Standard\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3-some-invalid-function-calls\">Example 3:  Some Invalid Function calls<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def student(firstname, lastname='Mark', standard='Fifth'):\n    print(firstname, lastname, 'studies in', standard, 'Standard')\n\n# Invalid call: Missing required argument 'firstname'\n# student()\n\n# Invalid call: Providing a non-string value for 'lastname'\n# student(\"John\", 123)\n\n# Invalid call: Unknown keyword argument 'grade'\n# student(firstname=\"Alice\", grade=\"A\")\n\n# Invalid call: Providing too many positional arguments\n# student(\"Bob\", \"Johnson\", \"Sixth\", \"ExtraArgument\")\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default-argument-values-in-python\">&nbsp;Default argument values in Python<\/h2>\n\n\n\n<p>In Python, when you create a function with default values, those values are set when the function is defined. However, if the default value is a list or another mutable object, it can cause unexpected behavior<\/p>\n\n\n\n<p>Let us look into the example given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_fruit(fruit, basket=&#91;]):\n    basket.append(fruit)\n    return basket\n\n# Example calls\nprint(add_fruit(\"apple\"))   # Output: &#91;'apple']\nprint(add_fruit(\"orange\"))  # Output: &#91;'apple', 'orange']\nprint(add_fruit(\"banana\"))  # Output: &#91;'apple', 'orange', 'banana']\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_fruit(fruit, basket=None):\n    if basket is None:\n        basket = &#91;]\n    basket.append(fruit)\n    return basket\n\n# Example calls\nprint(add_fruit(\"apple\"))   # Output: &#91;'apple']\nprint(add_fruit(\"orange\"))  # Output: &#91;'orange']\nprint(add_fruit(\"banana\"))  # Output: &#91;'banana']\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In conclusion, a Python function is like a mini-program within your code that performs a specific task when called. It takes input values, processes them, and can return a result. Functions are handy for organizing code, making it more readable, and allowing you to reuse the same functionality in different parts of your program. <\/p>\n\n\n\n<p>They can have default values for some or all of their parameters, making them flexible and easy to use. Understanding how to define and call functions is a fundamental skill in Python programming that helps make your code efficient and modular.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default-arguments-in-python-fa-qs\">Default arguments in Python- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1708429372532\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What are the default arguments in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Default arguments are the values that are given while defining functions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708429383032\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the default input in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The input will return the string.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708429392480\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What are the 4 types of arguments in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Default arguments, Keyword arguments (named arguments), Positional arguments and <br \/>Arbitrary arguments are the 4 types of argument in Python.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python will enable the Python arguments to have the default values. Whereas, if the function is defined without an argument. Then the argument will have a default value. Read this article to learn more about Default arguments in Python. What is a default value? Python has several ways of showing syntax and the default value &#8230; <a title=\"Default arguments in Python\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/default-arguments-in-python\/\" aria-label=\"More on Default arguments in Python\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7270,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[928,931,929,57,866],"class_list":["post-7267","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-default-argument","tag-default-value","tag-keyword-arguments","tag-python","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7267"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7267\/revisions"}],"predecessor-version":[{"id":8273,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7267\/revisions\/8273"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7270"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}