What is a Variable?
A variable in Python is like a labeled container that holds a value.
We can use it to store information like text, numbers, or other data,
and later use or change it in our program whenever needed.
In Python, you don’t need to declare a variable type — just assign a value using the =
operator, and Python will understand the type automatically.
Syntax:
variable_name = value
Example:
student = "Ram" # Storing the name of the student (String)
grade = 12 # Storing number in variable(grade) (Integer)
marks = 92.5 # Storing marks in variable(marks) (Float)
Explanation:
student
is a variable that stores the name of the student, which is a text value (String).grade
stores the class or standard of the student, which is a whole number (Integer).marks
stores the student's marks, which is a number with decimal points (Float).
Important Points about Variables:
- You don't need to declare the variable type in Python.
- The variable's type is determined automatically based on the value assigned.
- Variables make programs flexible and reusable.
Why are Variables Important?
- They store data to be used multiple times.
- They allow for dynamic data manipulation.
- They make code readable and logical.
How to Create a Variable
Just use the assignment operator =
:
message = "Enjoying Coding with ShikshaSanchar"
count = 5
pi = 3.14
Rules for Naming Variables
While Python is flexible, it has some rules and best practices for naming variables:
-
Variable names must begin with a letter (a–z, A–Z) or underscore (_)
_valid = 100
name = "Devanshi"
2name = "error" # ❌ Invalid: starts with a number @age = 20 # ❌ Invalid: starts with special character
Note: In Python, variable names can’t start with a number or special character. Therefore,
2name
and@age
are invalid and will give an errors. -
Variable names can only contain letters, digits, and underscores
my_var1 = 10
roll_number = 25
my-var = 20 # ❌ Invalid: contains hyphen my var = 100 # ❌ Invalid: contains space
Note: Python variable names cannot have spaces or hyphens. In the above examples,
my-var
andmy var
are invalid and will give an error. Use underscores (_) instead, likemy_var
. -
Variable names are case-sensitive
This means
Name
andname
are considered different.Name = "Shiksha" name = "Sanchar" print("Value of variable Name:", Name) print("Value of variable name:", name)
Output:
Value of variable Name: Shiksha
Value of variable name: Sanchar
In Python, variable names are case-sensitive.Therefore in the above example,
Name
stores "Shiksha" andname
stores "Sanchar". Python prints both separately based on their exact spelling and case. -
Variable names should not be Python reserved keywords
Examples of reserved words:
if
,else
,for
,True
,class
, etc.class = "Math" # ❌ Invalid: 'class' is a keyword if = 10 # ❌ Invalid: 'if' is a keyword
Note: In Python, keywords like
class
andif
cannot be used as variable namesbecause they are reserved for special purposes in the language. -
Variable names should be meaningful
Use descriptive variable names like
total_marks
instead of short or unclear ones liketm
to make your code more readable and understandable.# ✅ Recommended (clear and descriptive)
total_marks = 480
# ❌ Not recommended (unclear) tm = 480
Using
total_marks
makes it clear that the variable stores total marks. In contrast,tm
is short and confusing, especially in longer programs.
Common Mistakes Students Make:
- Starting variable names with numbers (e.g., 2value)
- Using special characters like @, $, %, - (e.g., my-name)
- Using spaces between variable names (e.g.,my var)
- Using reserved keywords (e.g., if, while)
Correct vs Incorrect Examples
# ✅ Valid Examples
name = "Peehu"
_student_age = 18
marks1 = 95
# ❌ Invalid Examples
1name = "Riya" # Starts with a digit
stu dent = 21 # Contains space
@marks = 92 # Starts with @ symbol
for = "loop" # Uses reserved keyword
Best Practices for Writing Variables
- Use lowercase letters and underscores for multi-word names (e.g.,
total_price
). - Avoid short, unclear names (e.g.,
a
,m
). - Use variable names that describe the data being stored.
- Use snake_case (words separated by underscores) as a standard in Python.