Learn python variable naming rules with your child

  • 2020-10-23 20:09:35
  • OfStack

Variable naming rules

Here are some rules for variable names (also known as identifiers)

Must begin with 1 letter or 1 underscore character. This can be followed by a sequence of 1 letter, number, or underscore character of any length.

Letters can be uppercase or lowercase; case is different. In other words, Ax is different from aX.

A number can be any numeric character from 0 to 9, including 0 to 9.

No characters other than alphabetic, numeric, and underscore characters are allowed. Spaces, punctuation marks, and other characters are not allowed in variable names.

The only special character allowed is the underscore character. In case you don't know what this is, here are some examples:

[

First_number=15
John Student_name = ""

]

Characters between First and number are underscores, and there is also an underscore between student and name. Programmers sometimes use an underscore to separate two words of a variable name. Because Spaces are not allowed in variable names, they use underscores.

It is recommended that you do not use underscore characters at the beginning and end of variable names unless you know exactly why. In some cases, the use of an underscore character at the beginning and end of an identifier may have special meaning. So avoid using this:

[

_first_number=15
John Student_name = ""

]

Here are 1 examples of valid variable names:

My_answer
answer23
answer_23
YourAnswer
Your2ndAnswer

Here are some examples of illegal variable names:

23answer (variable names cannot begin with Numbers)

Your-answer(no hyphens allowed)

My answer (no Spaces allowed)


Related articles: