{"id":51,"date":"2024-01-24T14:20:35","date_gmt":"2024-01-24T14:20:35","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=51"},"modified":"2024-01-24T14:20:36","modified_gmt":"2024-01-24T14:20:36","slug":"python-default-parameters","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-default-parameters\/","title":{"rendered":"Python Default Parameters"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python default parameters to simplify function calls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python default parameters<\/h2>\n\n\n\n<p>When you\u00a0define a function, you can specify a default value for each parameter.<\/p>\n\n\n\n<p>To specify default values for parameters, you use the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def function_name(param1, param2=value2, param3=value3, ...):<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax, you specify default values (<code>value2<\/code>,&nbsp;<code>value3<\/code>, \u2026) for each parameter using the assignment operator (<code>=)<\/code>.<\/p>\n\n\n\n<p>When you call a function and pass an argument to the parameter that has a default value, the function will use that argument instead of the default value.<\/p>\n\n\n\n<p>However, if you don\u2019t pass the argument, the function will use the default value.<\/p>\n\n\n\n<p>To use default parameters, you need to place parameters with the default values after other parameters. Otherwise, you\u2019ll get a syntax error.<\/p>\n\n\n\n<p>For example, you cannot do something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def function_name(param1=value1, param2, param3):<\/code><\/code><\/pre>\n\n\n\n<p>This causes a syntax error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python default parameters example<\/h2>\n\n\n\n<p>The following example defines the\u00a0<code>greet()<\/code>\u00a0function that returns a greeting message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name, message='Hi'): return f\"{message} {name}\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>greet()<\/code>&nbsp;function has two parameters:&nbsp;<code>name<\/code>&nbsp;and&nbsp;<code>message<\/code>. And the&nbsp;<code>message<\/code>&nbsp;parameter has a default value of&nbsp;<code>'Hi'<\/code>.<\/p>\n\n\n\n<p>The following calls the\u00a0<code>greet()<\/code>\u00a0function and passes the two arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name, message='Hi'): return f\"{message} {name}\" greeting = greet('John', 'Hello') print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hello John<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since we pass the second argument to the&nbsp;<code>greet()<\/code>&nbsp;function, the function uses the argument instead of the default value.<\/p>\n\n\n\n<p>The following example calls the\u00a0<code>greet()<\/code>\u00a0function without passing the second argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name, message='Hi'): return f\"{message} {name}\" greeting = greet('John') print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hi John<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this case, the&nbsp;<code>greet()<\/code>&nbsp;function uses the default value of the&nbsp;<code>message<\/code>&nbsp;parameter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple default parameters<\/h2>\n\n\n\n<p>The following redefines the\u00a0<code>greet()<\/code>\u00a0function with the two parameters that have default values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name='there', message='Hi'): return f\"{message} {name}\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, you can call the\u00a0<code>greet()<\/code>\u00a0function without passing any parameters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name='there', message='Hi'): return f\"{message} {name}\" greeting = greet() print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hi there<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Suppose that you want the\u00a0<code>greet()<\/code>\u00a0function to return a greeting like\u00a0<code>Hello there<\/code>. You may come up with the following function call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name='there', message='Hi'): return f\"{message} {name}\" greeting = greet('Hello') print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Unfortuntely, it returns an unexpected value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hi Hello<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Because when you pass the&nbsp;<code>'Hello'<\/code>&nbsp;argument, the&nbsp;<code>greet()<\/code>&nbsp;function treats it as the first argument, not the second one.<\/p>\n\n\n\n<p>To resolve this, you need to call the\u00a0<code>greet()<\/code>\u00a0function using\u00a0keyword arguments\u00a0like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def greet(name='there', message='Hi'): return f\"{message} {name}\" greeting = greet(message='Hello') print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hello there<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python default parameters to simplify function calls. Introduction to Python default parameters When you\u00a0define a function, you can specify a default value for each parameter. To specify default values for parameters, you use the following syntax: In this syntax, you specify default values (value2,&nbsp;value3, \u2026) for each [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-4-functions"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":52,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/51\/revisions\/52"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}