Do-While Loop in Python (With Examples)

Do-while loop in Python: Command Prompt output showing a while loop that never runs and a while True loop that runs once

Do-while loops don’t exist in Python, but a while True loop that checks the condition at the bottom and exits with break does the same job. The body always runs at least once, just like a do-while in C, Java or JavaScript. The screenshots come from Python 3.12.5 in a real Command Prompt, typed answers … Read more >>

Python while Loop (With Examples)

Python while loop: Command Prompt output of a savings loop that runs until the balance reaches 1000 dollars

While loops in Python repeat a block of code as long as a condition stays true. Python checks the condition before every pass, so a Python while loop is the right choice when you don’t know in advance how many times it should run. The examples use US dollars and ZIP codes, and each one … Read more >>

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 >>