In Python, keywords are reserved words that have a special predefined meaning and cannot be used as identifiers (e.g., variable names, function names). Keywords are the building blocks of Python syntax and structure.

Most keywords are explained on a different page, so the “Context” column will link to the details.

ContextKeywordDescription
DatatypeFalseBoolean value representing False.
DatatypeTrueBoolean value representing True.
DatatypeNoneRepresents the absence of a value or a null value.
OperatorandLogical AND operator. True if all operands are True.
OperatororLogical OR operator. True if at least one operand is True.
OperatornotLogical NOT operator. Negates a boolean value.
OperatorinChecks if an element exists in a sequence or collection. Also used to iterate a variable in a for loop.
VariableisCheck if two objects have the same identity (=location in memory).
VariabledelDelete an object, variable, or items from a collection.
VariableglobalDeclares a variable as global, accessible outside the current scope.
VariablenonlocalRefers to a variable in the nearest enclosing scope (not global).
Flow ControlifDefines a conditional statement.
Flow ControlelifConditional branching; short for “else if”.
Flow ControlelseDefines the block of code to execute if the if condition is False. Also has a special use-case in the context of while, for and except.
Flow ControlmatchUsed for pattern matching. Executes blocks of code based on the structure or value of an expression.
Flow ControlcaseDefines a block within a match statement, representing a specific case to match a value or pattern.
Flow ControldefaultUsed in a match statement to handle no case is matching.
Flow ControlwhileDefines a loop that continues as long as the condition is True.
Flow ControlforDefines a loop that iterates over a sequence.
Flow ControlbreakTerminates/jumps after the nearest enclosing loop.
Flow ControlcontinueSkips the remaining code in a loop iteration and moves to the next iteration.
Flow ControlpassPlaceholder statement that does nothing.
FunctiondefDefines a new function or method.
FunctionlambdaDefines an anonymous/inline function.
FunctionreturnExits a function and optionally passes back a value.
FunctionyieldPauses a function, returning a value, and allows it to be resumed later (used in generators).
OOPclassDefines a new function or class.
ExceptiontryDefines a block of code to test for exceptions.
ExceptionexceptDefines how to handle exceptions in a try block.
ExceptionfinallyDefines cleanup code that always runs after try or except blocks.
ExceptionraiseUsed to raise exceptions manually.
ExceptionassertDebugging tool, if the condition is False, it will throw an exception.
ExceptionwithContext Manager to properly enter and exit resources like files.
ImportimportImports a module or specific components from a module.
ImportfromSpecifies the module from which to import.
Import / ExceptionasUsed for creating aliases/alternative names.
AsynchronousasyncUsed to define asynchronous functions (coroutines).
AsynchronousawaitUsed to pause execution in asynchronous functions until a result is available.

Questions

  • What is a keyword? Explain in your own words.