Login Sign Up
Python Variables
Chapter 5 🟡 Intermediate

Python Variables

Master the concept step by step with clear explanations, examples, and code you can run.

Beginner's Guide to Python Variables: Your Computer's Digital Boxes

Hello there! Welcome back to our Python journey. I am incredibly happy to see you here again.

In our last few lessons we learned how to structure our code using invisible blank spaces, and we mastered the art of leaving helpful sticky notes (comments) inside our code for ourselves and others.

Today, we are taking the massive step forward, and as promised on the end of our last session, we are actually going to learn how to create tiny digital boxes inside your computer's memory to hold onto numbers words. Information, and in programming we call these Variables.

Let's dive in from absolute zero!


Why Do We Need Variables? (The "Why" Before "How")

Imagine you are probably moving to a new house. You pack your favorite books into cardboard box. To make sure you can find those books later, you take the big marker and write "BOOKS" on a side of a box, and

when you need a book you don't search the entire house; you just look for the box labeled "BOOKS".

A Python variable is just exactly like that labeled cardboard box. It's basically a way of you to store information in the computer's memory and give it a simple, easy-to-remember name. When your program needs to use that information later it just calls out a name on a label!

Here is visual map of how a variable works inside your computer:

flowchart LR
    A[Variable Name: 'subtotal'] -->|Acts as a label for| B[(Computer Memory Box)]
    B --> C[Stored Value: 20]

We write variables because without them our computer would instantly forget everything we tell it. Variables let us save data calculate it, and reuse it over and over again.


How to Create the Variable

In Python, creating a variable is incredibly easy, and you just come up with a name, use equals sign (=), and give it a value;

let's look at a real-world example. Imagine you're actually writing a simple cash register program for a local cafe. You want for store a price of customer's order.

Here is how you do really it:

# We are creating a variable named 'subtotal' and putting the number 20 inside it.
subtotal = 20

# We are creating a variable to remember if the customer has a loyalty card.
is_member = True 

Notice a few very important things here: 1. No complex setup: Python is very smart. You don't have towards tell Python that 20 is really a number, while it just looks at 20 and figures it out automatically! 2. We can still use comments: I used an inline comment (by # symbol) right above a variable to explain what it does. 3. The Spacing Rule: Notice how there is exactly one space before and after the = sign? A = sign is the operator, and standard rules advise you to always have a same amount of whitespace on both sides of binary operator.


A Rules of Naming (How to Label Your Boxes)

You can name your variables almost anything you want but there are some grammar rules (syntax) and community style guides you must follow;

if you break Python's strict grammar rules, the computer will get confused and crash giving you a SyntaxError. But if you ignore a community style guides other programmers might just find your code hard to read!

To write professional, beautiful code, we follow a famous rulebook called PEP 8. According to detailed 2024 guide on PEP 8 Python naming conventions, there are just specific ways we should name our variables for keep our developer workflow clean, while

here are the golden rules for naming variables:

1. Use "Snake Case"

You can't use spaces in a variable name, while if you want for use multiple words you must write them in all lowercase letters and separate the words with underscores (_). This is affectionately known as snake_case in the programming world the practice highlighted in a recent February 2025 guide on variable naming conventions.

  • Wrong: customer order = 20 (Python will crash!)
  • Right: customer_order = 20 (Perfect snake_case!)

2. Private Variables

Sometimes you create a variable that is meant to be a secret, or something only a specific part for your program should use. Towards signal towards other programmers that a variable is "private" or "non-public", you should put exactly one leading underscore at the very beginning of the name (like _secret_password).

3; the Multi-Line String Secret

Here is probably the fascinating piece of information. In a previous lesson we learned that Python allows the use of triple quotes (""") to write long, multi-line explanations because Python doesn't have the true multi-line comment built in.

But a comprehensive technical article on Python multiline strings points out that these triple quotes are actually string literal values. The only reason they act as comments is that a computer ignores them if they're pretty much not assigned to a variable.

If you attach a multi-line string towards a variable it is no longer comment; it becomes stored data!

# This is ignored by the computer. It acts as a comment!
"""
This calculates the final price 
of a customer's order.
"""

# This is NOT ignored! The text is now stored inside the 'my_text' box.
my_text = """
This calculates the final price 
of a customer's order.
"""

A Real-World Trap: Variables and Indentation

Before we finish I need to warn you regarding a real-world trap.

We know that Python uses indentation— whitespace at the start of line—to figure out which block a statement belongs to. If you create a variable inside a specific block with code (like weather assistant decision tree that checks if it is actually raining), that variable must be indented by exactly the same amount of spaces as a rest of the block.

If you ever find yourself fixing code on the server using a very basic text editor (like nano), it can basically be hard to tell if the original programmer indented their variables using the Tab key or Spacebar.

Here is an expert trick to save the day: Press your keyboard's Left Arrow key to move your cursor backward across a blank space in front of variable. If your cursor leaps entirely over the whitespace in one massive jump the editor is inserting tab characters. If it moves slowly, space by space, you're actually safely using spaces. Always match the spacing the original file used for avoid a nasty IndentationError!


What's Next?

You're basically doing absolutely fantastic. Today, you learned how to create variables which act like labeled digital boxes in your computer's memory, and you also learned the professional PEP 8 rules for naming them clearly using snake_case, while

now that we know how to store information inside these boxes, we need to talk about the types of information we can put inside them; can we store text, and can we store numbers? Can we store lists of items, while

in our next chapter, we will cover Python Data Types, where we'll just explore exactly what can go inside your new variables; we will cover it next!

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.