| False |
Boolean value indicating false. |
is_student = False |
Use in conditional statements to represent a false state. |
| None |
Represents the absence of a value or a null value. |
result = None |
Commonly used to initialize variables or indicate no result. |
| True |
Boolean value indicating true. |
is_student = True |
Use in conditional statements to represent a true state. |
| and |
Logical AND operator. |
if is_student and is_enrolled: |
Used to combine multiple conditions in a single if statement. |
| as |
Used to create an alias. |
import numpy as np |
Commonly used with imports to give a module a different name. |
| assert |
Used for debugging purposes, to test if a condition in your code returns True, if not, the program will raise an AssertionError. |
assert x > 0, "x should be greater than 0" |
Useful for checking conditions and throwing errors when conditions are not met. |
| async |
Declares an asynchronous function or coroutine. |
async def fetch_data(): |
Use with await to handle asynchronous operations. |
| await |
Pauses the execution of async function until the awaited coroutine completes. |
await fetch_data() |
Only used within async functions. |
| break |
Terminates the nearest enclosing loop. |
for i in range(10): if i == 5: break |
Use to exit loops early based on a condition. |
| class |
Used to define a new user-defined class. |
class MyClass: |
Classes are blueprints for creating objects. |
| continue |
Skips the rest of the code inside the current loop iteration and goes to the next iteration. |
for i in range(10): if i % 2 == 0: continue |
Useful for skipping certain iterations in a loop. |
| def |
Used to define a new function. |
def my_function(): |
Functions encapsulate reusable code. |
| del |
Used to delete objects. |
del my_list[0] |
Can be used to delete variables, list items, or object attributes. |
| elif |
Used in conditional statements, same as else if. |
if x > 0: pass elif x < 0: pass |
Use to add multiple conditions in an if statement. |
| else |
Used in conditional statements. |
if x > 0: pass else: pass |
Executes a block of code if all preceding conditions are false. |
| except |
Used with exceptions, what to do when an exception occurs. |
try: pass except ValueError: pass |
Catches and handles exceptions raised in the try block. |
| finally |
Used with exceptions, a block of code that will be executed no matter if there is an exception or not. |
try: pass except: pass finally: pass |
Ensures execution of cleanup code regardless of exceptions. |
| for |
Used to create a for loop. |
for i in range(10): pass |
Iterates over sequences like lists, tuples, or strings. |
| from |
Used to import specific parts of a module. |
from math import sqrt |
Use to import only the required functions or classes. |
| global |
Used to declare a global variable inside a function. |
global x |
Allows modification of a global variable inside a function. |
| if |
Used to make a conditional statement. |
if x > 0: pass |
Executes a block of code if the condition is true. |
| import |
Used to import a module. |
import math |
Allows access to functions, classes, and variables defined in a module. |
| in |
Used to check if a value is present in a sequence (lists, tuples, strings, etc.). |
if x in my_list: |
Checks for membership in sequences. |
| is |
Tests for object identity. |
if x is None: |
Use to check if two variables refer to the same object. |
| lambda |
Used to create an anonymous function. |
f = lambda x: x + 1 |
Useful for short, throwaway functions. |
| nonlocal |
Used to declare a non-local variable. |
nonlocal x |
Used in nested functions to modify a variable defined in an enclosing scope. |
| not |
Logical NOT operator. |
if not x: |
Negates a boolean value. |
| or |
Logical OR operator. |
if x or y: |
Used to combine multiple conditions in an if statement. |
| pass |
Null statement, a statement that will do nothing. |
if x > 0: pass |
Useful as a placeholder for future code. |
| raise |
Used to raise an exception. |
raise ValueError("error message") |
Allows manual raising of exceptions. |
| return |
Exits a function and returns a value. |
def func(): return x |
Ends function execution and optionally returns a value. |
| try |
Used to catch exceptions. |
try: pass except: pass |
Wraps code that may throw exceptions. |
| while |
Used to create a while loop. |
while x > 0: pass |
Repeatedly executes a block of code as long as the condition is true. |
| with |
Used to simplify exception handling. |
with open('file.txt') as f: |
Ensures proper acquisition and release of resources. |
| yield |
Ends the execution of a function, returns a generator. |
def generator(): yield x |
Used in functions to return an iterator instead of a single value. |
Leave a Reply