4. PYTHON KEYWORDS, IDENTIFIERS






1. Keywords:


·      Keywords are special words which are reserved and have a specific meaning.
·      Python has a set of keywords that cannot be used as constants or variables in programs or any other identifier names.
·      All keywords in Python are case sensitive. So, you must be careful while using them in your code.
·      To get hold of the up-to-date list, you can open Python shell and run the following commands as shown in the below snippet.
help> keywords
 
Here is a list of the Python keywords.  Enter any keyword to get more help.
 
False              def                if                  raise
None              del                import          return
True               elif                in                 try
and                 else               is                 while
as                   except           lambda         with
assert             finally           nonlocal       yield
break             for                 not                 
class              from              or                  
continue        global            pass                
 

·      Alternatively, you can use Python’s keyword module, import it straight from the shell and run the below commands to view the supported keywords.

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 
'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 
'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

2. Identifiers:

  A Python identifier is a name used to identify a variable, function, class, moduleor other object. 

•  Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. Thus, Manpower and manpower are two different identifiers in Python.

       Here are naming conventions for Python identifiers −

          1. Class names start with an uppercase letter. All other identifiers start with a lowercase letter.

       2. Starting an identifier with a single leading underscore indicates that the identifier is private.

      3. Starting an identifier with two leading underscores indicates a strongly private identifier.

•  If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.

  §       Avoid using names with only one character. Instead, make meaningful names.

For example – While i = 1 is valid, but writing iter = 1 or index = 1 would make more sense.

   §     You can use underscore to combine multiple words to form a sensible name.
       
        For example – count_no_of_letters

Rules to be followed while creating Identifiers:

 1. To form an identifier, use a sequence of letters either in lowercase (a to z) or uppercase (A to Z). However, you can also mix up digits (0 to 9) or an underscore (_) while writing an identifier.

For example – Names like shorpClassstyle_1, and update_shape_to_db are all valid identifiers.

2. You can’t use digits to begin an identifier name. It’ll lead to the syntax error.

For example – The name, 0Shape is incorrect, but shape1 is a valid identifier.

3. Also, the Keywords are reserved, so you should not use them as identifiers.
>>> for=5
SyntaxError: invalid syntax
>>> True=1
SyntaxError: can't assign to keyword
>>> While=9
SyntaxError: can't assign to keyword
 
   
     4. Python Identifiers can also not have special characters [‘.’, ‘!’, ‘@’, ‘#’, ‘$’, ‘%’] in their formation. 

>>> @index=0
SyntaxError: invalid syntax
>>> isPython?=True
SyntaxError: invalid syntax
>>> $dollar=7
SyntaxError: invalid syntax

     5. Python doc says that Limit all lines to a maximum of 79 characters.

Testing If An Identifier Is Valid or Invalid:

·      You can test whether a Python identifier is valid or not by using the keyword.iskeyword() function. It returns “True” if the keyword is correct or “False” otherwise.
Please refer below

>>> import keyword
>>> keyword.iskeyword("world")
False
>>> keyword.iskeyword("try")
True
>>> keyword.iskeyword("Greater_Noida")
False

·      Another useful method to check if an identifier is valid or not is by calling the str.isidentifier() function. But it is only available in Python 3.0 and onwards.

>>> 'technology'.isidentifier()
True
>>> '1hyderabad'.isidentifier()
False
>>> 'technology_com'.isidentifier()
True


No comments:

Post a Comment