Best practices for naming variables in Python

1

When naming variables in Python there are some best practices that we will discuss. If you don’t follow the best practices you’ll get errors and that will suck.

  • Python variable names can contain only letters, numbers, and underscores.
  • Python variable names can start with a letter or an underscore, but they cannot start with a number. Example:  thisworks_1 will work however 1_thisdoesntwork will not work.
  • Spaces are not allowed in Python variable names.
  • Python variable should not contain keywords and function names as variable names.
  • It is best if your Python variable names are short. You will often see people use x or y or ab but it would be better to use bank_location vs. x or city_name vs cn.
  • Python variable structure is best when using lowercase and stick to a naming convention.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here