TOOGLE
python's 34 Reserved words
-
Back-End
and: Logical AND operator. Returns True if both operands are true.
Example: if x > 5 and y < 10:
as: Used in the context of aliasing imports or in a context manager.
Example: import math as m
assert: Used for debugging purposes to check if a condition is True. Raises an error if the condition is False.
Example: assert x > 0, "x must be greater than 0"
async: Used to define asynchronous functions that can use the await keyword.
Example: async def fetch_data():
except: Used in exception handling to define a block of code that executes when an exception occurs.
Example:
try:
...
except ValueError as ve:
...
False: Boolean value representing false.
Example: is_raining = False
not: Logical NOT operator. Returns True if the operand is false, and vice versa.
Example: if not is_raining:
or: Logical OR operator. Returns True if either of the operands is true.
Example: if x < 0 or y < 0:
finally: Used in exception handling to define a block of code that always executes, regardless of whether an exception occurred or not.
Example:
try:
...
except:
...
finally:
...
for: Used to iterate over a sequence (like a list, tuple, or string) or any iterable object.
Example: for num in range(1, 5):
pass: Placeholder statement that does nothing. Used when a statement is syntactically required, but no action is needed.
Example:
def some_function():
pass
raise: Used to raise an exception explicitly.
Example: raise ValueError("Invalid input")
await: Used inside async functions to await the result of another async function.
Example: result = await fetch_data()
break: Used to exit from a loop prematurely.
Example:
for num in range(10):
if num == 5:
break
class: Used to define a new class.
Example: class Dog:
from: Used to import specific attributes or functions from a module.
Example: from math import sqrt
global: Used to declare a variable inside a function as global, so it can be accessed and modified globally.
Example:
def some_function():
global x
if: Used to define conditional statements.
Example: if x > 0:
return: Used to return a value from a function.
Example: def add(x, y): return x + y
True: Boolean value representing true.
Example: is_sunny = True
try: Used in exception handling to define a block of code to be tested for errors.
Example:
try:
...
except ValueError:
...
continue: Used to skip the rest of the loop and start the next iteration.
Example:
for num in range(10):
if num % 2 == 0:
continue
def: Used to define a new function.
Example: def greet(name): ...
del: Used to delete references to objects.
Example: del my_list[0]
elif: Used in conjunction with if, for providing additional conditions to check.
Example: if x > 0: ... elif x == 0: ...
else: Used in conjunction with if, for providing a block of code to execute when the condition is False.
Example: if x > 0: ... else: ...
import: Used to import modules or packages.
Example: import random
is: Used for identity comparison, checking if two variables refer to the same object.
Example: if x is None:
lambda: Used to define small anonymous functions.
Example: multiply = lambda x, y: x * y
None: Represents a null or absence of a value.
Example: result = None
nonlocal: Used to declare a variable that belongs to an enclosing (but non-global) scope, allowing modification in nested functions.
Example:
def outer_func():
nonlocal x
while: Used to define a loop that continues while a condition is True.
Example: while count < 10:
with: Used to create a context manager to handle resources (e.g., file handling) efficiently.
Example: with open("file.txt", "r") as file:
yield: Used in generator functions to produce a value and suspend the function's state, allowing resumption later.
Example: def countdown(n): yield n