datacraftworks.com

Python Keywords

Keyword Definition Syntax Example Explanation
False Represents the Boolean false value. flag = False Used in logical expressions.
None Represents the absence of a value or a null value. result = None Often used for default function return values or placeholders.
True Represents the Boolean true value. flag = True Used in logical expressions.
and Logical AND operator. if x > 0 and y > 0: Returns True if both operands are true.
as Used to create an alias. import pandas as pd Commonly used in import statements.
assert Used for debugging by testing a condition. assert x > 0, "x must be positive" Raises an AssertionError if the condition is false.
async Declares an asynchronous function. async def my_coroutine(): Used with asynchronous programming.
await Used to wait for the result of an async function call. await my_coroutine() Only valid inside async functions.
break Terminates a loop prematurely. for i in range(10): break Immediately exits the nearest enclosing loop.
class Declares a class. class MyClass: Used for creating user-defined objects.
continue Skips the rest of the loop iteration. for i in range(10): continue Proceeds to the next iteration of the loop.
def Defines a function. def my_function(): Used for defining functions.
del Deletes a reference to an object. del my_list[0] Removes an item or deletes a variable.
elif Conditional statement. if x > 0: elif x < 0: Used after an if statement, short for “else if”.
else Specifies a block of code to execute if the if condition is false. if x > 0: else: Part of an if-else or try-except block.
except Catches exceptions in a try block. try: except ValueError: Handles specific exceptions.
finally Specifies a block of code to execute no matter what. try: finally: Always runs after try, whether an exception occurred or not.
for Declares a for loop. for i in range(10): Iterates over a sequence.
from Imports specific parts of a module. from math import pi Used with import.
global Declares a global variable. global x Used to modify variables outside the current scope.
if Declares a conditional statement. if x > 0: Executes a block of code based on a condition.
import Imports a module. import os Allows you to use external modules and libraries.
in Tests membership in a sequence. if x in my_list: Also used in for loops for iteration.
is Tests identity (whether two objects are the same). if x is y: Compares object identities, not values.
lambda Creates an anonymous function. lambda x: x * 2 Returns a function object without a name.
nonlocal Declares a non-local variable. nonlocal x Used in nested functions to refer to variables in the enclosing (but non-global) scope.
not Logical NOT operator. if not x: Returns the inverse of the truth value.
or Logical OR operator. if x > 0 or y > 0: Returns True if at least one operand is true.
pass A null statement that does nothing. if x > 0: pass Often used as a placeholder for future code.
raise Raises an exception. raise ValueError("Invalid input") Used for error handling.
return Exits a function and returns a value. return x * 2 Used in function definitions.
try Specifies a block of code to test for exceptions. try: except: Used for error handling.
while Declares a while loop. while x > 0: Repeats a block of code while a condition is true.
with Simplifies exception handling by automatically managing resources. with open('file.txt') as f: Often used for file operations.
yield Pauses a generator and returns a value. yield x Used in generator functions to produce a sequence of values lazily.