<?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: Samiksha Srivastav</title>
    <description>The latest articles on DEV Community by Samiksha Srivastav (@samikshasri).</description>
    <link>https://dev.to/samikshasri</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%2F3912250%2Fbd929b4b-28fa-47fa-8a4c-a1093a1484fc.png</url>
      <title>DEV Community: Samiksha Srivastav</title>
      <link>https://dev.to/samikshasri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samikshasri"/>
    <language>en</language>
    <item>
      <title>global and nonlocal in Python (The Concept That Finally Made Scope Click for Me)</title>
      <dc:creator>Samiksha Srivastav</dc:creator>
      <pubDate>Thu, 21 May 2026 16:07:08 +0000</pubDate>
      <link>https://dev.to/samikshasri/global-and-nonlocal-in-python-the-concept-that-finally-made-scope-click-for-me-4kb6</link>
      <guid>https://dev.to/samikshasri/global-and-nonlocal-in-python-the-concept-that-finally-made-scope-click-for-me-4kb6</guid>
      <description>&lt;p&gt;While learning Python scopes and closures, I reached two keywords that looked very similar initially:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;global&lt;/li&gt;
&lt;li&gt;nonlocal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At first, both felt like:&lt;br&gt;
    "Okay... both are just used to access outer variables."&lt;/p&gt;

&lt;p&gt;But once I understand how Python internally handles variable assignment and scope, the difference became much clearer.&lt;/p&gt;

&lt;p&gt;So here's my attempt to explain them in the simplest way possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, Why Does This Error Happen?
&lt;/h2&gt;

&lt;p&gt;Let's start with this example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fomd6ks4q8psc5hmnpthc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fomd6ks4q8psc5hmnpthc.png" alt=" " width="268" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;UnboundLocalError&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first, this feels confusing because: x already exists globally.&lt;br&gt;
So why is Python complaining?&lt;/p&gt;

&lt;h2&gt;
  
  
  What Python Internally Thinks
&lt;/h2&gt;

&lt;p&gt;Whenever Python sees:&lt;/p&gt;

&lt;p&gt;x = something&lt;br&gt;
inside a function, it automatically assumes:&lt;/p&gt;

&lt;p&gt;So internally this becomes:&lt;br&gt;
local x = local x + 1&lt;/p&gt;

&lt;p&gt;And now the problem becomes obvious:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the local x does not exist yet&lt;/li&gt;
&lt;li&gt;but Python is trying to use it first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence: &lt;strong&gt;UnboundLocalError&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter global
&lt;/h2&gt;

&lt;p&gt;Now look at this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fycqa8vgcenkms8mw375s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fycqa8vgcenkms8mw375s.png" alt=" " width="223" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;strong&gt;11&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Did global Actually Do?
&lt;/h2&gt;

&lt;p&gt;global x tells Python:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Do not create a local variable.&lt;br&gt;
    Use the global one instead.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So now the actual global variable gets modified.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Thing Most Beginners Miss&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You do NOT need global to READ a global variable.&lt;/p&gt;

&lt;p&gt;This works perfectly fine:&lt;/p&gt;

&lt;p&gt;**x = 10&lt;/p&gt;

&lt;p&gt;def test():&lt;br&gt;
    print(x)**&lt;/p&gt;

&lt;p&gt;But: &lt;br&gt;
the moment you MODIFY it,&lt;br&gt;
Python tries creating a local variable.&lt;/p&gt;

&lt;p&gt;That’s where global becomes necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now Comes nonlocal
&lt;/h2&gt;

&lt;p&gt;This one confused me even more initially &lt;br&gt;
Because it looks similar to global, but works differently.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fu7boyl32ydhwq8j1bf4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu7boyl32ydhwq8j1bf4s.png" alt=" " width="312" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again: &lt;strong&gt;UnboundLocalError&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Same reason:&lt;br&gt;
Python thinks x is local to inner().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enter nonlocal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flyleh36094xbpyx6whe8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flyleh36094xbpyx6whe8.png" alt=" " width="277" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;strong&gt;11&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What nonlocal Actually Means
&lt;/h2&gt;

&lt;p&gt;nonlocal x tells Python:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This variable belongs to the enclosing function scope.&lt;br&gt;
Do not create a local copy.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So instead of creating a new local variable:&lt;br&gt;
    - Python modifies the outer function’s variable directly.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Core Difference
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;global Targets: &lt;strong&gt;global scope&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;nonlocal Targets: &lt;strong&gt;enclosing function scope&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Visual Understanding&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;x = "global"&lt;/p&gt;

&lt;p&gt;def outer():&lt;/p&gt;

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

def inner():

    z = "inner"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;x --&amp;gt; global scope&lt;/li&gt;
&lt;li&gt;y --&amp;gt; enclosing scope&lt;/li&gt;
&lt;li&gt;z --&amp;gt; local scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;global works with x&lt;/li&gt;
&lt;li&gt;nonlocal works with y&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why nonlocal Matters So Much
&lt;/h2&gt;

&lt;p&gt;Because closures depend on it.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;def counter():&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;count = 0

def increment():

    nonlocal count

    count += 1

    return count

return increment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;c = counter()&lt;/p&gt;

&lt;p&gt;print(c())&lt;br&gt;
print(c())&lt;br&gt;
print(c())&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;1&lt;br&gt;
2&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;The inner function keeps remembering and modifying the outer variable.&lt;/p&gt;

&lt;p&gt;That’s where closures and nonlocal connect beautifully.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One-Line Understanding&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;global --&amp;gt; modifies global scope variable&lt;br&gt;
nonlocal --&amp;gt; modifies enclosing function variable&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Initially, both keywords looked almost identical to me.&lt;/p&gt;

&lt;p&gt;But once I understood:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- how Python creates local variables,
- how assignment works internally,
- and how scopes are connected,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the difference became much more intuitive.&lt;/p&gt;

&lt;p&gt;And honestly, understanding these concepts made closures and Python scopes feel way less “magical” and much more logical.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding Scope and Closures in Python</title>
      <dc:creator>Samiksha Srivastav</dc:creator>
      <pubDate>Thu, 14 May 2026 14:28:40 +0000</pubDate>
      <link>https://dev.to/samikshasri/understanding-scope-and-closures-in-python-1i6</link>
      <guid>https://dev.to/samikshasri/understanding-scope-and-closures-in-python-1i6</guid>
      <description>&lt;p&gt;I was comfortable with Python scopes pretty quickly.&lt;/p&gt;

&lt;p&gt;But when I reached closures, my first reaction was:&lt;br&gt;
“How is this even possible?” &lt;/p&gt;

&lt;p&gt;An inner function remembering variables after the outer function has already finished execution felt really confusing at first.&lt;/p&gt;

&lt;p&gt;But after understanding how Python handles memory and scope internally, closures finally became much easier to understand.&lt;/p&gt;

&lt;p&gt;So here’s the explanation that helped me the most.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, What is Scope?
&lt;/h2&gt;

&lt;p&gt;Scope simply means:&lt;/p&gt;

&lt;p&gt;“Where can a variable be accessed from?”&lt;/p&gt;

&lt;p&gt;Python mainly works with different scopes like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global scope&lt;/li&gt;
&lt;li&gt;Local scope&lt;/li&gt;
&lt;li&gt;Enclosing scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see a simple example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F15xqnwzefogj39i969lv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F15xqnwzefogj39i969lv.png" alt=" " width="508" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;x can be accessed almost everywhere because it is global&lt;/li&gt;
&lt;li&gt;y only exists inside the function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the function finishes execution, y disappears from memory.&lt;/p&gt;

&lt;p&gt;That part is straightforward.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now Comes Closures
&lt;/h2&gt;

&lt;p&gt;Closures become interesting when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a function exists inside another function&lt;/li&gt;
&lt;li&gt;and the inner function uses variables from the outer function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2jcpw89uf8ulqjm8mptc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2jcpw89uf8ulqjm8mptc.png" alt=" " width="407" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first glance, this might look normal.&lt;/p&gt;

&lt;p&gt;But something very important is happening here.&lt;/p&gt;

&lt;p&gt;What happens?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;outer() starts execution&lt;/li&gt;
&lt;li&gt;x = 10 is created&lt;/li&gt;
&lt;li&gt;inner() function is created&lt;/li&gt;
&lt;li&gt;inner gets returned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Normally, after a function finishes execution, its local variables should disappear.&lt;/p&gt;

&lt;p&gt;So technically: x should have been destroyed.&lt;/p&gt;

&lt;p&gt;the inner() function is still using x&lt;br&gt;
So instead of deleting it, Python preserves that variable in memory.&lt;br&gt;
Even though outer() has already finished execution.&lt;/p&gt;

&lt;p&gt;How?&lt;/p&gt;

&lt;p&gt;Because the inner function remembered the variable from its outer function.&lt;/p&gt;

&lt;p&gt;And this is exactly what a closure is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Definition of Closure
&lt;/h2&gt;

&lt;p&gt;A closure is:&lt;/p&gt;

&lt;p&gt;A function that remembers variables from its outer scope even after the outer function has finished execution.&lt;/p&gt;

&lt;p&gt;Or even more simply:&lt;/p&gt;

&lt;p&gt;Closure = Function + Remembered Environment&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why This Feels Special&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Normally, local variables disappear after function execution ends.&lt;/p&gt;

&lt;p&gt;But in closures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python keeps those variables alive&lt;/li&gt;
&lt;li&gt;because another function still needs them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s the “magic” behind closures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's take a real example - Counter&lt;/strong&gt;&lt;br&gt;
This example can make closures finally click for us:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fhft7j95nf20yg4wtmu08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fhft7j95nf20yg4wtmu08.png" alt=" " width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and output is:&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s Happening Here?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every time increment() runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it still remembers the old value of count&lt;/li&gt;
&lt;li&gt;even though counter() already finished execution long ago&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That remembered state is preserved because of closure.&lt;/p&gt;

&lt;p&gt;And: nonlocal allows us to modify that outer variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Confusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not every nested function is a closure.&lt;/p&gt;

&lt;p&gt;A closure only exists when:&lt;br&gt;
the inner function remembers variables from the outer function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Closures Matter
&lt;/h2&gt;

&lt;p&gt;Closures are heavily used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;decorators&lt;/li&gt;
&lt;li&gt;callbacks&lt;/li&gt;
&lt;li&gt;state management&lt;/li&gt;
&lt;li&gt;function factories&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even many advanced Python features internally depend on this concept.&lt;/p&gt;

&lt;h2&gt;
  
  
  End Game
&lt;/h2&gt;

&lt;p&gt;At first, closures looked very confusing to me because the outer function had already finished execution.&lt;/p&gt;

&lt;p&gt;But once I understood that Python preserves variables in memory when inner functions still need them, the concept became much clearer.&lt;/p&gt;

&lt;p&gt;It’s one of those topics that feels “magical” initially, but very logical once broken down step by step.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>str() vs repr() vs print() in Python</title>
      <dc:creator>Samiksha Srivastav</dc:creator>
      <pubDate>Mon, 11 May 2026 03:07:53 +0000</pubDate>
      <link>https://dev.to/samikshasri/str-vs-repr-vs-print-in-python-4p9m</link>
      <guid>https://dev.to/samikshasri/str-vs-repr-vs-print-in-python-4p9m</guid>
      <description>&lt;p&gt;While learning Python, I came across three things that look very similar at first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;str()&lt;/li&gt;
&lt;li&gt;repr()&lt;/li&gt;
&lt;li&gt;print()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Initially, I thought all of them simply display output.&lt;br&gt;
But after exploring deeper, I realized they actually serve different purposes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Core Idea
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;str() ---&amp;gt; user-friendly representation&lt;/li&gt;
&lt;li&gt;repr() ---&amp;gt; developer/debugging representation&lt;/li&gt;
&lt;li&gt;print() ---&amp;gt; displays output on the screen&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's understand the things in detail
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;str()&lt;/strong&gt; is used when we want output that is clean and readable for humans.&lt;br&gt;
&lt;a href="https://media2.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%2F3e9oi3tf1zhlzhy4kenh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3e9oi3tf1zhlzhy4kenh.png" alt=" " width="423" height="92"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, Python gives us a simple readable version of the object.&lt;/p&gt;

&lt;p&gt;But, &lt;strong&gt;repr() is different.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It tries to show the &lt;strong&gt;exact representation&lt;/strong&gt; of the object - the way Python internally sees it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwg9w8i87sbmzefzre8sy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwg9w8i87sbmzefzre8sy.png" alt=" " width="465" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice something interesting?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quotes are visible here.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's because &lt;strong&gt;repr()&lt;/strong&gt; is meant more for developers and debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Better Example
&lt;/h2&gt;

&lt;p&gt;Now let’s take a string containing a newline character:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvz9zd8kthtvfnlu5z3j3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvz9zd8kthtvfnlu5z3j3.png" alt=" " width="424" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But using &lt;strong&gt;repr()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F64xlwnccdfh2tf3bh43i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F64xlwnccdfh2tf3bh43i.png" alt=" " width="459" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s Happening Here?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;str() shows the readable version&lt;/li&gt;
&lt;li&gt;repr() shows the raw/internal representation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why \n is visible inside repr().&lt;/p&gt;

&lt;h2&gt;
  
  
  So What Does print() Do?
&lt;/h2&gt;

&lt;p&gt;print() is simply used to display output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmwggkpk9pgssjgw4jg7j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmwggkpk9pgssjgw4jg7j.png" alt=" " width="304" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have noticed....Internally, print() usually uses str() to display objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Function&lt;/strong&gt;  | &lt;strong&gt;Purpose&lt;/strong&gt;&lt;br&gt;
str()         | Human-readable output&lt;br&gt;
repr()        | Developer/debugging output&lt;br&gt;
print()       | Displays output&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;At first, these functions looked almost identical to me.&lt;/p&gt;

&lt;p&gt;But understanding the difference between readable output and internal representation made Python behavior much clearer — especially for debugging and understanding objects deeply.&lt;/p&gt;

&lt;p&gt;Small concept, but very powerful.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding Mutable and Immutable Objects in Python</title>
      <dc:creator>Samiksha Srivastav</dc:creator>
      <pubDate>Wed, 06 May 2026 14:44:48 +0000</pubDate>
      <link>https://dev.to/samikshasri/understanding-mutable-and-immutable-objects-in-python-1ml8</link>
      <guid>https://dev.to/samikshasri/understanding-mutable-and-immutable-objects-in-python-1ml8</guid>
      <description>&lt;p&gt;Now let's discuss &lt;strong&gt;mutable and immutable objects in Python.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mutable&lt;/strong&gt; means something that &lt;em&gt;can change&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Immutable&lt;/strong&gt; means something that &lt;em&gt;cannot change&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, any data or object whose value can be changed after creation is called &lt;strong&gt;mutable&lt;/strong&gt;, and those which cannot be changed are called &lt;strong&gt;immutable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's All About Memory&lt;/strong&gt;&lt;br&gt;
What does "change" actually mean here?&lt;/p&gt;

&lt;p&gt;This concept is all about how values are stored in memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s Take an Immutable Data Type (Integer)&lt;/strong&gt;&lt;br&gt;
Let’s understand this with an example in the Python shell:&lt;br&gt;
&lt;a href="https://media2.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%2F8b8n2ssq8lmsih9rfgil.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8b8n2ssq8lmsih9rfgil.png" alt=" " width="422" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But Wait…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We know that integers are immutable,&lt;br&gt;
so how are we able to “change” the value of int_2?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What’s Actually Happening?&lt;/strong&gt;&lt;br&gt;
When we create int_1 = 5, a value &lt;strong&gt;5 is stored in memory&lt;/strong&gt;, and int_1 refers to it&lt;br&gt;
When we assign int_2 = int_1, both variables refer to the &lt;strong&gt;same memory location (value 5)&lt;/strong&gt;&lt;br&gt;
But when we do int_2 = 8, &lt;strong&gt;we are not modifying 5&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, Python creates a &lt;strong&gt;new object (8) in memory&lt;/strong&gt;,&lt;br&gt;
and now int_2 starts referring to this new location&lt;/p&gt;

&lt;p&gt;While int_1 still refers to the original value 5&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Insight
&lt;/h2&gt;

&lt;p&gt;Immutable objects cannot be changed in place.&lt;br&gt;
Any “change” actually creates a &lt;strong&gt;new object in memory.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What About Mutable?
&lt;/h2&gt;

&lt;p&gt;Mutable objects (like lists, dictionaries, etc.) can be changed directly &lt;strong&gt;without creating a new object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Thought&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, mutable vs immutable is not just about “change” -&lt;br&gt;
it’s about &lt;strong&gt;how Python handles memory and references internally.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’re learning Python, follow along - I’m sharing my journey step by step.&lt;/p&gt;

</description>
      <category>python</category>
      <category>learning</category>
      <category>programming</category>
    </item>
    <item>
      <title>Day-2 of learning Python | Exploring Python Shell and Module Reloading</title>
      <dc:creator>Samiksha Srivastav</dc:creator>
      <pubDate>Tue, 05 May 2026 14:28:45 +0000</pubDate>
      <link>https://dev.to/samikshasri/day-2-of-learning-python-exploring-python-shell-and-module-reloading-2275</link>
      <guid>https://dev.to/samikshasri/day-2-of-learning-python-exploring-python-shell-and-module-reloading-2275</guid>
      <description>&lt;p&gt;While learning python today, I spent some time understanding how the Python shell works and how modules behave inside it.&lt;/p&gt;

&lt;p&gt;The Python shell (or REPL) is basically an interactive environment where you can run code line-by-line. It's super useful when you just want to test something quickly instead of running a full script every time.&lt;/p&gt;

&lt;p&gt;While experimenting, I tried something interesting. I created a Python file, defined a few functions and variables in it, and then imported that file into the Python shell. everything worked perfectly at first.&lt;/p&gt;

&lt;p&gt;But then I made some changes in the file - added few functions and modified a few existing ones - and tried to use them again in the shell. Surprisingly, I started getting errors.&lt;/p&gt;

&lt;p&gt;At first, it felt confusing because I had clearly updated the code. But then I learned what was actually happening behind the scenes.&lt;/p&gt;

&lt;p&gt;When we import a module in Python, it gets loaded into the memory once. So even if we change the file later and save them, the Python shell doesn't automatically pick up those changes.&lt;/p&gt;

&lt;p&gt;To fix this, Python provides a way to reload the module using the &lt;strong&gt;importlib&lt;/strong&gt; module:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;import importlib&lt;br&gt;
importlib.reload(module_name)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This forces Python to reload the module and reflect the latest changes.&lt;/p&gt;

&lt;p&gt;It was a small thing, but understanding this made the behavior of Python much clearer to me. It also showed me how important it is to know what's happening internally, not just write code.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>devjournal</category>
      <category>learning</category>
      <category>python</category>
    </item>
    <item>
      <title>Getting Started with Python</title>
      <dc:creator>Samiksha Srivastav</dc:creator>
      <pubDate>Mon, 04 May 2026 14:45:53 +0000</pubDate>
      <link>https://dev.to/samikshasri/getting-started-with-python-3cnj</link>
      <guid>https://dev.to/samikshasri/getting-started-with-python-3cnj</guid>
      <description>&lt;p&gt;Today I started learning Python, and I explored some fundamental concepts that helped me understand how Python actually works behind the scenes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Python?
&lt;/h2&gt;

&lt;p&gt;Python is a &lt;strong&gt;high-level, interpreted programming language.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Being &lt;strong&gt;high-level&lt;/strong&gt; means it is easy to read and write, as it is closer to human language and abstracts away hardware complexity.&lt;/li&gt;
&lt;li&gt;This makes it very different from low-level languages like &lt;strong&gt;assembly&lt;/strong&gt; or &lt;strong&gt;machine language&lt;/strong&gt;, which directly interact with hardware.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Is Python Really Interpreted?
&lt;/h2&gt;

&lt;p&gt;We often hear that Python is an interpreted language, but there is a bit more to it.&lt;/p&gt;

&lt;p&gt;Python actually works in two main steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compilation to Bytecode&lt;/strong&gt;&lt;br&gt;
 First, Python converts your code into an intermediate format called &lt;strong&gt;bytecode.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Execution using Python Virtual Machine (PVM)&lt;/strong&gt;&lt;br&gt;
 This bytecode is then executed by the &lt;strong&gt;Python Virtual Machine (PVM.)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This process is what makes Python both flexible and easy to debug.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;strong&gt;pycache&lt;/strong&gt;?
&lt;/h2&gt;

&lt;p&gt;While running Python programs, you might notice a folder named &lt;strong&gt;pycache&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It stores compiled &lt;strong&gt;bytecode files(.pyc)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;These files help Python run faster when the program is executed again &lt;/li&gt;
&lt;li&gt;This process is usually automatic and hidden from developers
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python is &lt;strong&gt;easy to read and beginner-friendly&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;It is &lt;strong&gt;interpreted but internally uses bytecode&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Execution happens through the Python Virtual Machine (PVM)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pycache&lt;/strong&gt; improves performance by storing compiled code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Understanding what happens behind the scenes makes learning Python much more interesting. This is just the beginning of my journey, and I'm excited to explore more.&lt;/p&gt;

&lt;p&gt;Follow my journey as I continue learning and building in Python!&lt;/p&gt;

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