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.
Context | Keyword | Description |
---|---|---|
Datatype | False | Boolean value representing False. |
Datatype | True | Boolean value representing True. |
Datatype | None | Represents the absence of a value or a null value. |
Operator | and | Logical AND operator. True if all operands are True. |
Operator | or | Logical OR operator. True if at least one operand is True. |
Operator | not | Logical NOT operator. Negates a boolean value. |
Operator | in | Checks if an element exists in a sequence or collection. Also used to iterate a variable in a for loop. |
Variable | is | Check if two objects have the same identity (=location in memory). |
Variable | del | Delete an object, variable, or items from a collection. |
Variable | global | Declares a variable as global, accessible outside the current scope. |
Flow Control | if | Defines a conditional statement. |
Flow Control | elif | Conditional branching; short for “else if”. |
Flow Control | else | Defines 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 Control | while | Defines a loop that continues as long as the condition is True. |
Flow Control | for | Defines a loop that iterates over a sequence. |
Flow Control | break | Terminates/jumps after the nearest enclosing loop. |
Flow Control | continue | Skips the remaining code in a loop iteration and moves to the next iteration. |
Function | def | Defines a new function or method. |
Function | return | Exits a function and optionally passes back a value. |
OOP | class | Defines a new function or class. |
Questions
- What is a keyword? Explain in your own words.