Python Identifiers & Reserved Keywords (Detailed Guide)

๐Ÿ”น Identifiers in Python

What is an Identifier?

An identifier is the name used to identify variables, functions, classes, modules, and objects in Python.

โœ… Examples of Identifiers:

name = "Alice"      # Variable identifier
def add_numbers():  # Function identifier
    pass

class Student:      # Class identifier
    pass

Rules for Naming Identifiers in Python

  1. Can contain letters (A-Z, a-z), digits (0-9), and underscores (_)
    • โœ… my_variable, Class1, num_2 are valid.
  2. Must start with a letter (A-Z, a-z) or an underscore (_) but NOT a digit (0-9)
    • โœ… _temp, var_name are valid.
    • โŒ 2variable is invalid.
  3. Cannot be a Python reserved keyword (e.g., if, else, while)
    • โŒ class = 10 is invalid because class is a reserved word.
  4. Identifiers are case-sensitive
    • MyVar, myvar, and MYVAR are different.
  5. Can be of any length but should be meaningful for readability.
  6. Cannot contain special characters (@, #, $, %, &, etc.).
    • โŒ user@name is invalid.

โœ… Valid Identifiers:

age = 25
_name = "Alice"
class1 = "Python"  # Not a reserved keyword

โŒ Invalid Identifiers:

2name = "Bob"     # Starts with a digit โŒ
user@name = "Tom" # Special character โŒ
class = "Math"    # Reserved keyword โŒ

๐Ÿ”น Python Reserved Keywords

Reserved keywords (also called keywords) are special words in Python that cannot be used as identifiers because they have a predefined meaning.

List of Python Keywords (Python 3.x)

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

Using keyword Module to Get Keywords

You can check all Python keywords using the keyword module:

import keyword
print(keyword.kwlist)  # Prints a list of all reserved keywords

Examples of Reserved Keywords Usage

โœ… Correct Usage (As Keywords)

if True:
    print("This is valid")  # 'if' is used correctly as a keyword

def my_function():          # 'def' is correctly used to define a function
    pass

โŒ Incorrect Usage (As Identifiers)

class = "Python"  # โŒ Invalid: 'class' is a keyword

def = 10          # โŒ Invalid: 'def' is a keyword

โœ… Corrected Version

class_name = "Python"  # โœ… Valid identifier
function_name = 10     # โœ… Valid identifier

๐Ÿ”น Summary

Feature Identifiers Keywords
Definition User-defined names for variables, functions, etc. Reserved words with predefined meanings
Rules Letters, digits, and _, cannot start with digits or special characters Cannot be used as variable or function names
Examples my_var, Student, _temp123 if, else, def, class, import
Case-Sensitive? โœ… Yes (MyVar โ‰  myvar) โœ… Yes (True is different from true)
Customizable? โœ… Yes โŒ No

โœ… Conclusion

  • Identifiers are names given to variables, functions, and classes, but they must follow certain rules.
  • Reserved Keywords are built-in words in Python that cannot be used as identifiers.
  • Use meaningful identifiers to make code readable.

Would you like examples of best practices for naming variables and functions? ๐Ÿ˜Š


Leave a Reply

Your email address will not be published. Required fields are marked *