{"id":489,"date":"2020-10-13T02:37:57","date_gmt":"2020-10-13T02:37:57","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=489"},"modified":"2025-03-31T02:29:41","modified_gmt":"2025-03-31T02:29:41","slug":"python-keyword-arguments","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-keyword-arguments\/","title":{"rendered":"Python Keyword Arguments"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python keyword arguments, and how to use them to make function calls more obvious.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-keyword-arguments'>Introduction to the Python keyword arguments <a href=\"#introduction-to-the-python-keyword-arguments\" class=\"anchor\" id=\"introduction-to-the-python-keyword-arguments\" title=\"Anchor for Introduction to the Python keyword arguments\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s start with a simple <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">function<\/a> that calculates the net price from the selling price and discount:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, discount)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>get_net_price()<\/code> function has two parameters: <code>price<\/code> and <code>discount<\/code>.<\/p>\n\n\n\n<p>The following shows how to call the <code>get_net_price()<\/code> function to calculate the net price from the price <code>100<\/code> and discount <code>10%<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, discount)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">0.1<\/span>)\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToNCiAgICByZXR1cm4gcHJpY2UgKiAoMS1kaXNjb3VudCkNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgxMDAsIDAuMSkNCnByaW50KGYnJHtuZXRfcHJpY2U6IC4yZn0nKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"> <span class=\"hljs-number\">90.00<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">In this example, we use an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-f-strings\/\" target=\"_blank\" rel=\"noreferrer noopener\">f-string<\/a> to format the output number with two decimal places (<code>f'{net_price: .2f}'<\/code>).<\/p>\n\n\n\n<p>In the <code>get_net_price(100, 0.1)<\/code> function call, we pass each argument as a positional argument. In other words, we pass the <code>price<\/code> argument first and the <code>discount<\/code> argument second.<\/p>\n\n\n\n<p>However, the function call <code>get_net_price(100, 0.1)<\/code> has a readability issue. Because by looking at that function call only, you don&#8217;t know which argument is <code>price<\/code> and which one is the <code>discount<\/code>.<\/p>\n\n\n\n<p>On top of that, when you call the <code>get_net_price()<\/code> function, you need to know the position of each argument. <\/p>\n\n\n\n<p>If you don&#8217;t, the function will calculate the <code>net_price<\/code> incorrectly. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, discount)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(<span class=\"hljs-number\">0.1<\/span>, <span class=\"hljs-number\">100<\/span>)\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToKICAgIHJldHVybiBwcmljZSAqICgxLWRpc2NvdW50KQoKbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgwLjEsIDEwMCkKcHJpbnQobmV0X3ByaWNlKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">-9.9<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To improve the readability, Python introduces keyword arguments.<\/p>\n\n\n\n<p>The following shows the keyword argument syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">fn(parameter1=value1,parameter2=value2)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using the keyword argument syntax, you don&#8217;t need to specify the arguments in the same order as defined in the function. <\/p>\n\n\n\n<p>Therefore, you can call a function by swapping the argument positions like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">fn(parameter2=value2,parameter1=value1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows how to use the keyword argument syntax to call the <code>get_net_price()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, discount)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(\n    price=<span class=\"hljs-number\">100<\/span>, \n    discount=<span class=\"hljs-number\">0.1<\/span>\n)\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToNCiAgICByZXR1cm4gcHJpY2UgKiAoMS1kaXNjb3VudCkNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICBwcmljZT0xMDAsIA0KICAgIGRpc2NvdW50PTAuMQ0KKQ0KDQpwcmludChmJ3tuZXRfcHJpY2U6IC4yZn0nKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Or:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, discount)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(\n    discount=<span class=\"hljs-number\">0.1<\/span>,\n    price=<span class=\"hljs-number\">100<\/span>\n)\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToNCiAgICByZXR1cm4gcHJpY2UgKiAoMS1kaXNjb3VudCkNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICBkaXNjb3VudD0wLjEsDQogICAgcHJpY2U9MTAwDQopDQoNCnByaW50KGYne25ldF9wcmljZTogLjJmfScp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Both of them returns the same result.<\/p>\n\n\n\n<p>It is possible to leave a trailing comma after the last argument:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def get_net_price(price, discount):\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(\n    discount=<span class=\"hljs-number\">0.1<\/span>,\n    price=<span class=\"hljs-number\">100<\/span>,\n)\n\n<span class=\"hljs-keyword\">print<\/span>(f<span class=\"hljs-string\">'{net_price: .2f}'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToNCiAgICByZXR1cm4gcHJpY2UgKiAoMS1kaXNjb3VudCkNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICBkaXNjb3VudD0wLjEsDQogICAgcHJpY2U9MTAwLA0KKQ0KDQpwcmludChmJ3tuZXRfcHJpY2U6IC4yZn0nKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>When you use the keyword arguments, their names that matter, not their positions.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def get_net_price(price, discount):\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(\n    price=<span class=\"hljs-number\">100<\/span>, \n    discount=<span class=\"hljs-number\">0.1<\/span>\n)\n<span class=\"hljs-keyword\">print<\/span>(net_price)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToNCiAgICByZXR1cm4gcHJpY2UgKiAoMS1kaXNjb3VudCkNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICBwcmljZT0xMDAsIA0KICAgIGRpc2NvdW50PTAuMQ0KKQ0KcHJpbnQobmV0X3ByaWNlKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Note that you can call a function by mixing positional and keyword arguments. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, discount)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> price * (<span class=\"hljs-number\">1<\/span>-discount)\n\nnet_price = get_net_price(\n    <span class=\"hljs-number\">100<\/span>, \n    discount=<span class=\"hljs-number\">0.1<\/span>\n)\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIGRpc2NvdW50KToNCiAgICByZXR1cm4gcHJpY2UgKiAoMS1kaXNjb3VudCkNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICAxMDAsIA0KICAgIGRpc2NvdW50PTAuMQ0KKQ0KDQpwcmludChmJ3tuZXRfcHJpY2U6IC4yZn0nKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='keyword-arguments-and-default-parameters'>Keyword arguments and default parameters <a href=\"#keyword-arguments-and-default-parameters\" class=\"anchor\" id=\"keyword-arguments-and-default-parameters\" title=\"Anchor for Keyword arguments and default parameters\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you have the following <code>get_net_price()<\/code> function that calculates the net price from the selling price, tax, and discount.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>get_net_price()<\/code> function, the tax and discount <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-default-parameters\/\">parameters have default values<\/a> of <code>7%<\/code> and <code>5%<\/code> respectively.<\/p>\n\n\n\n<p>The following calls the <code>get_net_price()<\/code> function and uses the default values for <code>tax<\/code> and <code>discount<\/code> parameters:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price\n\nnet_price = get_net_price(<span class=\"hljs-number\">100<\/span>)\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIHRheF9yYXRlPTAuMDcsIGRpc2NvdW50PTAuMDUpOg0KICAgIGRpc2NvdW50ZWRfcHJpY2UgPSBwcmljZSAqICgxIC0gZGlzY291bnQpICANCiAgICBuZXRfcHJpY2UgPSBkaXNjb3VudGVkX3ByaWNlICogKDEgKyB0YXhfcmF0ZSkgIA0KICAgIHJldHVybiBuZXRfcHJpY2UNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgxMDApDQpwcmludChmJ3tuZXRfcHJpY2U6IC4yZn0nKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">101.65<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Suppose that you want to use the default value for the <code>tax<\/code> parameter but not <code>discount<\/code>. The following function call doesn&#8217;t work correctly.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price\n\nnet_price = get_net_price(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">0.06<\/span>)\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIHRheF9yYXRlPTAuMDcsIGRpc2NvdW50PTAuMDUpOg0KICAgIGRpc2NvdW50ZWRfcHJpY2UgPSBwcmljZSAqICgxIC0gZGlzY291bnQpICANCiAgICBuZXRfcHJpY2UgPSBkaXNjb3VudGVkX3ByaWNlICogKDEgKyB0YXhfcmF0ZSkgIA0KICAgIHJldHVybiBuZXRfcHJpY2UNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgxMDAsIDAuMDYpDQpwcmludChmJ3tuZXRfcHJpY2U6IC4yZn0nKQ\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">100<span class=\"hljs-selector-class\">.70<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230; because Python will assign <code>100<\/code> to price and <code>0.1<\/code> to tax, not discount.<\/p>\n\n\n\n<p>To fix this, you must use keyword arguments:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price\n\nnet_price = get_net_price(\n    price=<span class=\"hljs-number\">100<\/span>, \n    discount=<span class=\"hljs-number\">0.06<\/span>\n)\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIHRheF9yYXRlPTAuMDcsIGRpc2NvdW50PTAuMDUpOg0KICAgIGRpc2NvdW50ZWRfcHJpY2UgPSBwcmljZSAqICgxIC0gZGlzY291bnQpICANCiAgICBuZXRfcHJpY2UgPSBkaXNjb3VudGVkX3ByaWNlICogKDEgKyB0YXhfcmF0ZSkgIA0KICAgIHJldHVybiBuZXRfcHJpY2UNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICBwcmljZT0xMDAsIA0KICAgIGRpc2NvdW50PTAuMDYNCikNCnByaW50KGYne25ldF9wcmljZTogLjJmfScp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">100.58<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Or you can mix the positional and keyword arguments:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price\n\nnet_price = get_net_price(\n    <span class=\"hljs-number\">100<\/span>, \n    discount=<span class=\"hljs-number\">0.06<\/span>\n)\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIHRheF9yYXRlPTAuMDcsIGRpc2NvdW50PTAuMDUpOg0KICAgIGRpc2NvdW50ZWRfcHJpY2UgPSBwcmljZSAqICgxIC0gZGlzY291bnQpICANCiAgICBuZXRfcHJpY2UgPSBkaXNjb3VudGVkX3ByaWNlICogKDEgKyB0YXhfcmF0ZSkgIA0KICAgIHJldHVybiBuZXRfcHJpY2UNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICAxMDAsIA0KICAgIGRpc2NvdW50PTAuMDYNCikNCnByaW50KGYne25ldF9wcmljZTogLjJmfScp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">100.58<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-keyword-argument-requirements'>Python keyword argument requirements <a href=\"#python-keyword-argument-requirements\" class=\"anchor\" id=\"python-keyword-argument-requirements\" title=\"Anchor for Python keyword argument requirements\">#<\/a><\/h2>\n\n\n\n<p>Once you use a keyword argument, you need to use keyword arguments for the remaining parameters.<\/p>\n\n\n\n<p>The following will result in an error because it uses the positional argument after a keyword argument:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price\n\nnet_price = get_net_price(\n    <span class=\"hljs-number\">100<\/span>, \n    tax=<span class=\"hljs-number\">0.08<\/span>, \n    <span class=\"hljs-number\">0.06<\/span>\n)\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIHRheF9yYXRlPTAuMDcsIGRpc2NvdW50PTAuMDUpOg0KICAgIGRpc2NvdW50ZWRfcHJpY2UgPSBwcmljZSAqICgxIC0gZGlzY291bnQpICANCiAgICBuZXRfcHJpY2UgPSBkaXNjb3VudGVkX3ByaWNlICogKDEgKyB0YXhfcmF0ZSkgIA0KICAgIHJldHVybiBuZXRfcHJpY2UNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICAxMDAsIA0KICAgIHRheD0wLjA4LCANCiAgICAwLjA2DQopDQoNCnByaW50KGYne25ldF9wcmljZTogLjJmfScp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Shell Session\" data-shcb-language-slug=\"shell\"><span><code class=\"hljs language-shell\">SyntaxError: positional argument follows keyword argument<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Shell Session<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">shell<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To fix this, you need to use the keyword argument for the third argument like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_net_price<\/span><span class=\"hljs-params\">(price, tax_rate=<span class=\"hljs-number\">0.07<\/span>, discount=<span class=\"hljs-number\">0.05<\/span>)<\/span>:<\/span>\n    discounted_price = price * (<span class=\"hljs-number\">1<\/span> - discount)  \n    net_price = discounted_price * (<span class=\"hljs-number\">1<\/span> + tax_rate)  \n    <span class=\"hljs-keyword\">return<\/span> net_price\n\nnet_price = get_net_price(\n    <span class=\"hljs-number\">100<\/span>, \n    tax_rate=<span class=\"hljs-number\">0.08<\/span>, \n    discount=<span class=\"hljs-number\">0.06<\/span>\n)\n\nprint(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{net_price: <span class=\"hljs-number\">.2<\/span>f}<\/span>'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdldF9uZXRfcHJpY2UocHJpY2UsIHRheF9yYXRlPTAuMDcsIGRpc2NvdW50PTAuMDUpOg0KICAgIGRpc2NvdW50ZWRfcHJpY2UgPSBwcmljZSAqICgxIC0gZGlzY291bnQpICANCiAgICBuZXRfcHJpY2UgPSBkaXNjb3VudGVkX3ByaWNlICogKDEgKyB0YXhfcmF0ZSkgIA0KICAgIHJldHVybiBuZXRfcHJpY2UNCg0KbmV0X3ByaWNlID0gZ2V0X25ldF9wcmljZSgNCiAgICAxMDAsIA0KICAgIHRheF9yYXRlPTAuMDgsIA0KICAgIGRpc2NvdW50PTAuMDYNCikNCg0KcHJpbnQoZid7bmV0X3ByaWNlOiAuMmZ9Jyk\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">101.52<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Python keyword arguments to make your function call more readable and obvious, especially for functions that accept many arguments.<\/li>\n\n\n\n<li>All the arguments after the first keyword argument must also be keyword arguments too.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=keyword-arguments\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"489\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-keyword-arguments\/\"\n\t\t\t\tdata-post-title=\"Python Keyword Arguments\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"489\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-keyword-arguments\/\"\n\t\t\t\tdata-post-title=\"Python Keyword Arguments\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python keyword arguments, and how to use them to make function calls more obvious.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":21,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-489","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/489\/revisions"}],"predecessor-version":[{"id":7273,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/489\/revisions\/7273"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}