Tech With Galvan
Courses/Python Mastery/The Building Blocks (Tokens & Syntax)

Identifiers & Naming Rules

15 min • Beginner • Module 2

How to name variables, functions, and classes — the rules, the conventions, and names that are illegal.

Identifiers & Naming Rules

Identifiers are the names you invent — for variables, functions, classes, files. Keywords belong to Python; identifiers belong to you. But Python has rules about what counts as a legal name, and conventions about what makes a good one.

A valid identifier:

  1. Can contain letters, digits, and underscores: name, user2, my_score
  2. Cannot start with a digit: 2cool ❌ but cool2 ✅
  3. Cannot be a keyword: for ❌ but for_you ✅

Test them yourself:

my_name = "Galvan"       # ✅
score2 = 99              # ✅
2score = 99              # ❌ SyntaxError
my-name = "Galvan"       # ❌ SyntaxError (hyphen reads as minus!)
class = "10th"           # ❌ keyword
_class = "10th"          # ✅ (underscore prefix saves it)

That hyphen example deserves a second look: my-name = "Galvan" fails because Python reads my-name as my minus name — subtraction! Only underscores work inside names.

Case sensitivity — the classic trap

age = 15
Age = 20
AGE = 25

print(age, Age, AGE)     # 15 20 25 — three different variables!

age, Age, and AGE are completely unrelated names. Misspelling capitalization causes a huge share of beginner NameErrors:

name = "Galvan"
print(Name)              # NameError: name 'Name' is not defined

The fix is always: match the capitalization exactly.

The naming conventions (how good code looks)

Python won’t stop you from writing x2q = 42, but the community has agreed-on styles. Follow them and your code looks professional; break them and your code works but reads wrong:

What Convention Example
Variables lowercase_with_underscores total_price, user_name
Constants (never change) ALL_CAPS MAX_SCORE, PI
Functions lowercase_with_underscores calculate_total()
Classes CapitalWords (PascalCase) Student, BankAccount

This style is called snake_case for variables/functions — and it’s not just taste: the official Python style guide (PEP 8) specifies it, and every Python codebase you’ll ever read follows it.

Names should say what they hold

The single most valuable naming advice:

# legal but terrible
x = 99
d = "Galvan"

# legal and clear
score = 99
student_name = "Galvan"

Future-you reads code three weeks later with no memory of writing it. x forces future-you to reverse-engineer the whole function. score explains itself. Code is read 10× more often than it’s written — optimize for the reader.

One accepted exception: short names in tiny scopes are fine — for i in range(10): is universally understood.

Common Errors & Fixes

  • SyntaxError: invalid syntax on a name with a hyphen or leading digit — rename with underscores, or move the digit.
  • NameError on a variable you “already made” — capitalization mismatch. Copy-paste the exact name.
  • SyntaxError: invalid syntax with class, if, for as names — keyword collision; add a suffix or underscore.

✅ Checkpoint

  1. Which are legal: my_var, 2fast, _hidden, my-var, Class? (my_var ✅, 2fast ❌, _hidden ✅, my-var ❌, Class ✅ — it’s not the keyword “class”)
  2. Are score and Score the same variable? (No — identifiers are case-sensitive)
  3. How would you name a variable holding a user’s email? (user_email — snake_case, descriptive)

Next: comments and docstrings — how to write notes that future-you will thank present-you for.