<?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: Rubyfield press</title>
    <description>The latest articles on DEV Community by Rubyfield press (@hassan_alruby_5a2e0c5f349).</description>
    <link>https://dev.to/hassan_alruby_5a2e0c5f349</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4269550%2F44837901-ac8d-4af5-ab6a-5dbd83608b9c.png</url>
      <title>DEV Community: Rubyfield press</title>
      <link>https://dev.to/hassan_alruby_5a2e0c5f349</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hassan_alruby_5a2e0c5f349"/>
    <language>en</language>
    <item>
      <title>Python Variables Explained for Complete Beginners (With Pictures in Your Head)</title>
      <dc:creator>Rubyfield press</dc:creator>
      <pubDate>Fri, 27 Nov 2026 18:56:30 +0000</pubDate>
      <link>https://dev.to/hassan_alruby_5a2e0c5f349/python-variables-explained-for-complete-beginners-with-pictures-in-your-head-4ogj</link>
      <guid>https://dev.to/hassan_alruby_5a2e0c5f349/python-variables-explained-for-complete-beginners-with-pictures-in-your-head-4ogj</guid>
      <description>&lt;h2&gt;
  
  
  Why variables trip people up
&lt;/h2&gt;

&lt;p&gt;Variables are the first idea in programming where you have to picture something you can't see. Most tutorials show &lt;code&gt;x = 5&lt;/code&gt;, say "x is now 5" and move on. Then the beginner sees &lt;code&gt;x = x + 1&lt;/code&gt; and thinks the computer has broken maths.&lt;/p&gt;

&lt;p&gt;This post explains variables the way we teach them in &lt;em&gt;Python Made Simple&lt;/em&gt;: an everyday picture first, then the real Python terms, then code you can run. You need Python installed and any editor. We use the free Thonny editor because it is made for beginners.&lt;/p&gt;

&lt;h2&gt;
  
  
  Picture a labelled box
&lt;/h2&gt;

&lt;p&gt;When you move house, you write a label on every box: &lt;em&gt;Kitchen&lt;/em&gt;, &lt;em&gt;Books&lt;/em&gt;, &lt;em&gt;Winter coats&lt;/em&gt;. Weeks later you don't open every box to find a cookery book. You look for the one labelled &lt;em&gt;Books&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A variable works the same way. It has two halves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;name&lt;/strong&gt;: the label on the outside, such as &lt;code&gt;customer_name&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;value&lt;/strong&gt;: the information it holds, such as &lt;code&gt;"Priya Shah"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why bother? Imagine greeting a customer by name in ten places in a program. Without a variable you type the name ten times. With one, you type it once, and changing it in one place changes it everywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your first variable
&lt;/h2&gt;

&lt;p&gt;Type this into a new file and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Priya Shah"&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Priya Shah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line is an &lt;strong&gt;assignment statement&lt;/strong&gt;. The &lt;code&gt;=&lt;/code&gt; sign is the &lt;strong&gt;assignment operator&lt;/strong&gt;, and Python always carries it out in two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Work out the value on the right.&lt;/li&gt;
&lt;li&gt;Attach the name on the left to that value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The most important thing to unlearn from school maths: in Python, a single &lt;code&gt;=&lt;/code&gt; does not mean "equals". It is an instruction. Read the line aloud as "customer_name &lt;strong&gt;is set to&lt;/strong&gt; Priya Shah".&lt;/p&gt;

&lt;h3&gt;
  
  
  The label or the text?
&lt;/h3&gt;

&lt;p&gt;Quotation marks change everything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Priya Shah"&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"customer_name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customer_name
Priya Shah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rule of thumb: quotation marks mean &lt;em&gt;these exact characters&lt;/em&gt;. No quotation marks means &lt;em&gt;the value attached to this name&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Several variables together
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;shop_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Corner Bakery"&lt;/span&gt;
&lt;span class="n"&gt;customer_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Priya Shah"&lt;/span&gt;
&lt;span class="n"&gt;number_of_items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Welcome to"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shop_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Customer:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Items in basket:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number_of_items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Welcome to Corner Bakery
Customer: Priya Shah
Items in basket: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Text goes in quotation marks; the number &lt;code&gt;3&lt;/code&gt; doesn't. The comma inside &lt;code&gt;print()&lt;/code&gt; separates the items, and Python adds one space between them for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing a value
&lt;/h2&gt;

&lt;p&gt;It's called a &lt;em&gt;variable&lt;/em&gt; because its value can vary. Assign a new value to the same name and the old one is replaced:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;favourite_drink&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tea"&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"My favourite drink is"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favourite_drink&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;favourite_drink&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"coffee"&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"My favourite drink is now"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;favourite_drink&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;My favourite drink is tea
My favourite drink is now coffee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The line that looks impossible
&lt;/h3&gt;

&lt;p&gt;Now the one that confuses everyone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;number_of_items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;number_of_items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;number_of_items&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Items in basket:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number_of_items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Items in basket: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In maths, "number = number + 2" can never be true. In Python it makes perfect sense, because &lt;code&gt;=&lt;/code&gt; is the same two-step instruction as before:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Work out the right-hand side: &lt;code&gt;number_of_items + 2&lt;/code&gt; is &lt;code&gt;3 + 2&lt;/code&gt;, which is &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Attach the name on the left to the result: &lt;code&gt;number_of_items&lt;/code&gt; now refers to &lt;code&gt;5&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is how every program keeps a running total: a score going up, a basket filling, a balance changing.&lt;/p&gt;

&lt;p&gt;Python also has a shortcut for it. &lt;code&gt;score += 5&lt;/code&gt; means exactly the same as &lt;code&gt;score = score + 5&lt;/code&gt;, and there are matching versions for subtracting (&lt;code&gt;-=&lt;/code&gt;), multiplying (&lt;code&gt;*=&lt;/code&gt;) and dividing (&lt;code&gt;/=&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Naming your variables
&lt;/h2&gt;

&lt;p&gt;Python is strict about names. A variable name:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;can contain letters, digits (0–9) and the underscore &lt;code&gt;_&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;cannot start with a digit: &lt;code&gt;item_2&lt;/code&gt; is fine, &lt;code&gt;2nd_item&lt;/code&gt; is not&lt;/li&gt;
&lt;li&gt;cannot contain spaces or symbols such as &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;!&lt;/code&gt; or &lt;code&gt;?&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;cannot be a keyword Python already uses, such as &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;is case-sensitive: &lt;code&gt;total_price&lt;/code&gt; and &lt;code&gt;Total_price&lt;/code&gt; are different names&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A name can follow every rule and still be unhelpful:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Valid but unclear&lt;/th&gt;
&lt;th&gt;Clear and meaningful&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;x&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;number_of_items&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tp&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;total_price&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;thing&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;delivery_address&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Python's official style guide, PEP 8, recommends lower-case words joined by underscores, a style called &lt;em&gt;snake_case&lt;/em&gt;. Pick names that say what the value means; future you will thank you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three mistakes everyone makes (and the fixes)
&lt;/h2&gt;

&lt;p&gt;Error messages are clues, not criticism. Read the last line first; it usually tells you the most.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. A typo in the name&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Priya Shah"&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;custmer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&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;NameError: name 'custmer_name' is not defined. Did you mean: 'customer_name'?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python matches names letter for letter, capitals included, so &lt;code&gt;custmer_name&lt;/code&gt; is a brand-new name it has never seen. &lt;strong&gt;Fix:&lt;/strong&gt; take Python's suggestion and correct the spelling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Using a variable before creating it&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delivery_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;delivery_address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"12 Station Road"&lt;/span&gt;
&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;NameError: name 'delivery_address' is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python runs top to bottom, so at line 1 the name doesn't exist yet. &lt;strong&gt;Fix:&lt;/strong&gt; assign first, use second.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. A space in the name&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Priya Shah"&lt;/span&gt;
&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;SyntaxError: invalid syntax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python sees two separate words. Hyphens (&lt;code&gt;first-name&lt;/code&gt;) and leading digits (&lt;code&gt;2nd_place&lt;/code&gt;) fail too. &lt;strong&gt;Fix:&lt;/strong&gt; &lt;code&gt;customer_name&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it yourself
&lt;/h2&gt;

&lt;p&gt;Predict the answer before you run this. Start with 20 coins, spend 7, then double what's left:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write your prediction in the comments, then run it. (The answer is at the very end of this post.)&lt;/p&gt;

&lt;p&gt;Bonus challenge: create two variables, &lt;code&gt;morning_drink = "coffee"&lt;/code&gt; and &lt;code&gt;evening_drink = "hot chocolate"&lt;/code&gt;, then write code that swaps them so the morning drink is hot chocolate and the evening drink is coffee. It's trickier than it looks. Share your solution below.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;You now know what a variable is, how &lt;code&gt;=&lt;/code&gt; really works, how to change a value and how to name things well. That's the foundation of every program you'll ever write.&lt;/p&gt;

&lt;p&gt;Next in this series: why &lt;code&gt;3 + 4&lt;/code&gt; gives &lt;code&gt;7&lt;/code&gt; but &lt;code&gt;"3" + "4"&lt;/code&gt; gives &lt;code&gt;"34"&lt;/code&gt;, which is all about data types.&lt;/p&gt;

&lt;p&gt;This post is adapted from Chapter 5 of &lt;em&gt;Python Made Simple: A Visual, Hands-On Guide to Programming for Complete Beginners&lt;/em&gt;, a full-colour book where every idea gets a plain-English explanation and a labelled diagram on the facing page. It goes from your very first program to building a calculator, a quiz game and an expense tracker. Every example was tested on Python 3.15 and 3.14.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://leanpub.com/pythonmadesimpleavisualhands-onguidetoprogrammingforcompletebeginners" rel="noopener noreferrer"&gt;Read the free sample on Leanpub&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Answer to the coins puzzle: 26. Starting at 20, spending 7 leaves 13, and doubling 13 gives 26.&lt;/em&gt;&lt;/p&gt;

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