Welcome back!
In the last post, we learned the basics of variables and constants in Python. But guess what? We didn’t cover everything! There are still a few super useful tips and tricks that you definitely need to know.
I intentionally split this topic into two parts because variables and constants are truly the building blocks of programming. And instead of dumping everything in one go, I want to make sure each part is clear and easy to follow.
So, let’s continue from where we left off — exploring some more important concepts about variables in Python.
Rules for Naming Variables
When you create or assign a variable in Python, you need to follow some basic rules:
-
Always start with a letter.
You can’t begin a variable name with numbers or special symbols. -
Underscore is allowed — but not at the beginning.
You can use underscores in between words when naming multi-word variables.
Example:customer_name = "Alice" account_balance = 1500 -
Avoid special characters such as
@,#,$,%, etc.
Stick to letters, numbers, and underscores only. -
Use meaningful names.
Instead of writing something likex = 500, use:account_balance = 1500 -
This makes your code cleaner and easier to understand.
-
Stick to English names.
Even if you’re comfortable in another language, using English helps others collaborate easily on your code.
Don’t Use Reserved Words
Python has a set of reserved words (keywords) that already have special meanings — like class, def, if, else, while, etc.
You cannot use them as variable names. If you do, Python will get confused because it expects those words to perform a specific function in the language.
Example
if = 10 # Invalid
Python will throw an error because if is a reserved word used for conditions.
So always make sure the name you choose is not already a reserved keyword.
Here are a few examples of reserved words in Python:
and, as, assert, async, await, break, class, continue, def, elif, else, for, from, if, import, in, is, lambda, not, or, pass, return, try, while, with, yield
📝 Assignment in Python
Let’s talk about assignment, which is just a fancy way of saying:
“Hey Python, store this value inside this variable.”
When you write:
number = 10
name = "Mary"
balance = 1000
active = True
You’re telling Python to store these values inside their respective variables.
The = (equal sign) is what assigns the value to the variable.
You can even assign multiple values to multiple variables in one line!
Example:
a, b, c = 1, 2, 3
Here:
-
agets the value1 -
bgets the value2 -
cgets the value3
Try printing them:
print(a)
print(b)
print(c)
You’ll see 1, 2, and 3 as outputs.
Important: Python treats uppercase and lowercase variables as different.
A = 10
a = 5
Here, A and a are two separate variables. Always keep that in mind!
⚙️ What Is Dynamic Typing?
Now let’s look at something awesome — dynamic typing.
Python is called dynamically typed, which means you don’t have to tell it what type of value you’re assigning. It figures it out automatically.
Example:
x = 10
print(type(x))
Output:
<class 'int'>
Here, Python understands that x is an integer.
Now if you change it:
x = "Hello"
print(type(x))
Output:
<class 'str'>
And if you do:
x = 3.14
print(type(x))
Output:
<class 'float'>
See? Python instantly knows whether it’s an integer, string, or float — no need to declare it manually!
You can even reuse the same variable for a new data type, and Python will automatically update it for you.
If you ever want to check what type of value a variable holds, you can use the built-in type() function:
type(variable_name)
It’s super handy when debugging or verifying that your code is behaving as expected.
🧠 Quick Recap
Here’s a short summary of everything we’ve covered in this part:
-
Follow naming rules – start with letters, use underscores, and avoid symbols.
-
Don’t use reserved words like
if,class,while, etc. -
Assignment means storing values in variables using
=. -
Python is case-sensitive (
A≠a). -
Dynamic typing allows Python to automatically detect variable types.
-
Always give meaningful names and write code in English for better readability.
That was our deep dive into Python variables — covering naming rules, assignments, and dynamic typing.
See you in the next post, 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-deep-dive-naming-rules-assignments-and-dynamic-typing-explained/
License agreement:Creative Commons Attribution-NonCommercial 4.0 International License