Python Identifiers and Reserved keywords
A Python identifier is a name used to identify a variable, function, class, module or other object.
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
Eg: 1student is invalid student1 is valid
# 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
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