<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohammed Mokhtar</title>
    <description>The latest articles on DEV Community by Mohammed Mokhtar (@mohammedmokhtar2).</description>
    <link>https://dev.to/mohammedmokhtar2</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2126297%2F69bc023c-567f-4069-9bc0-c0343f4af130.jpg</url>
      <title>DEV Community: Mohammed Mokhtar</title>
      <link>https://dev.to/mohammedmokhtar2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohammedmokhtar2"/>
    <language>en</language>
    <item>
      <title>The difference between “ *args and **kwargs “ in our Python functions</title>
      <dc:creator>Mohammed Mokhtar</dc:creator>
      <pubDate>Wed, 25 Sep 2024 20:46:09 +0000</pubDate>
      <link>https://dev.to/mohammedmokhtar2/the-difference-between-args-and-kwargs-in-our-python-functions-4ee7</link>
      <guid>https://dev.to/mohammedmokhtar2/the-difference-between-args-and-kwargs-in-our-python-functions-4ee7</guid>
      <description>&lt;p&gt;Let's start with the normal functions, for which we know what we are sending in our main body to the integrated function. For example, we are going to send 3 variables and print them in our function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def func_print( x , y , z ): #we have created a function which receives 3 variables 
   print( x )
   print( y )
   print( z )

func_print("mohammed" , 39 , ['id ', 10235 , "name"]) # we sent string , integer, and list 

"""
output will be mohammed
               39
               ['id ', 10235 , "name"]
"""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Now what will happen if we send another variable to our function?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu6mrwxu3xfz6n4ngc61e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu6mrwxu3xfz6n4ngc61e.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
So to fix that, we will use “*args“ instead of multiple variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def func_print( *args ): #we have created a function which receives 3 variables 
   for y in args :
    print(y)

func_print("mohammed" , 39 , ['id ', 10235 , "name"] , 90 ) # we sent string , integer, and list 

"""
output will be mohammed
               39
               ['id ', 10235 , "name"]
               90
"""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9bube3gndpcoblisojzt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9bube3gndpcoblisojzt.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;But what does “*args“ actually make?!&lt;/strong&gt;&lt;br&gt;
*args allows you to pass a variable number of positional arguments to a function.&lt;/p&gt;

&lt;p&gt;These arguments can be integers, strings, lists, dictionaries, or any other data type.&lt;/p&gt;

&lt;p&gt;Internally, it collects the arguments passed into a tuple, regardless of the type of the arguments. That is why we have used a for loop to print our data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But hold on, what should we do if we want to send keyword arguments?!🤔&lt;/strong&gt;&lt;br&gt;
“**kwargs” will be the solution&lt;/p&gt;

&lt;p&gt;“**kwargs” captures keyword arguments as a dictionary, where the keys are the argument names and the values are the corresponding argument values.&lt;/p&gt;

&lt;p&gt;This is useful when you want to handle a varying number of named arguments.&lt;/p&gt;

&lt;p&gt;let’s say we want to send key-value variables just like dictionary&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def func_print( **kwargs ): 
   for x , y in kwargs.items() :
    print(f"The key is {x} while the value is {y}")


func_print(location="Borneo", cause="Illegal logging", area="National Park" )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtky8vh7dhk98xst75rk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtky8vh7dhk98xst75rk.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you see, they save those values in a dictionary.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vxwrj5d9t49wi0juaip.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vxwrj5d9t49wi0juaip.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We have a problem; we can’t send number of dictionaries to function as it will raise an error. So what is the solution?!&lt;/strong&gt;&lt;br&gt;
It’s simple; we have to place ** before the dictionary name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftbq4cwjicji0rv2q0vnm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftbq4cwjicji0rv2q0vnm.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Let’s say we want to use Positional arguments &amp;amp; Keyword arguments together&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def func_print(*args , **kwargs ): 
   ## args part
    print("__________________Args Shape________________________")
    print(args)                                                     # Tuple
    print("__________________Args Values________________________")
    for x in arguments:
      print(x)
  ## kwards part
    print("__________________Kwargs Shape________________________")
    print(kwargs)                                                   # Dictionary
    print("__________________Kwargs Values _____________________")
    for x , y in kwargs.items() :
       print(f"The key is {x} while the value is {y}")

dict_new = dict(location="Borneo", cause="Illegal logging", area="National Park" )
func_print(  "Loss of biodiversity", "Climate change" , "Disruption of ecosystems" , name = "Ahmed," , **dict_new  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxfg56h18kcenb88uzex.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxfg56h18kcenb88uzex.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Finally, in that piece:&lt;br&gt;
The difference between *args &amp;amp; **kwargs is:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;args: Collects positional arguments into a tuple.&lt;br&gt;
**kwargs: Collects keyword arguments into a dictionary.&lt;br&gt;
**kwargs: is designed for keyword arguments (key-value pairs), not for a single positional argument like a dictionary.&lt;br&gt;
You need to use the *&lt;/em&gt; operator to unpack the dictionary into keyword arguments, which **kwargs can accept.&lt;br&gt;
You can use both together in a function to handle a flexible number of both positional and keyword arguments.&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/mohammedmokhtar2/" rel="noopener noreferrer"&gt;LinkedIN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="mailto:muhammedmukhtar822@gmail.com"&gt;muhammedmukhtar822@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
