
Variable naming rules
Basic
In Python, the characters that can be used in variable names are specified.
- a-z
- A-Z
- 0-9
- _
Define variable names by combining lowercase and uppercase letters, numbers from 0 to 9, and underscores (“_”).
In Python, upper and lower case letters are distinguished, so the following variables are treated as different.
NUM = 1
num = 2
nUM = 3
To confirm that data is being managed separately for each variable, write the following code and execute it.
(In the author's environment, a new file named sec01_learning02_variables.py is created.)
NUM = 1
num = 2
nUM = 3
print(NUM)
print(num)
print(nUM)
When executed, the values should be displayed as shown in the figure below. “NUM”, “num”, and “nUM” are recognized as separate variables and can be confirmed to hold data.

Combination of alphanumeric characters and underscores
The previous examples were all composed of letters, but you can also combine numbers and underscores.
num1 = 1
average_score = 1.5
_tmp_data = 100
You can combine letters and numbers, such as “num1” above, or separate words with underscores, such as “average_score,” to represent variable names. You can also use an underscore at the beginning, such as “_tmp_data.”
※ However, numbers cannot be placed at the beginning.
1num = 2 # ERROR
_1num = 2 # OK
For example, in the following code, “1num” will cause an error because it starts with a number. If you want to put a number at the beginning of a variable name, add an underscore first to avoid the error.
The code above contains the terms “# ERROR” and “# OK,” which are comments. In Python, anything to the right of “#” on each line is treated as a comment and is not processed, so it does not affect the results of the code.
In the following code,
# Define two variables
NUM = 1 # Define the NUM variable
num = 2 # Distinguish from NUM
# Output the information of each variable
print(NUM)
print(num)
Comments such as “# Define two variables” or “# Define the NUM variable” have no effect on processing. You can comment out an entire line or add comments after the processing code. In either case, all characters to the right of “#” are treated as comments. (The code to the left of “#” is processed.)
In addition, you may comment out the code to be processed in order to perform debugging and other tasks.
# NUM = 1
NUM = 2.5 # would like to check the result when substituting 2.5 instead of 1.
print(NUM)
In the above case, “NUM = 1” is valid code, but if you want to try other processing or perform debugging, you can comment out that code. This allows you to skip the processing during testing without deleting the code you wrote. (After debugging, if you want to restore the code, you can do so immediately by uncommenting the code without having to rewrite it from the beginning.)
While you are experimenting with various approaches, it is acceptable to have commented-out lines. However, when you finally share the code with your team, be sure to remove any commented-out lines that are not necessary for the processing before sharing. (Note: Comments that explain the behavior of the code can be left as they are.)
Additionally, in Visual Studio Code, place the cursor on the line you want to comment out, press “Ctrl + /
”, and the line will be commented out. If you select multiple lines, all selected lines will be commented out. If the line is already commented out, it will no longer be commented out.