Think of variables like storage boxes — the ones you use at home. You can label the box and put anything inside it. If you want to change what’s inside later, you can easily do that.
In Python, variables work exactly the same way. They are just names we give to values so that we can reuse them in our code.
What Are Variables in Python?
Let’s take an example. Open your coding editor and create a new file called:
variable.py
Now, type this:
snack = "chocolate"
print(snack)
When you run this, the output will be:
chocolate
You can run it using the Run Python button or from the command prompt.
Just open your command prompt in the same folder and type:
python variable.py
You’ll see the same output.
Here’s what’s happening:
-
You created a variable called
snack. -
You told Python that it holds the word
"chocolate". -
When you printed
snack, Python gave you backchocolate.
Simple, right?
Variables Can Change
Now, what if you suddenly stop liking chocolate and want to switch to chips? Easy!
Just change the value:
snack = "chips"
print(snack)
This time, your output will be:
chips
Python is like — “Okay boss, updated chocolate to chips!”
That’s the power of variables. They are labels for values that you can change whenever you want.
So, imagine there’s a box named snack. You put chocolate inside it. Whenever you want chocolate, you just use the word snack. Later, if you want to replace it with chips, you simply update it — and the next time you print snack, you get chips instead.
Why Variables Matter
Variables are used everywhere — when saving a name, a number, or any kind of data.
They play a big role in almost every operation you’ll perform in Python.
One important thing to remember — always give your variables meaningful names.
For example:
customer_name = "John"
customer_age = 25
Meaningful names make your code easier to understand.
If you write something like a = 100, the next developer might have no idea what a means. But if you name it customer_age, it’s instantly clear.
Good naming also helps your teammates when they read your code. They’ll understand what each variable does without needing to guess.
Declaring Variables in Python
In Python, declaring a variable is super simple. You just assign a value to a name — no need to mention any data type.
Example:
age = 25
name = "Alice"
Python automatically understands whether it’s a number, a string, or something else. You don’t have to write things like int or string before it.
What Are Constants in Python?
Now let’s talk about constants.
Constants are values that you don’t want to change. Python doesn’t have strict rules for constants, but by convention, we write them in all capital letters.
It’s like giving a big warning to yourself and other developers: “Hey, don’t mess with this value!”
For example:
PI = 3.14
Here, PI is a constant. You should never change it — just like the mathematical value of π (pi), which is always 3.14.
You can use constants in Python without any special keyword. Just write them in capital letters, and everyone will understand that they are not meant to be modified.
Even though Python technically allows you to change them, you shouldn’t. The idea of constants is for you and your team to remember not to touch those values.
📘 Variables vs Constants
| Feature | Variable | Constant |
|---|---|---|
| Can change value | ✅ Yes | ❌ No |
| Naming style | lowercase or snake_case | ALL_CAPS |
| Purpose | Store changeable data | Store fixed data |
Examples:
# Variable
age = 25
# Constant
MAX_USERS = 100
Types of Constants
-
Numerical constants: can be integers or floating-point numbers.
Example:PI = 3.14 SPEED_LIMIT = 80 - Text constants: can be in single or double quotes.
Example:GREETING = "Welcome to Python" -
They behave just like normal strings or numbers, but by naming them in caps, you’re telling everyone that these should not be changed.
🧠 Final Thoughts
So, that’s how variables and constants work in Python.
-
Variables are changeable boxes — like age, snack, or name.
-
Constants are fixed values — like
PI = 3.14orMAX_USERS = 100.
You’ll be using both of these frequently as you begin building your programs.
See you in the next one — and as always, happy coding! 💻
Copyright statement: Unless otherwise indicated, all articles are original to this site, please cite the source when sharing.
Article link:http://pybeginners.com/simplified-python/python-variables-constants-explained-simply/
License agreement:Creative Commons Attribution-NonCommercial 4.0 International License