Posts

GeneratorExit in Python: Causes and Fixes with Simple Examples

  What is GeneratorExit in Python? A GeneratorExit in Python occurs when a generator is closed or terminated , usually when the program stops iteration or calls the close() method on a generator. In simple words, Python is saying: “This generator has been closed and should stop execution.” Unlike normal errors, GeneratorExit is not a bug — it is a signal to stop a generator gracefully . What is a Generator in Python? A generator is a special function that returns values one at a time using the yield keyword instead of returning all values at once. Example: def numbers(): yield 1 yield 2 yield 3 Generators are memory-efficient and commonly used for loops, large data processing, and streams . When Does GeneratorExit Occur? GeneratorExit occurs when: A generator is closed manually A generator finishes execution A program stops generator iteration early Python garbage collects a generator generator.close() is called Common GeneratorEx...

SystemExit in Python: Causes and Fixes with Simple Examples

  What is SystemExit in Python? A SystemExit in Python occurs when a program intentionally stops execution using the sys.exit() function. In simple words, Python is saying: “The program has been asked to exit.” Unlike most errors, SystemExit is not always a bug  - it is often used to stop a program cleanly . When Does SystemExit Occur? SystemExit usually happens when: sys.exit() is called A program is designed to stop under certain conditions A script exits after completing a task The user stops execution in controlled code Common SystemExit Error Message SystemExit Or with an exit code: SystemExit: 0 Example 1: SystemExit Using sys.exit() ❌ Code import sys print("Program started") sys.exit() print("This will not run") ❌ Output Program started SystemExit ✔ Why this happens The program stops immediately when sys.exit() is called. Example 2: Exiting with a Custom Message ✔ Code import sys sys.exit("Program stopped ...

FloatingPointError in Python: Causes and Fixes with Simple Examples

  What is FloatingPointError in Python? A FloatingPointError in Python occurs when a floating-point calculation (decimal number operation) fails or produces an invalid result . In simple words, Python is saying: “There is a problem with a decimal or floating-point calculation.” This error is rare in normal Python use, but it can appear in scientific computing, numerical operations, and advanced math calculations . What Are Floating-Point Numbers? Floating-point numbers are decimal numbers , such as: 3.14 0.001 99.99 Python uses floating-point numbers for precise calculations , but sometimes math operations can produce errors like overflow , underflow , or invalid results. Common FloatingPointError Message FloatingPointError: invalid value encountered in double_scalars or simply: FloatingPointError Why FloatingPointError Usually Doesn’t Appear? By default, Python does not raise FloatingPointError automatically — it silently returns values like inf ...

ReferenceError in Python: Causes and Fixes with Simple Examples

  What is ReferenceError in Python? A ReferenceError in Python occurs when you try to access an object that no longer exists in memory . In simple words, Python is saying: “This object reference is no longer valid.” This error is rare in everyday Python coding but can happen when using weak references or objects that have already been deleted. Understanding Weak References in Python Python allows creating weak references using the weakref module. A weak reference: Does not prevent an object from being deleted Becomes invalid when the object is garbage collected If you try to access it after deletion, Python raises a ReferenceError . Common ReferenceError Message ReferenceError: weakly-referenced object no longer exists Example 1: ReferenceError with weakref ❌ Incorrect Code import weakref class Test: pass obj = Test() weak_obj = weakref.ref(obj) del obj print(weak_obj()) ❌ Error ReferenceError: weakly-referenced object no longer exists ✔...

StopIteration in Python: Causes and Fixes with Simple Examples

  What is StopIteration in Python? A StopIteration error in Python occurs when an iterator has no more values to return . It signals that a loop or iteration has reached the end of a sequence. In simple words, Python is saying: “There are no more items left to iterate.” This is not always a bug - it is part of normal iterator behavior — but it becomes an issue when raised unexpectedly. Understanding Iterators in Python An iterator is an object that allows looping through values, such as: Lists Tuples Files Custom iterator objects Python automatically raises StopIteration when iteration finishes. Common StopIteration Error Message StopIteration Example 1: StopIteration Using next() ❌ Incorrect Code numbers = [1, 2, 3] iterator = iter(numbers) print(next(iterator)) print(next(iterator)) print(next(iterator)) print(next(iterator)) # Extra call ❌ Error StopIteration ✔ Why this happens There are only 3 items — calling next() again causes the e...

UnboundLocalError in Python: Causes and Fixes with Simple Examples

  What is UnboundLocalError in Python? An UnboundLocalError in Python occurs when you try to use a local variable before assigning a value to it inside a function. In simple words, Python is saying: “You are using a variable before giving it a value.” This error usually happens due to variable scope confusion , especially when working inside functions. Understanding Variable Scope in Python Python has two main variable scopes: Local scope — variables defined inside a function Global scope — variables defined outside a function If Python detects a variable is being assigned inside a function, it treats it as local , even if a global variable with the same name exists. Common UnboundLocalError Message UnboundLocalError: local variable 'x' referenced before assignment Example 1: Using a Variable Before Assignment ❌ Incorrect Code def show_number(): print(number) number = 10 show_number() ❌ Error UnboundLocalError: local variable 'numb...

KeyboardInterrupt in Python: Causes and Fixes with Simple Examples

  What is KeyboardInterrupt in Python? A KeyboardInterrupt in Python occurs when the user manually stops a running program , usually by pressing Ctrl + C on the keyboard. In simple words, Python is saying: “The program was stopped by the user.” This error is not caused by wrong code - it happens when a program runs too long or enters an infinite loop, and the user interrupts it. When Does KeyboardInterrupt Occur? KeyboardInterrupt commonly happens when: A program runs too long A program enters an infinite loop A user presses Ctrl + C to stop execution A script waits for input but gets interrupted Common KeyboardInterrupt Error Message KeyboardInterrupt Example 1: Infinite Loop Causing KeyboardInterrupt ❌ Incorrect Code while True: print("Running...") 🔴 The program runs forever until you press Ctrl + C ❌ Error KeyboardInterrupt ✔ Why this happens The loop never ends, so the user stops it manually. Example 2: Long-Running Program ...