Python Keywords and Identifiers

Learn via video course
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
Start Learning

Keywords in Python are unique reserved words that cannot be used as a variable, function, or other identifier. These special words define the syntax and structure of the Python language. On the other hand, an identifier is a name used to identify entities like class, functions, variables, etc.

Python Keywords

In Python programming, keywords are reserved words that hold a specific meaning and are a part of the language's syntax. Understanding the rules governing using keywords is essential for writing syntactically correct and efficient Python code. Here are the key rules and guidelines related to Python keywords:

Rules for Keywords in Python

  1. Reserved Use: Keywords have specific, predefined uses and cannot be altered.
  2. Case Sensitivity: Python keywords are case-sensitive.
  3. No Identifier Usage: Keywords cannot be used as variable names, function names, or other identifiers.
  4. Fixed Number: The keywords are fixed and vary with Python versions.
  5. Non-Modifiable: Keywords are part of Python's syntax and cannot be modified.
  6. Contextual Keywords: Some words act as keywords only in specific contexts but can be used as identifiers elsewhere.
  7. Syntax Compliance: Keywords must be used according to Python's syntax rules.

List of Python Keywords

FalseRepresents the boolean value false.
NoneRepresents the absence of a value or a null value.
TrueRepresents the boolean value true.
andA logical operator used to combine conditional statements.
asUsed to create an alias while importing a module.
assertUsed for debugging purposes to check if a statement is true.
breakBreaks out of the current closest enclosing loop.
classUsed to define a new user-defined class.
continueContinues to the next iteration of the current loop.
defUsed to define a function.
delUsed to delete an object.
elifUsed in conditional statements, same as else if.
elseUsed in conditional statements.
exceptUsed with exceptions, what to do when an exception occurs.
finallyUsed with exceptions, a block of code that will be executed whether there is an exception or not.
forUsed to create a for loop.
fromUsed to import specific parts of a module.
globalDeclares a global variable.
ifUsed to make a conditional statement.
importUsed to import a module.
inChecks if a value is present in a list, tuple, etc.
isTests object identity.
lambdaCreates an anonymous function.
nonlocalDeclares a non-local variable.
notA logical operator used to invert the truth value.
orA logical operator used to combine conditional statements.
passA null statement, a statement that will do nothing.
raiseUsed to raise an exception.
returnUsed to exit a function and return a value.
tryMakes a try-except statement.
whileUsed to create a while loop.
withUsed to simplify exception handling.
yieldEnds a function, returns a generator.

Python Identifiers

In Python, identifiers are names used to identify a variable, function, class, module, or other object. They are essentially user-defined names to represent these entities in a program. The purpose of identifiers is to make the code readable and meaningful, as they can describe the role of the entity they are naming.

Rules for Naming Python Identifiers

  1. Start with Letters or Underscore: Identifiers must begin with a letter (A-Z, a-z) or an underscore (_). They cannot start with a digit.

  2. Case Sensitive: Python identifiers are case-sensitive. This means myVar, MyVar, and myvar are three different identifiers.

  3. Contain Alphanumeric Characters and Underscores: After the first letter or underscore, identifiers can contain letters, digits (0-9), and underscores.

  4. No Special Characters: Identifiers cannot contain special characters like @, $, !, etc.

  5. Avoid Keywords: Identifiers should not be any of Python’s reserved keywords. For example, you cannot name an identifier if or for.

  6. Length: There is no length limit for Python identifiers. However, keeping them within a reasonable length is good practice to maintain readability.

  7. Conventions: Python follows certain naming conventions like using lowercase for variable names, uppercase for constants, and camelCase or snake_case for multi-word identifiers. Python does not enforce these, but they are good practices.

Examples of Python Identifiers

Valid Python Identifiers:

  • Variables: height, max_value, _data
  • Functions: calculate_area(), get_info()
  • Classes: User, StockPrice
  • Constants: MAX_LIMIT, PI

Invalid Python Identifiers:

  • Starting with a digit: 1variable (invalid)
  • Containing special characters: user-name (invalid)
  • Using Python keywords: for, if (invalid)

These examples follow the naming rules and conventions for Python identifiers. Identifiers are a critical part of the code as they give meaning to the different parts of the program, making it easier to understand and maintain.

Python Keywords and Identifiers Examples

Let's go through some examples of Python keywords and identifiers:

Example 1: Use of async and await Keyword


Example 2: Use of from Keyword


Example 3: Use of is Keyword


Example 4: Use of import Keyword


Example 5: Use of finally Keyword


Example 6: Use of pass Keyword


Example 7: Use of assert Keyword


Example 8: Example of yield Keyword


Example 9: Use of global Keyword


Example 10: Use of del Keyword


Example 11: return Keyword


Example 12: lambda Keyword


Example 13: try, except, raise


Example 14: def, if, and else Keywords


Example 15: for, in, if, elif, and else Keywords


Example 16: break, continue Keywords and Identifier


Example 17: and, or, not, True, False Keywords


These examples provide a practical overview of how different Python keywords and identifiers are used in programming and their expected outputs.


  • Python keywords are reserved words with specific, pre-defined meanings and roles in the language's syntax.
  • Identifiers are user-defined names given to various programming entities like variables, functions, classes, etc.
  • Keywords are case-sensitive and have rules governing their use, ensuring syntactical correctness in Python code.
  • Identifiers also follow specific naming conventions, including starting with a letter or underscore, being case-sensitive, and avoiding using special characters and Python keywords.
  • Practical examples of keywords and identifiers demonstrate their usage in real-world Python programming, providing insights into how these elements contribute to the language's structure and functionality.
  • Understanding and correctly implementing Python keywords and identifiers is crucial for writing efficient, error-free, and readable Python programs.

See Also