In Python, keywords are a set of special reserved words used to define the structure and rules of the language. They have specific purposes and should not be used as variable names, function names, or identifiers, as doing so will cause errors in your code.
Python Common Keywords
| Keyword | Description |
|---|---|
False | Boolean value for “not true” |
None | Represents a null or no value |
True | Boolean value for “yes” or true |
and | Combines two conditions, both must be true |
as | Used when giving a module or object an alias |
assert | Used for debugging; checks if a condition is true |
break | Exits a loop early |
class | Used to define a class (for object-oriented programming) |
def | Defines a function |
del | Deletes a variable, list item, or object |
elif | Else if – adds another condition after if statements |
else | What to do if all if or elif conditions are false |
except | Defines what to do when a specific error occurs |
finally | A block of code that will always be executed, whether there is an exception or not |
for | Starts a loop that runs over items |
from | Used when importing specific arts from a module |
if | Runs code only if a condition is true |
import | Loads a module into your code |
in | Checks if a value exists in a sequence (e.g. list, range, etc.) |
is | Checks if two values refer to the same object |
lambda | Creates a small, anonymous function |
nonlocal | Refers to a variable in the nearest outer scope |
not | Reverses a condition (True becomes False, and vice versa) |
or | Combines conditions; true if at least one condition is true |
raise | Forces an error to happen |
return | Sends a value back from a function |
try | Runs code and watches for errors |
while | Starts a loop that runs as long as a condition is true |
with | Used to manage resources (e.g. files) safely |
yield | Returns a value from a generator function |