4. Rules to name a Variable

A variable can have a short name (like a and b) or a more descriptive name (name, salary, occupation). There are some rules to name variables in python:

  1. A variable name must start with a letter or the underscore character

          Example: var, Var, VAR, _variable    (Note: Python variable are case sensitive. i.e.,  var & VAR are two different variables.

      2.  A variable name cannot start with a number.

          Example: 9var, 3_var, 4a, etc. are invalid variable names.

      3. A variable name can only contain alpha-numeric characters and underscores (A- z, 0-9, and _ )

          Example:  a3b, a_b, var_name, _ab      # valid variable names

                             2ab, $var, @34b                    # invalid variable names

       4. Variable names are case-sensitive (age, Age and AGE are three different          variables)

        5. A variable name cannot be any of the keywords of python.

            Example: if, elif, for, class, etc   are can not be used as variable names

Video: The following video provides you the knowledge to how to name a variables in python.


"Chapter 2: Naming Variables" by J David Eisenberg is licensed under CC BY-SA 4.0