Python Variables and Identifiers: The Building Blocks of Every Program
Python Variables and Identifiers: The Building Blocks of Every Program
Introduction
Imagine you want to store your name, age, or marks in a Python program. How does Python remember these values?
This is where Identifiers and Variables come into play. They are among the most fundamental concepts in programming and are used in almost every Python program you will ever write.
In this article, you'll learn what identifiers and variables are, how they work, and the difference between them.
What Are Identifiers?
An Identifier is the name given to a variable, function, class, or any other object in Python.
In simple words, an identifier is the name used to identify something in a program.
Example
name = "John"
age = 20
Here:
nameis an identifierageis an identifier
Python uses these names to refer to stored data.
Rules for Naming Identifiers
Python follows certain rules when creating identifiers.
Rules for Naming Identifiers and Variables
Python follows certain rules when naming identifiers. Since variable names are also identifiers, the same rules apply to both. Let's understand each rule with examples.
1. Must Start with a Letter or Underscore (_)
An identifier should begin with a letter (A-Z, a-z) or an underscore (_).
✅ Valid:
name = "John"
_age = 20
❌ Invalid:
1name = "John"
2. Can Contain Letters, Digits, and Underscores
After the first character, an identifier can contain letters, numbers, and underscores.
✅ Valid:
student1 = "Rahul"
student_name = "Rahul"
3. Cannot Start with a Digit
Although digits can be used in identifiers, they cannot appear at the beginning.
❌ Invalid:
9marks = 95
✅ Valid:
marks9 = 95
4. No Special Characters Allowed
Special characters such as @, #, $, %, and ! are not allowed in identifiers.
❌ Invalid:
student@name = "Rahul"
✅ Valid:
student_name = "Rahul"
5. Python Is Case-Sensitive
Python treats uppercase and lowercase letters differently.
age = 20
Age = 25
AGE = 30
These are three different identifiers. ( even you just changed case python think them as different )
6. Avoid Using Python Keywords
Keywords already have special meanings in Python and should not be used as identifiers.
❌ Invalid:
if = 10
for = 20
class = 30
What Are Variables?
A Variable is a container used to store data.
Think of a variable as a labeled box. The label helps us identify the box, and the box stores some value.
Example
name = "John"
age = 20
Here:
Variable
namestores"John"Variable
agestores20
Variable Syntax in Python
What is Syntax?
Syntax is the set of rules that tells us how to write code correctly in a programming language.
Think of it like grammar in English.
For example, in English:
✅ "I am a student."
❌ "Student am I a."
Both contain the same words, but only one follows English grammar rules.
Similarly, Python has syntax rules that tell us how code should be written.
example:-
variable_name = value
Storing Different Types of Data
Variables can store different kinds of values.
String
name = "Alice"
Integer
age = 18
Decimal Number
height = 5.8
Boolean
is_student = True( NOTE:-This topic is related literals which we learn in next post)
Multiple Variable Assignment
Python allows assigning multiple variables in a single line.
Example
a, b, c = 10, 20, 30
print(a)
print(b)
print(c)
Output
10
20
30
Good Variable Names
Good variable names make programs easier to understand.
Good Examples
student_name
total_marks
account_balance
Poor Examples
a
x1
abc
Choose meaningful names whenever possible.
Variables vs Identifiers
Many beginners confuse variables and identifiers.
๐Simple identifiers are the names given to entities like Variables, Functions, etc., While variables are specific type of identifiers used to store & manipulate data in a program
| Identifier | Variable |
|---|---|
| Acts as a label | Acts as a container |
Example: age | Example: age = 20 |
| Helps Python find data | Holds the actual value |
Example
age = 20
Here:
ageis the identifierage = 20represents a variable storing the value20
Real-Life Analogy
Imagine a classroom.
Each student has:
A name tag
Personal information
Example:
Name Tag → Rahul
Age → 18
Here:
"Rahul" acts like an identifier
The information associated with it acts like stored data
Similarly, in Python:
age = 18
age identifies the stored value.
Conclusion
Identifiers and variables are the foundation of Python programming.
In this article, we learned:
What identifiers are
Rules for naming identifiers
What variables are
what is syntax
Multiple assignments
The difference between variables and identifiers
Understanding these concepts is essential because almost every Python program uses variables and identifiers.
What's Next?
In the next article, we'll explore Constants, Literals, and Data Types in Python, which help us understand how Python stores and categorizes different kinds of data.



Comments
Post a Comment