Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Variable naming might seem like a minor detail, but it's one of the first things that separates readable, professional code from confusing spaghetti. You're being tested on your ability to write code that works and code that communicates—naming conventions directly impact both. Understanding these rules helps you avoid frustrating syntax errors, collaborate effectively with other programmers, and write self-documenting code that you'll actually understand when you revisit it weeks later.
These conventions connect to broader programming principles: code readability, maintainability, debugging efficiency, and community standards. Python's philosophy emphasizes that "readability counts" (it's literally in the Zen of Python), and naming conventions are your first tool for achieving that. Don't just memorize the rules—understand why each convention exists and when to apply it. That's what separates coders who can answer exam questions from programmers who can build real projects.
These aren't suggestions—they're enforced by the Python interpreter. Break these rules and your code won't run at all.
2fast or 99problems; Python's interpreter can't parse themmy-variable into a subtraction operationif, for, while, class, and return that cannot be used as variable namesimport keyword; print(keyword.kwlist) to see the complete list in your Python versionCompare: Starting with a number vs. using a keyword—both cause errors, but number-starts fail immediately at parsing while keyword conflicts may produce confusing error messages. If debugging a "why won't this run" question, check both.
These won't break your code, but they'll make experienced Python developers cringe—and cost you points on style-focused assessments.
user_name not userName or UserNametotal_monthly_revenueMAX_CONNECTIONS or API_TIMEOUT tell other programmers "don't change this"StudentRecord, HttpConnection, GameCharacterHTTPServer rather than HttpServer (though both appear in practice)Compare: user_score (variable) vs. USER_SCORE (constant) vs. UserScore (class)—same words, completely different meanings. Exam questions love testing whether you can identify what type of identifier each represents.
These conventions separate code that merely works from code that communicates intent clearly.
customer_email beats ce, and calculate_tax() beats do_stuff()temp is fine for a temporary swap variable but terrible for storing temperature datai, j, k are universally understood in for i in range(10) contextsy = m * x + b is clearer than result = slope * input_value + interceptcamelCase and snake_case is harder to read than either alonepylint and flake8 automatically check PEP 8 compliance, catching naming issues before submissionCompare: x = 5 in a loop vs. x = 5 storing a user's age—identical syntax, completely different appropriateness. The key question: "Will someone reading this code (including future you) immediately understand what this variable holds?"
| Concept | Best Examples |
|---|---|
| Valid syntax starters | Letters (a-z, A-Z), underscore (_) |
| snake_case variables | user_name, total_count, is_valid |
| CONSTANT naming | MAX_SIZE, DEFAULT_TIMEOUT, PI |
| CamelCase classes | StudentRecord, GameEngine, HttpRequest |
| Acceptable single letters | i, j, k (loops), x, y (coordinates/math) |
| Reserved keywords to avoid | if, for, while, class, return, import |
| Forbidden characters | @, #, $, %, -, spaces |
| Style guide reference | PEP 8 |
You see three identifiers: user_data, USER_DATA, and UserData. What type of Python element (variable, constant, or class) does each naming convention suggest?
Why would 2nd_place cause a syntax error while second_place works fine? What's the underlying rule?
Compare calculate_average(numbers) versus calc(n). Which follows Python conventions better, and what readability principle does this demonstrate?
A classmate names their loop counter current_iteration_index instead of i. Is this technically wrong? What would you advise them and why?
FRQ-style: Given a program tracking student grades, propose appropriate variable names for: (a) a single test score, (b) a constant representing the maximum possible score, and (c) a class representing a student. Explain which naming convention applies to each.