Identifier naming: Identifiers are names given to identify variables. In most of the programming languages a variable is a named location used to store data in the memory. Each variable must have a unique name called identifier. It is helpful to think of variables as container that hold data which can be changed later throughout programming. Non technically, you can suppose variable as a bag to store books in it and those books can be replaced at anytime.
There are some rules you have to follow for naming identifiers:
· The first character of the identifier must be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore ( _ ).
· The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores ( _ ) or digits (0-9).
· Identifier names are case-sensitive. For example, myname and myName are not the same. Note the lowercase n in the former and the uppercase N in the latter.
· Examples of valid identifier names are i , name_2_3 . Examples of invalid identifier names are 2things, this is spaced out , my-name and >a1b2_c3.
· It is customary to begin them with a lower-case letter and to use underscores when separating words. Beginning with an upper-case letter is not invalid, and some people may prefer camelCase or mixed upper- and lower-case letters when writing their variables, but these are less conventional choices.
· The most important style choice you can make is to be consistent.
· If you begin working on an existing project that has been using camelCase for its variable names, then it is best to continue using the existing style.
· PEP 8 is the official Python code style guide and it addresses many of the stylistic questions you may have about Python.
· In general, readability and consistency are favored over other stylistic concerns.
Variables can hold values of different types called data types. The basic types are numbers and strings, which we have already discussed. In later chapters, we will see how to create our own types using classes.
Note: In Python we don’t assign values to the variables, whereas python gives the reference of the object (value) to the variable.
Mnemonic Variable Names
· As long as you follow the simple rules of variable naming, and avoid reserved words, you have a lot of choice when you name your variables.
· In the beginning, this choice can be confusing both when you read a program and when you write your own programs.
· For example, the following three programs are identical in terms of what they accomplish, but very different when you read them and try to understand them.
#Example 1 |
The Python interpreter sees all three of these programs as exactly the same but humans see and understand these programs quite differently.
· Humans will most quickly understand the intent of the second program because the programmer has chosen variable names that reflect the intent of the programmer regarding what data will be stored in each variable.
· We call these wisely-chosen variable names “mnemonic variable names". The word mnemonic means “memory aid".
· We choose mnemonic variable names to help us remember why we created the variable in the first place.
Reserved Words
Python reserves 31 keywords for its use and these can not be used as variable names.
and |
del |
from |
Not |
while |
as |
elif |
global |
Or |
with |
assert |
else |
if |
Pass |
yield |
break |
except |
import |
|
raise |
class |
exec |
in |
Return |
try |
continue |
finally |
is |
Lambda |
|
def |
for |
|
|
|