1. Python Identifiers

Identifiers in Python are names used to identify variables, functions, classes, modules, or other objects. Rules for Python identifiers are:

  • Must start with a letter (a-z, A-Z) or underscore (_).
  • Can only contain letters, digits (0-9), or underscores (_).
  • Case-sensitive (myVar and myvar are different identifiers).

Valid Identifiers Examples

  variable_name = "Hello"
class_name = "MyClass"
function_name = "calculate_sum"
  

Invalid Identifiers Examples

  # Invalid identifiers due to starting with a digit
2nd_variable = "Value"

# Invalid identifier due to containing special characters
variable@name = "Invalid"
  

2. Reserved Words (Keywords)

Reserved words, or keywords, are predefined names in Python that have special meanings and cannot be used as identifiers (variable names, function names, etc.).

List of Python Keywords

Python has a set of keywords that are reserved for its syntax and cannot be used as identifiers. Here are some examples:

  # List of Python keywords
import keyword
print(keyword.kwlist)
  

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']
  

Examples of Using Keywords

  # Using keywords as identifiers will raise a syntax error
try = 10  # SyntaxError: invalid syntax
for = "iteration"  # SyntaxError: invalid syntax