Intro to Programming: Naming Variables

Basics of Python Syntax / Intro to Programming

Yeldos Balgabekov
3 min readDec 30, 2022

This lesson is written in the co-authorship with artificial intelligence and lots of manual edits of a human being. The rest of the course can be found here: “Intro to Programming”.

As part of the course “Intro to Programming”, I have developed in the co-authorship with Chat-GPT3, previously, we talked about Variables and the benefits they give programmers. In this tutorial, we will talk about proper ways to name your variables.

Naming

In Python, there are a few rules and conventions to follow when naming variables:

  1. Variable names must start with a letter or an underscore (_). They cannot start with a number.
  2. Variable names can contain letters, digits, and underscores. They are case-sensitive, so myVariable and myvariable are considered to be different variables.
  3. Variable names should be descriptive and meaningful, to make your code more readable and easier to understand. For example, student_name is a more descriptive and meaningful name for a variable than x.
  4. Variable names should not be the same as Python keywords, such as for, while, if, etc. These keywords are reserved for special use in Python and cannot be used as variable names.
  5. Variable names should follow a naming convention, such as snake_case (e.g. student_name) or camelCase (e.g. studentName). This helps to make your code more consistent and easier to read.

Here are some examples of valid variable names in Python:

x
my_variable
student_name
studentName
_private_variable

And here are some examples of invalid variable names in Python:

1x  # Cannot start with a number
my-variable # Cannot contain a hyphen
for # Cannot be the same as a Python keyword

Making code more readable and maintainable

It is worth noting that besides technical limitations, there are some conventional ones that makes your code better. When you name variables, you should use descriptive and meaningful names. Otherwise, your code would be more difficult to understand. It especially becomes painful on larger projects, where it can be hard to keep track of all the different pieces of data and logic.

Imagine you want to calculate the average height of a group of trees. The below code is with poorly chosen variable names:

# Calculate the average of three trees
x = 10
y = 20
z = 30
average = (x + y + z) / 3
print(average) # Output: 20

Without variables, it might be difficult to understand the meaning of the x, y, and z variables, especially if you are reading the code for the first time or if you are working on a large project with many different variables.

With variables, you could write the same code like this:

# Calculate the average height of three trees
height1 = 5.6 # meter
height2 = 6.0 # meter
height3 = 5.2 # meter
average_height = (height1 + height2 + height3) / 3
print(f"The average height of the three trees is {average_height:.1f} meteres.")
# Output: "The average height of the three trees is 5.5 meters."

In this example, the variables height1, height2, and height3 have meaningful names that clearly indicate the purpose and context of the data. This makes it easier to understand the code, and it also makes it easier to reuse the code or modify it later if needed.

Remember, I am always open for a feedback! So, drop your questions, corrections, and any other thoughts in the comments below.

For more complex data types, see this lesson about Lists

--

--