Show Menu
Cheatography

Python Naming Conventions Cheat Sheet by

Common Python naming conventions and their specific uses.

Reference

Python Class (10-15­2-117)
Reference: PEP 8 - Style Guide for Python Code

The Python Quick Rule

Use in Python
Preferred Convention
Example
Variables
snake_case
total_cost
Functions and methods
snake_case
calcul­ate­_to­tal()
Modules and packages
short snake_case
grade_­too­ls.py
Classes
PascalCase
GradeC­alc­ulator
Exceptions
PascalCase
Invali­dGr­ade­Error
Constants
UPPER_­SNA­KE_CASE
MAX_AT­TEMPTS = 3
Use snake_case for most names, PascalCase for classes, and UPPER_­SNA­KE_CASE for
constants. Names should describe the value, behavior, or respon­sib­ility they represent.

Common naming conven­tions

Convention
Example
Where you may see it
Python status
lowercase
score
Simple names
Common
snake_case
studen­t_score
Python variables, functions, methods, modules
PEP 8 preferred
PascalCase (Capit­alCase)
Studen­tScore
Classes and exceptions
PEP 8 preferred
camelCase
studen­tScore
JavaSc­ript, Java, C#
Avoid for Python names
UPPER_­SNA­KE_CASE (SCREA­MIN­G_S­NAK­E_CASE)
MAX_SCORE
Constants and config­uration
PEP 8 preferred
kebab-case (train­-case)
studen­t-score
URLs, CSS classes, comman­d-line options
Not valid for Python identi­fiers
dot.case
studen­t.score
Module paths and attribute access
Not a single identifier
Title Case
Student Score
Headings and human-­facing labels
Not a Python identifier
flatcase
studen­tscore
Occasional file names or labels
Rare; less readable
UPPERCASE
MAXSCORE
Acronyms or legacy systems
Rare; avoid for new Python names

Python­-sp­ecific underscore conven­tions

Pattern
Example
Meaning
Leading underscore
_cache­d_r­esult
Conven­tio­nally internal or non-pu­blic. from module import * does not import it.
Trailing underscore
class_
Avoids a conflict with a Python keyword or built-in name.
Double leading underscore
__balance
Python applies name mangling inside a class to help avoid accidental subclass conflicts.
Double leading and trailing unders­cores (dunder)
__init__, __len__
Special names defined by Python. Do not invent new dunder names.

Identifier rules

• Start with a letter or unders­core, followed by letters, digits, or unders­cores.
• Do not start with a digit: 2nd_at­tempt is invalid.
• Do not use spaces or hyphens: student score and studen­t-score are invalid.
• Python is case-s­ens­itive: score, Score, and SCORE are different names.
• Do not reuse Python keywords such as class, for, if, or return.
• Prefer descri­ptive names: averag­e_score is clearer than avg when space allows.

PEP 8 reminders

• Choose names that reveal intent: is_com­plete is clearer than flag.
• Use verbs for actions: save_f­ile(), calcul­ate­_to­tal(), and displa­y_m­enu().
• Use nouns for values and objects: studen­t_name, shoppi­ng_­cart, and report.
• Use boolean names that read like questions: is_valid, has_ac­cess, and can_su­bmit.
• Be consis­tent. A project using studen­t_name should not also use studen­tName.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter

          More Cheat Sheets by CashM

          Big-O Notation Cheat Sheet
          Firebase-Express Template with User Auth Cheat Sheet
          React & Material UI Project startup Cheat Sheet