Home » Python Keywords and Identifiers

Python Keywords and Identifiers

Python Keywords and Identifiers

In Python programming, Python keywords are fundamental, reserved terms with unique meanings recognized by the compiler. These keywords cannot be employed as names for variables, functions, or other identifiers, as they play a crucial role in shaping the syntax and structure of the Python language.

What are Keywords in Python?

Python Keywords are case-sensitive, with the exception of “True”, “False”, and “None”, which are capitalized. All other keywords should be used exactly as they appear, in lowercase.

Below is a comprehensive list of Python Keywords

CategoryKeywordsDescription
Boolean ValuesTrue, FalseRepresent boolean values for truthy and falsy conditions.
Namespace & Control Flowimport, from, as, pass, break, continue, return, yieldManage modules and control program flow.
Function & Class Definitionsdef, lambda, classDefine functions, lambda (anonymous) functions, and classes.
Loop & Conditional Constructsif, elif, else, for, while, try, except, finallyImplement decision making and loops; handle exceptions.
Variable Scope & Declarationsglobal, nonlocalSpecify variable scope within functions or blocks.
Logical Operationsand, or, notPerform logical operations for decision making.
Comparisons & Identityis, inTest object identity and membership in sequences.
Async/Awaitasync, awaitDefine asynchronous coroutines and wait for their completion.
MiscellaneousNone, del, with, assertRepresent the absence of a value, delete objects, context management, and debugging checks.

In different versions of Python, the words we mentioned earlier might change. Some new words might be added, and some old ones might be taken out. To find out all the keywords in the Python version you are using, write the code given below

code

import keyword  
     
print("list of keyword : ")  
print( keyword.kwlist )
Python

output

list of keyword : 
['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']
Python

Identifiers in Python Keywords

An identifier is a name that you create for things like variables, functions, classes, or modules in Python. It can contain letters, numbers, and underscores, but it’s important to remember that Python treats uppercase and lowercase letters differently. For example, ‘geekster’, ‘Geekster’, and ‘GEEKSTER’ are all seen as separate identifiers in Python. It’s a good idea to choose names for identifiers that make sense and help others understand your code better.

Rules for Naming Python Identifiers

  • It cannot be a reserved Python keyword.
for = 3 # because "for" is a keyword   </mark>
<mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-background-color">// This is invalid
Python
  • Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _.
  • An identifier cannot start with a digit.
1Geekster = 4 // is invalid

Geekster1 = 4 // is valid
Python
  • We cannot use special symbols like !@#$, % , etc. in our identifier.
  • we can not use white space in our identifier.
  • It should start with an alphabet character or an underscore ( _ ).

Things to Remember

Python is a case-sensitive language means uppercase and lowercase letters matter. So, Geekster and geekster are not the same.

When you name things, make sure the name makes sense. For example, instead of just l = 10, consider using length = 10. It’s easier to understand later.

Conclusion

Understanding Python keywords and identifiers is foundational for anyone learning to program in Python. Keywords are the building blocks of the Python language, dictating its structure and flow, while identifiers are the names you give to the various program elements like variables, functions, and classes. It’s crucial to follow the rules for naming identifiers to ensure your code is readable and maintainable. Remember, Python is case-sensitive, which means identifier names must be used consistently regarding their case. By adhering to these guidelines and best practices, you can write clearer, more effective Python code that is easier for others to read and understand. Always strive for meaningful names that convey the purpose of the variable or function, as this will make your code not just correct, but also intuitive and accessible to others.

Frequently Asked Questions