Technical Roadmap

Naming a Variable in Python

Before creating a variable let's learn some rules to name a variable.

What is an Identifier?

Identifier is a specific name used to identify a variable, function, class, module, or other object. We use this name to access the data we stored in memory

The Logicoria Tip

An identifier is like an Aadhaar ID: just as two people cannot have the same ID, two variables in the same code cannot share the same name.

Rules for Naming a Variable

  • Use only letters (a-z, A-Z), digits (0-9), and underscores (_).
  • An identifier cannot start with a number.
  • Python is case-sensitive. Age, age, and AGE are all different variables.
  • You cannot use Python's reserved keywords to name a variable..
  • Cannot use symbols like @, $, %, !, or spaces anywhere in the name.
PYTHON
name="Logicoria"
article_name="Naming a variable"
print(name)
print(article_name)

Output

Logicoria
Naming a Variable
  • Snake Case: Use lowercase with underscores for variables.
    Example: user_name
  • Pascal Case: Start every word with a capital letter for Classes.
    Example: UserProfile
  • Constants: Use ALL CAPS for values that never change.
    Example: PI = 3.14

Naming Comparisons

Good Practice Bad Practice
user_age = 20 (Descriptive) a = 20 (Vague)
is_logged_in = True (Clear intent) var = True (Meaningless)
total_price = 50.5 (Snake Case) Totalprice = 50.5 (Messy casing)

Quick Revision

  • Letters, digits, and underscores (_) only.
  • Must start with a letter or underscore. No digits.
  • age and Age are different variables.
  • Never use reserved words