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

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

Output

Logicoria
Naming a Variable

Multi-word variable names

For multi-word variable names, you can use these naming conventions to make your code more readable.

  • 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

NOTE: Remember variable names are case-sensitive. Num and num are separate variable.

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)