Welcome to your first real step into Python programming! In this post, we’ll explore variables — the building blocks of every Python program.
Whether you’re just starting out or brushing up on the basics, this guide will walk you through everything you need to know in plain English, with examples and tips to help you avoid common mistakes.
What Happens When You Run a Python Program?
Let’s start with the simplest Python program ever:
print("Hello world!")
When you run this code (typically saved in a file like hello_world.py
), Python does a few things:
- It recognizes the
.py
extension and knows it’s a Python file. - The interpreter reads the code line by line.
- It sees the
print()
function and displays whatever is inside the parentheses.
Output:
Hello world!
Your code editor might highlight different parts of the code in color—this is called syntax highlighting, and it helps you spot functions, strings, and keywords more easily.
What Are Variables?
Variables are like labels you stick on data so you can refer to it later. Let’s modify our program:
message = "Hello world!"
print(message)
Now, instead of printing the string directly, we store it in a variable called message
. Python remembers this value and prints it when asked.
You can even change the value of a variable:
message = "Hello world!"
print(message)
message = "Hello All About Python world!"
print(message)
Output:
Hello world!
Hello All About Python world!
Rules for Naming Variables
Python is flexible, but it has rules. Here’s what you need to know:
✅ Allowed:
- Letters, numbers, and underscores (
_
) - Start with a letter or underscore (not a number)
- Use underscores to separate words:
user_name
,total_score
❌ Not Allowed:
- Spaces:
user name
❌ - Starting with a number:
1st_place
❌ - Using Python keywords:
print
,for
,if
❌
💡 Tips:
- Keep names short but meaningful:
score
is better thans
- Avoid confusing characters like lowercase
l
and uppercaseO
(they look like1
and0
) - Stick to lowercase for now—uppercase has special uses later
Common Mistake: Name Errors
Let’s look at a mistake you’ll probably make (and that’s okay!):
message = "Hello reader!"
print(mesage) # Oops! Misspelled 'message'
Output:
NameError: name 'mesage' is not defined. Did you mean: 'message'?
Python is helpful—it tells you where the error is and even suggests a fix. This kind of error is called a NameError, and it usually means:
- You misspelled the variable name
- You tried to use a variable before defining it
Now try this:
mesage = "Hello Python reader!"
print(mesage)
This works fine because both lines use the same (misspelled) name. Python doesn’t care about spelling—it just wants consistency.
Variables Are Labels, Not Boxes
You might hear people say variables are like boxes that hold values. That’s okay for starters, but here’s a better way to think about it:
A variable is a label that points to a value.
This matters when you start working with more complex data types like lists and dictionaries. Understanding that variables reference values—not contain them—will help you debug tricky issues later.
Final Thoughts
Learning Python is like learning a new language. You’ll make typos, forget rules, and get confused—and that’s perfectly normal. The key is to keep practicing and experimenting.
Here’s a quick recap:
Concept | Summary |
---|---|
print() | Displays output to the screen |
Variable | A label that references a value |
Syntax Highlighting | Helps you read code more easily |
Naming Rules | Use letters, numbers, underscores; avoid spaces and keywords |
NameError | Happens when you use an undefined or misspelled variable |
Are you ready to try It yourself?
Open your code editor, create a file called variables.py
, and start playing with variables. Change values, make mistakes, and learn from them. That’s how every great programmer starts.
Let me know in the comments if you have questions or want help with your first Python project!
Ready for More?
If you’ve cracked the basics, it’s time to level up. Check out our next post Python Variables Continued: Assignments, Globals & Naming Styles where we dive deeper into multiple assignments, global variables, naming conventions, and constants. Your Python journey continues here!
