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:
studentis a variable that stores the name of the student, which is a text value (String).gradestores the class or standard of the student, which is a whole number (Integer).marksstores 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 example:
_valid = 100
name = "Devanshi"
Invalid example:
2name = "error" # ❌ Invalid: starts with a number @age = 20 # ❌ Invalid: starts with special characterNote:
In Python, variable names can’t start with a number or special character. Therefore,2nameand@ageare invalid and will give an errors. -
Variable names can only contain letters, digits, and underscores
Valid example:
my_var1 = 10
roll_number = 25
Invalid example:
my-var = 20 # ❌ Invalid: contains hyphen my var = 100 # ❌ Invalid: contains spaceNote:
Python variable names cannot have spaces or hyphens. In the above examples,my-varandmy varare invalid and will give an error. Use underscores (_) instead, likemy_var. -
Variable names are case-sensitive
This means
Nameandnameare considered different.Example:
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
Explanation:
In Python, variable names are case-sensitive.Therefore in the above example,
Namestores "Shiksha" andnamestores "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.Example:
class = "Math" # ❌ Invalid: 'class' is a keyword if = 10 # ❌ Invalid: 'if' is a keywordNote:
In Python, keywords likeclassandifcannot 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_marksinstead of short or unclear ones liketmto make your code more readable and understandable.Example:
# ✅ Recommended (clear and descriptive)
total_marks = 480
# ❌ Not recommended (unclear) tm = 480Explanation:
Usingtotal_marksmakes it clear that the variable stores total marks. In contrast,tmis 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.