voidex/20-29 Areas/20 Programming/20.01 Python/variableNamingBestPractices.md

1.3 KiB
Raw Permalink Blame History

title tags aliases
variableNamingBestPractices
python
variables
syntax
naming
constants
naming conventions
snake_case
ALL_CAPS
variable reassignment

[!important] Naming Best Practices

  • Use snake_case for variables: user_name, email_count

  • Use ALL_CAPS for constants: MAX_CONNECTIONS = 10

    • Python doesn't enforce constants, but the convention is SCREAMING_SNAKE_CASE
    • Define constants at the module level for global visibility
  • Avoid unintentional reassignment. Python allows variable re-binding to different types:

my_var = 1
my_var = "Now I'm a string"
  • Variables dont auto-update in expressions. You must reassign explicitly:
bacon = bacon + 1
# or
bacon += 1

[!example] Reassignment Example

first_variable = 1
first_variable = 2
print(type(first_variable))  # <class 'int'>
print(first_variable)        # 2

first_variable = "I'm a string now! I used to be an integer."
print(type(first_variable))  # <class 'str'>
print(first_variable)

[!info] Related Notes
variableNamingRules
typeConversions
pythonSyntaxOverview