The
variable name is arbitrary but programmers generally choose names for their
variables that are meaningful. It's easier to use meaningful names for
variables for better understanding of programming code. Also a very useful tip
is to document (comment) in the programming code what the variable is used for.
The
length of the variable name is also arbitrary and they can contain letters and
numbers but they have to begin with a letter. It’s legal to used uppercase
letters, but it’s a good idea to begin variable names with lowercase letters.
The
special character underscore (_) can also appear in a name. Variable names can
start with an underscore, but in general they are mostly used in writing
library code which is intended for others to use.
The
following example is example of illegal variable name. The Python will return a
syntax error.
>>>69mustang = ‘Ford Mustang’ SyntaxError: invalid syntax >>>mail@=190 SyntaxError: invalid syntax >>>class = ‘Python’ SyntaxError: invalid syntax
69mustang
is illegal variable name because it doesn’t begin with a letter. mail@ is
illegal because it contains special character which is illegal for variable
name in python. Class is also illegal because it is reserved by Python. In
other words the class is one of the Python keywords. The interpreter uses
keywords to recognize the structure of a program, and they cannot be used as
variable names.
To
find out which keywords are reserved by Python in Python type the following
code.
>>> import keywords >>>keyword.kwlist ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
If you type import keywords the Python interpreter
will call file called “keywords.py” and if you type keyword.kwlist it will list
out all the keywords used by Python.
If you’re using Spyder program for Python programming
you have to type print keyword.kwlist
in order to print out all the keywords used by Python programming language.
Pay attention to variable names. If the interpreter
complains about one of your variable names and you don’t know why, check this
list to see if you’re variable name is on this list.
Nema komentara:
Objavi komentar