Python Identifiers and Reserved keywords

Python Identifiers

A Python identifier is a name used to identify a variable, function, class, module or other object.
(OR)
A name in Python program is called as Identifier. Which can be used for Identification purpose like variable, function, class ,module soon on.

Rules for Identifiers

  • An identifier is a sequence of letters such as uppercase(A to Z),lowercase(a to z),digits(0 to 9) and an underscore (_) ie “Alphanumeric “ characters.
  • It does not allow punctuation characters such as @, $, and % within identifiers
  • It is a case sensitive ( Eg: Student and student are different identifiers)
  • Reserved words cannot be used as identifiers.
  • It cannot start with a digit. (Python thinks it is a number)
    Eg: 1student is invalid 
         student1 is valid 
    	      
  • Sample Code

        # Rule 1 Example :
        EMPLOYEENAME = ""
        employeename = ""
        employee_Name = ""
        employee1 = ""
    
        # Rule 2 - Invalid Identifiers
        employeename@ 
        employeename$ 
        employeename% 
        
        Output:
        File "", line 2
            employeename@ = ""
                          ^
        SyntaxError: invalid syntax
    
        # Rule 3 - Case Sensitive - Student and student are different identifiers
        Student = "Vinoth"
        student = "Anand"
        
        print(Student)
        print(student)
        
        Output:
        Vinoth
        Anand
    
        # Rule 4 - It cannot start with a digit - Invalid
        1Employee = ""
        
        Output:
          File "", line 2
            1Employee = ""
                    ^
        SyntaxError: invalid syntax
    
        # Rule 5 - Reserved words cannot be used as identifiers.
        if = ""
        while = ""
        
        Output:
          File "", line 2
            if = ""
               ^
        SyntaxError: invalid syntax
    	

    Reserved words(Keywords)

    Definition : In python some words are reserved to represent some meanings or functionality. Such type of words are called reserved words

    Rules for Reserved words

    • All the Python keywords must be in lowercase letters only.
    • It Cannot use them as constant or variable or any other identifier names.
    # Program to print all the keywords 
    
    import keyword
    print(keyword.kwlist)
    
    # Total count of the keywords
    print(len(keyword.kwlist))
    
    # Note - Till the previous version the totaly number of keyword is 33.
    # Now 'async', 'await' are considered as new keywords in the python. 
    # Total count is 33 + 2 = 35
    
        Output:
    ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await','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']
    35
    	   
    Total Website Visits: 40420
    © Copyright 2018 - 2020. All Rights Reserved.

    Developed by Vinoth Rathinam

    This Post Has One Comment

    1. Anand

      Wonderful sight for beginners

    Leave a Reply