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

Q1. Why can’t Python keywords be used as identifiers?

Ans: Python keywords are reserved words that have a special meaning to the Python interpreter. They define the syntax and structure of the Python language. Using them as identifiers would lead to ambiguity and confusion in understanding what the code is supposed to do.


Q2. Are Python keywords always in lowercase?

Ans: Mostly, yes. The majority of Python keywords are in lowercase (e.g., import, if, else, for). However, there are exceptions like True, False, and None, which are capitalized to distinguish them as the boolean values and the representation of the absence of a value.


Q3. How can I check the list of keywords in my Python version?

Ans: You can use the keyword module, which comes with Python. By printing keyword.kwlist, you can see a list of all keywords defined in your Python interpreter version. This is particularly useful as keywords may differ between versions.


Q4. What are the main rules for naming Python identifiers?

Ans: A4: The key rules include: Identifiers cannot be Python keywords, they can consist of letters (a-z, A-Z), digits (0-9), and underscores (_), cannot start with a digit, cannot contain special symbols like !, @, #, etc., and should not include whitespace.


Q5. Can Python identifiers start with an underscore?

Ans: Yes, identifiers can start with an underscore. However, identifiers starting with a single or double underscore have special meanings in Python, such as indicating a weak “internal use” or “name mangling” for inheritance respectively, so their use should be thoughtful.