• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Python | Keywords
  1. Literal Keywords
  2. Logical Operators
  3. Control Flow Keywords
  4. Exception Handling
  5. Function and Class Definition
  6. Scope and Import Keywords
  7. Async/Await Keywords
  8. Deletion
  9. Context Management

Python will throw a SyntaxError if you use any of the following keywords as a variable name.

See this page for more details: https://docs.python.org/3/reference/lexical_analysis.html#keywords.

  1. Literal Keywords
    • None: Represents null/empty value (e.g., x = None)

    • True: Boolean true value (e.g., x = True)

    • False: Boolean false value (e.g., x = False)
  2. Logical Operators
    • and: Logical AND operator (e.g., if x > 0 and y > 0:)

    • or: Logical OR operator (e.g., if x > 0 or y > 0:)

    • not: Logical NOT operator (e.g., if not x):

    • in: Membership test operator (e.g., if x in list:)

    • is: Identity comparison (e.g., if x is None:)
  3. Control Flow Keywords
    • if: Conditional statement (e.g., if x > 0:)

    • elif: Else if condition (e.g., elif x < 0:)

    • else: Default condition (e.g., else:)

    • for: For loop (e.g., for x in list:)

    • while: While loop (e.g., while condition:)

    • break: Exit loop (e.g., break)

    • continue: Skip to next iteration (e.g., continue)

    • pass: Null operation placeholder (e.g., pass)
  4. Exception Handling
    • try: Begin exception handling block (e.g., try:)

    • except: Handle specific exceptions (e.g., except ValueError:)

    • finally: Always executed block (e.g., finally:)

    • raise: Raise an exception (e.g., raise ValueError("Invalid input"))

    • assert: Debug assertion (e.g., assert x > 0, "x must be positive")
  5. Function and Class Definition
    • def: Define function (e.g., def my_function():)

    • class: Define class (e.g., class MyClass:)

    • return: Return value from function (e.g., return result)

    • yield: Generator yield (e.g., yield value)

    • lambda: Anonymous function (e.g., lambda x: x * 2)
  6. Scope and Import Keywords
    • global: Declare global variable (e.g., global counter)

    • nonlocal: Declare nonlocal variable (e.g., nonlocal outer_var)

    • import: Import module (e.g., import math)

    • from: Import specific items (e.g., from math import sqrt)

    • as: Alias for imports (e.g., import numpy as np)
  7. Async/Await Keywords
    • async: Define async function (e.g., async def fetch_data():)

    • await: Wait for async operation (e.g., result = await fetch_data())
  8. Deletion
    • del: Delete object reference (e.g., del my_variable)
  9. Context Management
    • with: Context manager (e.g., with open('file.txt') as f:)
© 2025  mtitek