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:

  1. 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.

  2. 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 and my var are invalid and will give an error. Use underscores (_) instead, like my_var.

  3. Variable names are case-sensitive

    This means Name and name 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" and name stores "Sanchar". Python prints both separately based on their exact spelling and case.

  4. 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 and if cannot be used as variable namesbecause they are reserved for special purposes in the language.

  5. Variable names should be meaningful

    Use descriptive variable names like total_marks instead of short or unclear ones like tm 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.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar