Venkatesh Kamath N
PRO
last week
Venkateshcountry asked

Are there any conventions used for variable naming especially in Python?

N
Expert
last week
Nisha Sharma answered

Hello there, good question.

Yes, Python does have some pretty standard naming conventions, and following them makes your code way easier to read (especially when you come back to it later).

  • Use clear, specific names

    • Good: favorite_food, total_price, student_count

    • Not so good: x, temp, data (unless it truly is temporary or generic)

  • Use snake_case for variables

    • Python style is lowercase words separated by underscores (known as snake_case):

    • favorite_food instead of favoriteFood

  • Don’t use Python keywords

    • You can’t name variables things like class, def, if, for, etc.

    • If you really need something close, people often do: class_ (with an underscore at the end)

  • Be consistent with casing

    • Python treats score, Score, and SCORE as different names.

    • Most variables stay lowercase: score, high_score, max_score

  • Special naming patterns you’ll see a lot

    • Constants (values you don’t plan to change): MAX_SPEED, PI, TAX_RATE

    • ā€œPrivateā€ / internal use (a hint to other programmers): _hidden_value

    • Avoid names that look like built-ins: don’t name a variable list, str, or sum (you’ll overwrite the built-in function by accident)

If you stick to descriptive names + snake_case, your code will instantly look more professional and be easier to understand.

Feel free to reach out if you have any questions.

Python
This question was asked as part of the Learn Python Basics course.