Remove a Character From a String in Python (by Index or Value)

Python remove a character from a string: the Command Prompt showing that str does not support item deletion

To remove a character from a string in Python, you build a new string without it. Which method you want depends on whether you know the position or the character: There is no del and no item assignment. Both raise a TypeError, which is the first thing to understand here. Every result below is a … Read more >>

Reverse a String in Python: Slicing, Loops and Recursion

Python reverse a string: the Command Prompt comparing the speed of slicing, reversed and a manual loop

To reverse a string in Python, slice it with a step of -1: There is no reverse() method on strings, because strings cannot be changed in place. Every approach below builds a new string. All timings and output come from real runs on Python 3.12.5. Reversing a string with slicing The slice takes three numbers, … Read more >>

NumPy sum(): Axis, keepdims, dtype and Sum of Squares

Python NumPy sum: the Command Prompt comparing three ways to compute a sum of squares

np.sum adds up the values in a NumPy array, either all of them or along one axis: The axis argument is what people come for, and the rule is short: the axis you name is the one that disappears from the result. All figures below are real runs on Python 3.12.5, NumPy 2.5.3. Using np.sum … Read more >>

PyTorch nn.Linear: Shapes, Weights and the Multiply Error

PyTorch nn.Linear: the Command Prompt showing that the weight shape is out_features by in_features

nn.Linear is PyTorch’s fully connected layer. It takes a number of input features and produces a number of output features: Two details save a lot of debugging. The weight is stored as (out_features, in_features), and the layer only transforms the last dimension of your input. Shapes, weights and errors below are real output from Python … Read more >>

Matplotlib Set Y Axis to Log Scale (base, symlog and zeros)

Matplotlib set y axis log scale: a linear plot beside the same data on a logarithmic y axis

To set a Matplotlib y axis to a log scale, call set_yscale on the axes or plt.yscale on the current figure: Two things catch people out. The argument is base, not basey, and any zero or negative value is dropped without a warning. Both are demonstrated below, on Python 3.12.5, Matplotlib 3.11.2. Setting the y … Read more >>

Get the First N Characters of a String in Python

Python get the first n characters of a string: the Command Prompt showing slice syntax in use

To get the first n characters of a string in Python, slice from the start: The useful part is what it doesn’t do. Asking for more characters than the string has returns the whole string rather than raising an error. All output below comes from real runs on Python 3.12.5. Getting the first n characters … Read more >>

NumPy 3D Arrays in Python: Shape, Indexing and Axes

Python NumPy 3D array: the Command Prompt showing how summing along each axis changes the shape

A 3D array in Python is a NumPy array with three dimensions, created most easily by reshaping a flat one: Read the shape from the outside in: 2 blocks, each holding 3 rows, each holding 4 columns. That ordering is what makes indexing and axis= make sense. All output here comes from real runs on … Read more >>

How to Increment Value in a Python Dictionary

How to Increment Value in Dictionary Python using while loop

While working on a project that involved analyzing customer purchase data in the USA, I had to keep track of how many times each product was bought. I quickly realized that the best way to do this in Python was by using a dictionary. Dictionaries in Python are powerful because they allow me to map … Read more >>

Debug TensorFlow Models: Best Practices

Debug TensorFlow Models Best Practices

TensorFlow provides immense flexibility for developers, but with that flexibility comes complexity. Those who have worked on training deep learning models know that things rarely work perfectly on the first attempt. The training loss might refuse to decrease, tensors might not match expected shapes, or a model that performs well during training might fail in … Read more >>

Python Increment and Decrement: There Is No ++ Operator

Python increment operators: the Command Prompt showing that plus plus x compiles to two no-op unary plus instructions

Python has no increment or decrement operators. There is no ++ and no –. You use augmented assignment instead: The dangerous part is that ++count does not raise an error. It’s valid Python that does absolutely nothing, so the bug is silent. Every result on this page comes from running the code on Python 3.12.5. … Read more >>