Python break and continue: Exit or Skip a Loop

Python break and continue: Command Prompt output of a for loop skipping cancelled orders with continue

Python’s break and continue both change how a loop runs. break exits the loop immediately, and continue skips the rest of the current pass and moves on to the next one. Both work in for and while loops. The orders, statuses and seat numbers below are made up, but every output is real: I ran … Read more >>

How to pip install a Specific Version of a Package in Python

pip install a specific version: Command Prompt showing pip installing tabulate 0.9.0 and Python confirming the installed version

pip installs a specific version when you put == and the version number after the package name. The same command downgrades or upgrades whatever’s already installed: I tested every command below in a real Windows Command Prompt with Python 3.12.5 and pip 24.2, inside a separate environment so you can see each install happen from … Read more >>

ModuleNotFoundError After pip install in Python: How to Fix It

ModuleNotFoundError after pip install: Command Prompt showing pip reporting the package installed while another Python cannot import it

ModuleNotFoundError after pip install almost always means pip installed the package for one Python, and you ran your code with a different one. The fix is to install with the exact interpreter that runs your script: Every screen below is a real Command Prompt on Windows with Python 3.12.5, using two separate environments so you … Read more >>

Mutable vs Immutable in Python (With Examples)

Mutable vs immutable in Python: Command Prompt output comparing object ids before and after changing a list, string, int and tuple

Mutable vs immutable in Python comes down to one question: can the object change after it’s created? Lists, dictionaries and sets are mutable, so they change in place. Strings, ints, floats and tuples are immutable, so every “change” builds a new object. I ran each snippet on Python 3.12.5, and the screenshots are the real … Read more >>

Python Variable Scope: Local, Global, Nonlocal and LEGB

Python variable scope: Command Prompt output showing the LEGB rule with local, enclosing, global and built-in names

Scope decides where a Python variable can be seen: a name created inside a function is local, and a name at the top of a file is global. Python looks names up in Local, Enclosing, Global, then Built-in scope, the LEGB rule. These examples ran on Python 3.12.5, and the screenshots come straight from the … Read more >>

Recursion in Python: A Complete Guide with Examples

Recursion in Python: Command Prompt output tracing the recursive calls and returns of factorial(4) on the call stack

Recursion in Python is when a function calls itself to solve a smaller version of the same problem. Every recursive function needs a base case that stops the calls and a recursive case that moves toward it; without a base case Python stops with RecursionError after about 1,000 calls. This guide shows how Python recursion … Read more >>

Regular Expressions in Python (re Module Guide)

Regular expressions in Python: Command Prompt output of re.search, re.match, re.fullmatch, re.findall, re.finditer, re.sub and re.split

Regular expressions in Python are patterns for finding, checking, extracting and replacing text, and they are provided by the built-in re module. re.search(r”\d+”, text) finds the first number in a string, re.findall() returns every match, and re.sub() replaces them. This guide covers every re function, the pattern syntax, groups, greedy and lazy matching, flags, lookarounds, … Read more >>

Python dict() Function: A Practical Guide With Examples

Python dict() Function

When I build a small reporting script, I often need a fast way to organize related details. For example, an employee record may need a name, department, email, and monthly sales figure. Keeping those values in separate variables becomes messy quickly. That is where the Python dict() function helps. It creates a dictionary, which stores … Read more >>

Python delattr() Function: Remove Object Attributes

Python delattr() Function

When I build small automation scripts, I often create objects to hold temporary report data—such as a customer’s draft notes, an API token, or a calculated status. Once that information is no longer valid, I do not want it sitting on the object and getting reused accidentally. That is where the Python delattr() function helps. … Read more >>