<?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: Sweta Shaw</title>
    <description>The latest articles on DEV Community by Sweta Shaw (@sweta_shaw).</description>
    <link>https://dev.to/sweta_shaw</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%2F621817%2F1d883184-8719-47cf-96a9-1fd674b66a4c.jpg</url>
      <title>DEV Community: Sweta Shaw</title>
      <link>https://dev.to/sweta_shaw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sweta_shaw"/>
    <language>en</language>
    <item>
      <title>Python Variables and Memory Management</title>
      <dc:creator>Sweta Shaw</dc:creator>
      <pubDate>Thu, 06 May 2021 08:18:54 +0000</pubDate>
      <link>https://dev.to/sweta_shaw/python-variables-and-memory-management-9bm</link>
      <guid>https://dev.to/sweta_shaw/python-variables-and-memory-management-9bm</guid>
      <description>&lt;h1&gt;
  
  
  Variable
&lt;/h1&gt;

&lt;p&gt;Variable is a way to refer to memory locations being accessed by a computer program. In languages like C, C++, Java, a variable is a name given to a memory location. So when we say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int x;
float y;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It asks the compiler to create memory space for two variables, 4 bytes each (size depends on the compiler and programming language). Variables are basically names assigned to the memory locations and if the variable is of type integer only integer values can be stored in it.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620278475715%2FPwxYlz4bD.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620278475715%2FPwxYlz4bD.png" alt="c_x.png"&gt;&lt;/a&gt;&lt;br&gt;
But in python, we do not need to declare a variable beforehand like &lt;code&gt;int x&lt;/code&gt;, we can straightaway move to define the variable and start using it like &lt;code&gt;x = 10&lt;/code&gt;. &lt;br&gt;
&lt;strong&gt;So how does Python know what is the type of the variable and how to access it ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Python, everything (integer, list, string, functions, etc. ) is an object and every object has a unique id that is assigned to it when the object is created. Variables are references (pointers) to these objects. What happens when we say :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Python executes the right-hand side of the code&lt;/li&gt;
&lt;li&gt;Identifies 10 to be an integer&lt;/li&gt;
&lt;li&gt;Creates an object of Integer class&lt;/li&gt;
&lt;li&gt;Assigns &lt;code&gt;x&lt;/code&gt; to refer the integer object 10&lt;/li&gt;
&lt;/ul&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620229572746%2F6HB3asCAl.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620229572746%2F6HB3asCAl.png" alt="variable.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we create another variable &lt;code&gt;y&lt;/code&gt; which is equal to &lt;code&gt;x&lt;/code&gt;, what do you think happens internally?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y = x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;y&lt;/code&gt; now refers to the same object as &lt;code&gt;x&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620234533788%2Fr8xAr_zZy.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620234533788%2Fr8xAr_zZy.png" alt="x_and_y.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do we know that?&lt;/strong&gt;&lt;br&gt;
It can be verified by checking the id of the object that these two variables &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are pointing to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id(y) == id(x)
# output
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also print the id of the object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(f"id of the object pointed by x:{id(x)}")
print(f"id of the object pointed by y:{id(y)}") 

# output
#your id value might be different from mine but the two ids printed must be same
id of the object pointed by x:140733799282752 
id of the object pointed by y:140733799282752
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, what if we assign a different value to &lt;code&gt;y&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y = 40
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create another integer object with the value 40 and &lt;code&gt;y&lt;/code&gt; will now refer to 40 instead of 10.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620236608813%2FAx4Wtgn2J.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620236608813%2FAx4Wtgn2J.png" alt="x_and_y_diff.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we check?&lt;/strong&gt;&lt;br&gt;
Again, we will use &lt;code&gt;id()&lt;/code&gt; to see the ids of the objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(x) # outputs 10
print(y) # outputs 40
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(f"id of the object pointed by x:{id(x)}")
print(f"id of the object pointed by y:{id(y)}")  

# output
id of the object pointed by x:140733799282752
id of the object pointed by y:140733799283712
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see from above, the two ids are now different which means a new object with value 40 is created and &lt;code&gt;y&lt;/code&gt; refers to that object now.&lt;/p&gt;

&lt;p&gt;If we wish to assign some other value to variable &lt;code&gt;x&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = "python"
print(id(x))

# output
1783541430128
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will get an object id that is different from the id "140733799282752" printed by &lt;code&gt;id(x)&lt;/code&gt; for integer object &lt;code&gt;10&lt;/code&gt; previously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what happened to the integer object &lt;code&gt;10&lt;/code&gt;?&lt;/strong&gt; The integer object 10 now remains unreferenced and can no longer be accessed. Python has a smart garbage collection system that looks out for objects that are no longer accessible and reclaims the memory so that it can be used for some other purpose.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620238788124%2F1zB6g7FmK.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620238788124%2F1zB6g7FmK.png" alt="xy.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One last question: What if we create two integer objects with the same value?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p = 100
q = 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before I answer this question. Let's check their ids:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(f"id of the object pointed by x:{id(p)}")
print(f"id of the object pointed by y:{id(q)}") 
# output
id of object pointed by x:140733799285632
id of object pointed by y:140733799285632
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what python does is optimize memory allocation by creating a single integer object with value 100 and then points both the variables &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;q&lt;/code&gt; towards it.&lt;/p&gt;

&lt;p&gt;Let's check one more example-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m = 350
n = 350

print(f"id of object pointed by x:{id(m)}")
print(f"id of object pointed by y:{id(n)}") 

# output
id of object pointed by x:1783572881872
id of object pointed by y:1783572881936
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To our surprise the ids of the integer object 300 referred to by &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are different. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is going on here??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python allocates memory for integers in the range [-5, 256] at startup, i.e., integer objects for these values are created and ids are assigned. This process is also known as &lt;strong&gt;integer caching&lt;/strong&gt;. So whenever an integer is referenced in this range, python variables point to the cached value of that object. That is why the object id for integer object 100 referenced by &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;q&lt;/code&gt; print the same ids. For integers outside the range [-5, 256], their objects are created as and when they are defined during program implementation. This is the reason why the ids for integer object 350 referenced by &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are different.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, variables point to the objects stored in memory. Hence variables are free to point to object of any type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 10 # x points to an object of 'int' type
x = ["python", 20, "apple"] # x now points to an object of type 'list'
x = "python" # x now points to a string type object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we have come to the end of this short article on the inner working of variables. I hope you enjoyed reading it. Feel free to suggest improvements to the article or any other topic that you would like to know more about.&lt;/p&gt;

&lt;p&gt;I have extensively used the concept of &lt;strong&gt;f-strings&lt;/strong&gt; in my &lt;code&gt;print&lt;/code&gt; statements. You can know more about it from  &lt;a href="https://swetashaw.hashnode.dev/formatted-string-literals-in-python" rel="noopener noreferrer"&gt;this article.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Connect with me on  &lt;a href="https://www.linkedin.com/in/sweta-shaw-972154a8/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; &lt;br&gt;
 &lt;a href="https://www.instagram.com/sweta.shaw__" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;  &lt;a href="https://github.com/swetshaw" rel="noopener noreferrer"&gt;Github&lt;/a&gt; ❤️&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Formatted String Literals in Python</title>
      <dc:creator>Sweta Shaw</dc:creator>
      <pubDate>Thu, 29 Apr 2021 20:22:09 +0000</pubDate>
      <link>https://dev.to/sweta_shaw/formatted-string-literals-in-python-6o1</link>
      <guid>https://dev.to/sweta_shaw/formatted-string-literals-in-python-6o1</guid>
      <description>&lt;h2&gt;
  
  
  Old-school  way of formatting strings &lt;code&gt;str.format()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The Python &lt;code&gt;string .format()&lt;/code&gt; method was introduced in version 2.6. Following is an example that uses &lt;code&gt;.format()&lt;/code&gt; method of formatting strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Example 1 (simple)
name = "Sweta Shaw"
print("My name is {}".format(name))

#output
My name is Sweta Shaw
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code looks neat but the real struggle happens when the number of variables increases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Example 2 (complex)
first_name = "Sweta"
last_name = "Shaw"
profession = "software developer"
platform = "hashnode"

print("Hi! I am {first_name} {last_name}, a {profession} by profession currently writing this article on {platform}.".format(first_name= first_name, last_name =last_name , profession = profession, platform = platform))

# output
Hi! I am Sweta Shaw, a software developer by profession currently writing this article on hashnode.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the &lt;strong&gt;f-string&lt;/strong&gt; literals come to the rescue. It was introduced in Python 3.6&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;&lt;em&gt;formatted string literal&lt;/em&gt; *&lt;em&gt;or *&lt;/em&gt;*f-string&lt;/strong&gt;* is a string literal that is prefixed with &lt;code&gt;f&lt;/code&gt; or &lt;code&gt;F&lt;/code&gt; and curly braces containing expressions that will be replaced with their values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Example 1 using f-string
name = "Sweta"
print(f"My name is {name}")

# output
My name is Sweta Shaw
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see how we can write the complex example 2 using f-strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Example 2 using f-string
first_name = "Sweta"
last_name = "Shaw"
profession = "software developer"
platform = "hashnode"

print(f"Hi! I am {first_name} {last_name}, a {profession} by profession currently writing this article on {platform}.")

# output
Hi! I am Sweta Shaw, a software developer by profession currently writing this article on hashnode.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also perform arithmetic operations using f-string literals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f"{2 * 37}"
# output 
'74'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = 20
b = 50

print(f"The smaller number between {a} and {b} is {a if a &amp;lt; b else b}")

# output
The smaller number between 20 and 50 is 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Printing a list
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;authors = [("id", "author", "book"), ("101", "Changing India", "Dr. Manmohan Singh"), ("102","War and Peace", "Leo Tolstoy "), ("103", "Emma", "Jane Austen")]
for id, author,book in authors:
    print(f"{id} {author} {book}")

# output
id author book
101 Changing India Dr. Manmohan Singh
102 War and Peace Leo Tolstoy 
103 Emma Jane Austen

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Beautify this output using f-string indentation
&lt;/h3&gt;

&lt;p&gt;This is done by mentioning the number of spaces taken by each variable. Here 'id' has been assigned 5 spaces and author and book 20 spaces each.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;authors = [("id", "author", "book"), ("101", "Changing India", "Dr. Manmohan Singh"), ("102","War and Peace", "Leo Tolstoy "), ("103", "Emma", "Jane Austen")]
for id, author,book in authors:
    print(f"{id:{5}} {author:{20}} {book}")

# output:
id    author               book
101   Changing India       Dr. Manmohan Singh
102   War and Peace        Leo Tolstoy 
103   Emma                 Jane Austen


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read more about f-string literals &lt;a href="https://www.python.org/dev/peps/pep-0498/#abstract"&gt;here&lt;/a&gt; &lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
