Python - Variable Name Rules

Introduction

In Python, names come into existence when you assign values to them.

Syntax

(underscore or letter) + (any number of letters, digits, or underscores) 

Variable names must start with an underscore or letter.

They can be followed by any number of letters, digits, or underscores.

_test, test, and Test_1 are legal names,

1_Test, test$, and @#! are not legal names.

Case

TEST is not the same as test

The names X and x refer to two different variables.

Names you define cannot be the same as reserved words in Python.

Naming conventions

Names with leading and trailing underscores, such as __X__, are system-defined names that have special meaning to the interpreter.

Names that begin with two underscores and do not end with two more like __X are localized to enclosing classes.

The name with single underscore _ retains the result of the last expression when you are working interactively.

Related Topic