{"id":4916,"date":"2025-03-17T07:47:12","date_gmt":"2025-03-17T07:47:12","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=4916"},"modified":"2025-07-22T06:56:39","modified_gmt":"2025-07-22T06:56:39","slug":"python-double-asterisk-explained","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/python-double-asterisk-explained\/","title":{"rendered":"Python Double Asterisk (**) Explained: Understanding Keyword Arguments in Functions \ud83d\ude80"},"content":{"rendered":"<h2><strong>Ever Wondered What ** in python<\/strong>\u00a0Does? \ud83e\udd14**<\/h2>\n<p>If you\u2019ve been coding in <a href=\"https:\/\/www.kaashivinfotech.com\/python-course\/\">Python<\/a> for a while, you\u2019ve probably encountered the python double asterisk (<code class=\"\" data-line=\"\">**<\/code>) in function arguments. Maybe you\u2019ve seen <code class=\"\" data-line=\"\">**kwargs<\/code> and thought, <em>What is this magic?<\/em> Or perhaps, you tried using it and got an error. Trust me, I\u2019ve been there! Today, we\u2019re diving deep into the <strong><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/what-is-python-language\/\">Python<\/a> double asterisk<\/strong>, breaking it down with real-life examples and a sprinkle of fun! \ud83d\ude09<\/p>\n<h2><strong>\ud83d\udd11 Key Highlights<\/strong><\/h2>\n<ul>\n<li><strong>Python double asterisk (<code class=\"\" data-line=\"\">**<\/code>) handles keyword arguments in functions.<\/strong><\/li>\n<li><strong>It collects multiple keyword arguments into a dictionary.<\/strong><\/li>\n<li><strong>You can unpack dictionaries using <code class=\"\" data-line=\"\">**<\/code> when passing arguments.<\/strong><\/li>\n<li><strong>It\u2019s super handy for flexible function definitions!<\/strong><\/li>\n<li><strong>We\u2019ll cover everything with easy-to-follow examples!<\/strong><\/li>\n<\/ul>\n<hr \/>\n<h2><strong>What is Python Double Asterisk (<code class=\"\" data-line=\"\">**<\/code>)?<\/strong><\/h2>\n<figure id=\"attachment_8798\" aria-describedby=\"caption-attachment-8798\" style=\"width: 680px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-8798\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk.png\" alt=\"** in python\" width=\"680\" height=\"383\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk.png 1400w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-300x169.png 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-1024x576.png 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-768x432.png 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-332x187.png 332w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-664x374.png 664w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-688x387.png 688w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-1376x774.png 1376w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2025\/03\/Python-Double-Asterisk-1044x588.png 1044w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><figcaption id=\"caption-attachment-8798\" class=\"wp-caption-text\">Python Double Asterisk<\/figcaption><\/figure>\n<p>Python&#8217;s double asterisk (<code class=\"\" data-line=\"\">**<\/code>) is a powerful tool used in function arguments. Unlike a single asterisk (<code class=\"\" data-line=\"\">*args<\/code>), which gathers positional arguments into a tuple, <code class=\"\" data-line=\"\">**kwargs<\/code> collects <strong>keyword arguments into a <a href=\"https:\/\/www.wikitechy.com\/dictionary-in-python\/\" target=\"_blank\" rel=\"noopener\">dictionary<\/a><\/strong>.<\/p>\n<h3><strong>Quick Example:<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">def greet(**kwargs):\n    for key, value in kwargs.items():\n        print(f&quot;{key}: {value}&quot;)\n\ngreet(name=&quot;Alice&quot;, age=25, city=&quot;New York&quot;)\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code class=\"\" data-line=\"\">name: Alice\nage: 25\ncity: New York\n<\/code><\/pre>\n<p><strong>Boom! \ud83d\ude80<\/strong> Python automatically converts the arguments into a dictionary!<\/p>\n<hr \/>\n<h2><strong>Why Use Double Asterisk (<code class=\"\" data-line=\"\">**<\/code>)? \ud83e\udd37\u200d\u2642\ufe0f<\/strong><\/h2>\n<p>Using <code class=\"\" data-line=\"\">**kwargs<\/code> in functions makes your code <strong>more flexible<\/strong> and <strong>future-proof<\/strong>. Here\u2019s why you should love it:<\/p>\n<ul>\n<li><strong>Allows multiple keyword arguments<\/strong> without knowing them beforehand.<\/li>\n<li><strong>Prevents errors<\/strong> when adding new parameters in functions.<\/li>\n<li><strong>Helps with function overloading<\/strong>, making one function handle various cases.<\/li>\n<li><strong>Improves code readability<\/strong> by clearly defining keyword-based parameters.<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>Double Asterisk in Function Arguments \ud83c\udfd7\ufe0f<\/strong><\/h2>\n<p>Let\u2019s go deeper! When defining a function, <code class=\"\" data-line=\"\">**<\/code> lets us accept <strong>any number of keyword arguments<\/strong>.<\/p>\n<h3><strong>Example 1: Using <code class=\"\" data-line=\"\">**kwargs<\/code> to Accept Dynamic Arguments<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">def profile(**info):\n    return info\n\nuser = profile(name=&quot;John&quot;, age=30, profession=&quot;Developer&quot;)\nprint(user)\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code class=\"\" data-line=\"\">{&#039;name&#039;: &#039;John&#039;, &#039;age&#039;: 30, &#039;profession&#039;: &#039;Developer&#039;}\n<\/code><\/pre>\n<p>\u27a1\ufe0f Here, <code class=\"\" data-line=\"\">**info<\/code> captures all keyword arguments and stores them in a dictionary!<\/p>\n<hr \/>\n<h2><strong>Unpacking Dictionaries Using <code class=\"\" data-line=\"\">**<\/code> \ud83c\udfc6<\/strong><\/h2>\n<p>Not only can we <strong>collect<\/strong> keyword arguments using <code class=\"\" data-line=\"\">**kwargs<\/code>, but we can also <strong>unpack<\/strong> dictionaries when passing arguments! \ud83e\udd2f<\/p>\n<h3><strong>Example 2: Unpacking a Dictionary into Function Arguments<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">def introduce(name, age, city):\n    print(f&quot;Hello, I&#039;m {name}, {age} years old from {city}.&quot;)\n\ndata = {&quot;name&quot;: &quot;Emma&quot;, &quot;age&quot;: 22, &quot;city&quot;: &quot;Boston&quot;}\nintroduce(**data)\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code class=\"\" data-line=\"\">Hello, I&#039;m Emma, 22 years old from Boston.\n<\/code><\/pre>\n<p>\u27a1\ufe0f Here, <code class=\"\" data-line=\"\">**data<\/code> unpacks the dictionary, passing values as keyword arguments.<\/p>\n<hr \/>\n<h2><strong>Mixing <code class=\"\" data-line=\"\">*args<\/code> and <code class=\"\" data-line=\"\">**kwargs<\/code> in the Same Function<\/strong><\/h2>\n<p>Yes, you can use both! This allows a function to handle both positional and keyword arguments.<\/p>\n<h3><strong>Example 3: Combining <code class=\"\" data-line=\"\">*args<\/code> and <code class=\"\" data-line=\"\">**kwargs<\/code><\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">def order_food(order_type, *items, **extras):\n    print(f&quot;Order Type: {order_type}&quot;)\n    print(&quot;Items:&quot;, items)\n    print(&quot;Extras:&quot;, extras)\n\norder_food(&quot;Takeaway&quot;, &quot;Burger&quot;, &quot;Fries&quot;, sauce=&quot;Ketchup&quot;, drink=&quot;Coke&quot;)\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code class=\"\" data-line=\"\">Order Type: Takeaway\nItems: (&#039;Burger&#039;, &#039;Fries&#039;)\nExtras: {&#039;sauce&#039;: &#039;Ketchup&#039;, &#039;drink&#039;: &#039;Coke&#039;}\n<\/code><\/pre>\n<p>\u27a1\ufe0f <code class=\"\" data-line=\"\">*items<\/code> collects positional arguments into a tuple, while <code class=\"\" data-line=\"\">**extras<\/code> stores keyword arguments in a dictionary. Super useful, right? \ud83c\udf89<\/p>\n<hr \/>\n<h2><strong>Common Mistakes with <code class=\"\" data-line=\"\">**kwargs<\/code> \u274c<\/strong><\/h2>\n<h3><strong>1. Mixing Positional Arguments Incorrectly<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">def example(**kwargs, name):  # \u274c Incorrect\n    print(name)\n<\/code><\/pre>\n<p>\u26d4 This will raise a <strong>SyntaxError<\/strong> because <code class=\"\" data-line=\"\">**kwargs<\/code> must always be <strong>the last parameter<\/strong>.<\/p>\n<p>\u2705 <strong>Corrected version:<\/strong><\/p>\n<pre><code class=\"language-python\" data-line=\"\">def example(name, **kwargs):  # \u2705 Correct\n    print(name)\n<\/code><\/pre>\n<h3><strong>2. Forgetting to Unpack a Dictionary<\/strong><\/h3>\n<pre><code class=\"language-python\" data-line=\"\">def calculate_price(price, tax):\n    return price + (price * tax)\n\ndata = {&quot;price&quot;: 100, &quot;tax&quot;: 0.08}\nprint(calculate_price(**data))  # \u2705 Correct\n<\/code><\/pre>\n<p>\u27a1\ufe0f Without <code class=\"\" data-line=\"\">**data<\/code>, you\u2019d need to manually pass each key-value pair.<\/p>\n<hr \/>\n<h2><strong>Final Thoughts: Why Python **(double asterisk) is a Game-Changer \ud83c\udfaf<\/strong><\/h2>\n<p>Mastering the <strong>Python double asterisk (<code class=\"\" data-line=\"\">**<\/code>)<\/strong> can make your code <strong>cleaner, more readable, and flexible<\/strong>. Whether you\u2019re handling <strong>dynamic arguments<\/strong> or <strong>unpacking dictionaries<\/strong>, <code class=\"\" data-line=\"\">**kwargs<\/code> is an essential tool in a Python developer\u2019s arsenal! \ud83d\udca1<\/p>\n<h3><strong>Recap:<\/strong><\/h3>\n<p>\u2705 <code class=\"\" data-line=\"\">**kwargs<\/code> collects <strong>multiple keyword arguments<\/strong> as a dictionary.<br \/>\n\u2705 You can <strong>unpack dictionaries<\/strong> into function arguments using <code class=\"\" data-line=\"\">**<\/code>.<br \/>\n\u2705 <code class=\"\" data-line=\"\">*args<\/code> and <code class=\"\" data-line=\"\">**kwargs<\/code> can be used <strong>together<\/strong> for flexibility.<br \/>\n\u2705 Always <strong>place <code class=\"\" data-line=\"\">**kwargs<\/code> at the end<\/strong> of function parameters.<\/p>\n<p>\ud83d\ude80 <strong>Now go try it out in your Python projects!<\/strong> If you have any questions, drop them in the comments. Let\u2019s discuss! \ud83d\udcac\ud83d\udd25<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever Wondered What ** in python\u00a0Does? \ud83e\udd14** If you\u2019ve been coding in Python for a while, you\u2019ve probably encountered the python double asterisk (**) in function arguments. Maybe you\u2019ve seen **kwargs and thought, What is this magic? Or perhaps, you tried using it and got an error. Trust me, I\u2019ve been there! Today, we\u2019re diving [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":4919,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3236,3203],"tags":[3518,3510,3520,3512,3513,3514,3517,3521,772,3511,3515,3519,3509,1253,3516],"class_list":["post-4916","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-programming","tag-args-and-kwargs-in-python","tag-args-and-kwargs-in-python-explained","tag-arbitrary-keyword-arguments-in-python","tag-args-and-kwargs-in-python-3","tag-functions-in-python","tag-how-to-use-functions-in-python","tag-keyword-arguments-in-python","tag-passing-any-number-of-keyword-arguments-to-a-function-in-python","tag-python","tag-python-arguments","tag-python-arguments-in-functions","tag-python-asterisk-function-argument","tag-python-functions","tag-python-tutorial","tag-what-does-the-double-asterisk-operator-mean-in-python"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/4916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=4916"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/4916\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/4919"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=4916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=4916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=4916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}