Crack Python Basics: Learn Variables the Easy Way

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.

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.

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!

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 than s
  • Avoid confusing characters like lowercase l and uppercase O (they look like 1 and 0)
  • Stick to lowercase for now—uppercase has special uses later

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.

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.

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:

ConceptSummary
print()Displays output to the screen
VariableA label that references a value
Syntax HighlightingHelps you read code more easily
Naming RulesUse letters, numbers, underscores; avoid spaces and keywords
NameErrorHappens when you use an undefined or misspelled variable

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!

Variables

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top