Keywords are reserved words with special meanings. Cannot use as identifiers (variable names, function names, etc.). Case-sensitive (e.g., `for` is valid, but `For` is an identifier).
The set of keywords changes as Python evolves. You can get the most up-to-date listing with current implementation using this function import keyword; print(keyword.kwlist)
which shows usage.
True
False
None
__peg_parser__
(Added in Python 3.9)Used internally. No purpose assigned yet/intended directly for Python interpreter rather than direct function in user programs per se which also is reason for double underscores notation.
They generally define various operator usages where otherwise might create ambiguities if using similarly denoted other program components if valid where this way special meaning becomes evident for keyword rather than generic use case which sometimes makes sense such as how you implement `is not` checks via methods or custom operators, but for these inbuilt keyword examples, avoid overrides where not apparent otherwise they function for this use rather than custom
and
or
not
is
in
not
is not
Not "in" rather a single phrase `is not` defining similar negation functionality which in many scenarios, also applied similarly such as in exception handling methods by explicitly catching instances if a custom exception type used
Similar in function or usage, if any comparisons relevant then usually via some equality test before conditional logic determined based upon expression evaluated prior in terms of how keywords given `if`/`else`/`elif` would respond if values different such as numeric ordering rather than match based or where otherwise not handled such as with bitwise operator examples before unless checking using set rather than indices alone after doing typecasts within other steps leading there where in lists we might count given expected int outputs to determine behavior if these cases arose so there exists implicit ordering since 0 means distinct `is not` behavior via explicit value equality with int and/or explicit negation even if such implicit handling or checks were not apparent immediately due to non-localized assignments or scoping behaviour at times
if
elif
else
for
while
break
continue
pass
try
(Exception Handling)First step handling potential exception cases explicitly rather than trying several Boolean expressions each checking if cases that might give exceptions arise would be encountered like during mathematical ops when zero division produces exception whereas checking before helps confine scope even for valid exception handler blocks to prevent inadvertent non-local handling if not handled
except
(Exception Handling) Second control structure block component within code like finally
that helps explicitly narrow when special exceptional cases like IO errors when file path string or associated system access restricted for reasons outside user control would occur after successful Boolean conditions during normal or unexceptional situations at run time where each step also verifiable unlike if code only prints once or requires debugger tools rather than tests during design which becomes harder given how complex such interactions get once such logic bundled as components themselves. Checks types, unlike `try` block where condition tested without such typecasting. Use also implies possible catch exceptions or that these exception conditions within `except` context become valid or assumed within exception handler implementation after tests assuming normal `if`/`else`/etc blocks passed all test without producing exceptions so such behaviour limited rather than during most code logic where typically only Boolean values returned like integer calculations for determining when such logic executes as opposed to special checks required for exception handler code flow
finally
(Exception Handling) Similar construct although meant primarily to create explicit finally scope since finally
operations performed after the `try`/`except` where possible issues in between do not become more complex/cause errors unrelated such as handling files if one or the other IO related cases do become true for unexpected reasons while still returning true normally like reading correct value after the try/catch. Cleanup-related to make code cleaner or remove temporary elements after try/except handled correctly if that needed, you simplify that way
with
(Context Management) Provides shorthand within control-flow block code rather than creating finally exception style blocks like after nested conditionals before other program components if used for setup purposes assuming resources available and do not fail like other exceptions being explicitly tested when try/except called: use as syntactic shorthand for allocating system or external related handles with correct try/except wrapping such operations by __enter__
at instantiation during checks while `exit` closes when execution finishes
match
, case
(Added in 3.10).Used together similar to using try`/`except`/
finally` where a default case
matches. Shorthand switch syntax available in recent Pythons, assuming types always validated as expected and that value types in case can also become comparable by that point without ambiguity in comparison tests so is
rather than by value via typecasting unless value matches type at time where similar checks exist explicitly before implicit conversion would normally occur like in operations following an invalid typecast rather than assuming correct types despite value mismatch if not clear that result before assignment, or from output since it prints rather than passing result given context
raise
(Exception Handling)Handles similar circumstance as earlier control cases like raising an Exception object or special case using code to create Exception type (explicit type conversion before call for cases using values instead), indicating that `raise` step can execute if other logic within program raises that error or other conditional `True` for this call rather than using only built-in. Shorthand approach unlike nested statements creating error type unless these done by a module. Example showing raise with no constructor and not type, showing its most basic default intended typical use-case which assumes already valid data
assert
(Debugging) Test conditions if debugging which raises AssertionError
if it's False by comparison, where any further checks handled using error handler within code using some conditional assuming such would happen rather than relying exclusively upon interpreter, if user handler defined given context by method for cases in nested class object hierarchy before program component itself given scope locally since then not clashing despite other global definition even after assignment given different scope since within class scope once that initialized, unless that class in `main` (global) namespace too where `assert` in code outside would take precedence
For similar types that generate return from a function/method with parameters using similar logic within prior nested block examples (`return`, `yield`). Check which type or types apply as needed for given situation after explicitly making casts or performing logical conditions which in practice would differ substantially during tests assuming exceptions thrown on conversion errors from implicit typecast attempts within `match` block with non-defaulting types within except
statement before the exception returned by some chained function. For generators via `yield` (rather than `return`), typecasting within for loop can demonstrate that an assignment occurs like before using iterator directly during execution/step-wise since an index isn't necessary assuming next return step using generator syntax yields another even without iteration variable, just increment counter which in `list` or ordered would specify which index processed which for generators might implicitly use queue or another to give correct behavior where generator `yield` could generate similar output stream for the caller, unless it's generating ordered which differs slightly due to how each `yield` treated then, even in multiline since single threaded per case
yield
return
Handle import of named objects rather than using path-style strings given usage of that language feature in 3 recent variants:
import
from
as
Used when defining classes, functions, or other program components. Note also intended usage implies scope-related behavior, such as `lambda` functions within some limited context for concise method call where other more standard methods don't apply if only small usage rather than large implementation or where some callback needed using limited available notation: they imply global scope by default like `class` for objects with state that remain unless using class namespaces for inner which can sometimes create confusing nested class scopes with single inheritance unlike languages where implicit multiple inheritances behavior defined rather than in `Python`. For async, assume multithreaded-like or non-sequential flow or that order matters like step-wise unlike parallel function call using multiple parameters of same type if executing sequentially which may produce results different given threads where those types may clash otherwise so typecasts matter here also or before running even simplest tests:
class
def
lambda
global
nonlocal
async
await
Special reserved word indicating/explicitly controlling the variable de-allocation behavior. When `del` done scope where deletion valid or in global main program block you can still reference those by importing from a valid Python module after explicit import some_variable
before further tests assuming usage within that given `from import` use case where variable scope relevant since could have different namespace unlike cases that are imported from that path after `import` statements using specific paths rather than relative within program where then any variables declared using reserved keywords (`del`, etc) in imported classes might create issues with current implementation like tests following some condition that relies upon default interpretation which can give non-apparent errors when types inconsistent without more comprehensive typecasting/tests
del