DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 6 of 77
←PreviousPrevNextNext→

Variable Names

Naming Rules

Python has specific rules for variable names:

  • Must start with a letter (a-z, A-Z) or underscore (_)
  • Can contain letters, numbers, and underscores
  • Case-sensitive (`age`, `Age`, and `AGE` are different)
  • Cannot be a Python keyword (`if`, `for`, `while`, etc.)
  • No special characters (!, @, #, etc.)
⚠️ Warning: Avoid single-character names except for counters or math variables.

Naming Conventions

ℹ️ Note: PEP 8 is Python's official style guide for naming conventions.
StyleExampleWhen to Use
snake_caseuser_nameVariables, functions
PascalCaseClassNameClasses
CAPS_SNAKEMAX_SIZEConstants
_private_internal_varNon-public variables
__dunder__init__Special methods

Good vs Bad Names

Example
# Good names
customer_count = 100
account_balance = 500.50
is_logged_in = True

# Bad names
a = 100          # What does 'a' represent?
bal = 500.50     # Abbreviation unclear
flag = True      # What does this flag indicate?

Good names should:

- Clearly indicate purpose

- Be pronounceable

- Use consistent style

- Avoid abbreviations unless well-known

Special Cases

Python has some special naming scenarios:

⚠️ Warning: Avoid shadowing built-in names as it can cause subtle bugs.
Example
# Name mangling for class privates
class MyClass:
    def __init__(self):
        self.public = 1
        self._protected = 2
        self.__private = 3  # Gets renamed to _MyClass__private

# Reassigning built-in names (not recommended)
str = "Hello"  # Shadows built-in str() function
list = [1,2,3]  # Now can't use list() constructor
Test your knowledge: Variable Names
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 6 of 77
←PreviousPrevNextNext→