Python Program for Bubble Sort (With Examples)

Python program for bubble sort: Command Prompt output showing each pass sorting six order prices

If you have to write a Python program for bubble sort, say for a class or a coding interview, the idea is simple: walk through the list, swap any two neighbors that are out of order, and repeat until a pass makes no swaps. The swap is one line. It’s the loop bounds that trip … Read more >>

For Loop vs While Loop in Python

For loop vs while loop in Python: Command Prompt output showing both loops reach the same sales total

The for loop vs while loop in Python choice comes down to one thing: a for loop walks through a sequence or a fixed count, and a while loop keeps repeating until a condition turns false, however many passes that takes. My test machine had Python 3.12.5; each output on this page is pasted from … Read more >>

Python Get Current Directory: os.getcwd() and Path.cwd()

Python get current directory: Command Prompt showing the working directory differs from the script folder so open fails

Call os.getcwd() to get the current directory in Python. It returns the working directory as a string, and Path.cwd() from pathlib gives you the same folder as a Path object: One catch trips up almost everyone: that’s the folder you launched Python from, not the folder your script sits in. I’ll show both, with real … Read more >>

How to Make an Infinite Loop in Python (and Stop It)

How to make infinite loop in Python: Command Prompt running a while True shopping cart loop with break

Here’s how to make an infinite loop in Python: write while True:, put your repeating code inside, and use break when it’s time to stop. Without a break, it runs until you press Ctrl+C: I typed the answers into a real Command Prompt for the first example, and every loop here ran on Python 3.12.5. … Read more >>

Matplotlib Bold Title and Subplot Title Font Size

Matplotlib 2x2 subplots of 2026 revenue by region with bold 14-point subplot titles and a bold figure title

Say you’ve got a 2×2 chart of 2026 revenue for four US regions, and the subplot titles look small and thin. For a Matplotlib title bold and bigger, pass fontweight=”bold” and fontsize=14 to ax.set_title(). In this tutorial, I’ll bold and resize subplot titles three ways (one setting skips the figure title), then bold other text, … Read more >>

Matplotlib Horizontal Bar Chart in Python (barh Step by Step)

Matplotlib horizontal bar chart of Q1 2026 sales by rep with dollar value labels and the top rep highlighted

A sales manager’s chart of seven reps had full names colliding under vertical bars. A Matplotlib horizontal bar chart fixed it: ax.barh() puts each name on the y-axis, where it’s got room. In this tutorial, I’ll build a Matplotlib horizontal bar chart step by step on Python 3.12.5 and Matplotlib 3.11.2, then handle negative values. … Read more >>

Pandas pivot_table() Examples in Python

pandas pivot table examples: Q1 2026 revenue by region and product as a stacked bar chart

If you’ve built a pandas pivot table on sales data, you’ve probably seen $3,773.75 where you expected a regional total. That happens because pivot_table() averages by default; pass aggfunc=”sum” and you get totals. In this tutorial, I’ll walk through five pandas pivot table examples on one small sales dataset, from a one-line summary to grand … Read more >>

Tkinter pack() Geometry Manager in Python

Tkinter pack geometry manager in Python: a bike rental window with a header, sidebar, main area and button bar

A bike rental shop asked me for a booking window with a title bar, a bike list, a price area and two buttons. Tkinter pack handles all of it: this geometry manager stacks each widget against one side of its parent. Here’s what we’re building, running for real on Windows 11: In this tutorial, I’ll … Read more >>

range vs xrange in Python (What Changed in Python 3)

Range and xrange in Python: Command Prompt output comparing range and list memory in Python 3

Range and xrange in Python come from two eras: in Python 2, range() built a full list while xrange() produced numbers lazily. Python 3 dropped xrange and made range work the lazy way, so today you just use range(): Python 2 reached end of life in 2020, so I describe its behavior rather than run … Read more >>

Fast Fourier Transform (FFT) in Python with NumPy

Fast Fourier transform in Python: NumPy FFT amplitude spectrum showing peaks at 50 Hz and 120 Hz

A fast Fourier transform in Python takes one NumPy call: np.fft.rfft(signal) turns a signal into its frequency components, and np.fft.rfftfreq() tells you which frequency each value belongs to: All the numbers and the plot below come from Python 3.12.5, NumPy 2.5.3 and SciPy 1.18.1, run on a synthetic signal so you can see exactly what … Read more >>