Python Split String by Character: Into Characters or on a Delimiter

Python split string by character: the Command Prompt comparing list() with split() on the same string

“Python split string by character” is asked about two completely different jobs, which is why the answers people find so often miss. One means breaking a string into its individual characters. The other means cutting it at a particular character: list() handles the first. str.split() handles the second. Both are covered below, along with the … Read more >>

NumPy Shape in Python: shape[0], shape[1] and the Tuple

NumPy shape: the Command Prompt showing shape[0] as the row count and shape[1] as the column count

NumPy shape is an attribute, not a function. It hands you a tuple with one entry per dimension, and you read it with no parentheses at all: That single detail explains most of the errors people hit with it. The rest of this guide covers what the tuple means, what shape[0] and shape[1] actually count, … Read more >>

Convert a String to datetime in Python (strptime and ISO)

Python convert a string to datetime: the Command Prompt showing the time data does not match format error

datetime.strptime converts a string to a datetime in Python, using a format that describes your text exactly: Reach for fromisoformat whenever the text is already ISO 8601. It needs no format string and it is dramatically faster. All output below is from real runs on Python 3.12.5 on Windows. Converting a string to datetime with … Read more >>

PyTorch Softmax: dim, log_softmax and CrossEntropyLoss

PyTorch softmax: the Command Prompt showing the wrong loss produced by applying softmax before CrossEntropyLoss

torch.softmax turns raw model scores into probabilities that add up to 1: Two things go wrong with it constantly. Picking the wrong dim normalises across the batch instead of across the classes, and applying softmax before CrossEntropyLoss applies it twice. Both are demonstrated below, on Python 3.12.5, PyTorch 2.14.0+cpu. Using softmax in PyTorch Every value … Read more >>

Matplotlib Horizontal Grid Lines (and set_axisbelow)

Matplotlib horizontal grid lines: a plot with grid on both axes beside one with horizontal lines only

To show only horizontal grid lines in Matplotlib, pass axis=”y” to grid: Add ax.set_axisbelow(True) with it. Without that, gridlines are drawn on top of your bars and markers, which isn’t what anyone wants. Every plot below is a real Matplotlib window on Python 3.12.5, Matplotlib 3.11.2. Showing horizontal grid lines only The axis argument decides … Read more >>

How to Check if a String Contains All Unique Characters in Python?

Check if a String Contains All Unique Characters in Python

In this tutorial, I will explain how to check if a string contains all unique characters in Python. As a Python developer, I came across a scenario where I needed to check if a string contains all unique characters. After researching different methods, I discovered several ways to accomplish this task efficiently. In this article, … Read more >>

Python NameError: name is not defined (Every Cause and Fix)

Python NameError: the traceback showing the Did you mean suggestion for a misspelled variable

NameError: name ‘x’ is not defined means Python reached a name it has no value for. Four causes account for almost every case: Since Python 3.10 the error often names the fix itself, which is the fastest place to start. Every traceback below is a real failing script on Python 3.12.5. The Python NameError “Did … Read more >>

How to Fix React Component Not Defined Error

Fix React Component Not Defined Error

If you’ve been working with React for a while, you’ve probably encountered the frustrating “React component not defined” error. I’ve faced this issue countless times during my experience as a React developer, and I know how confusing it can be when your app suddenly breaks because React can’t find a component you’re sure you’ve created. … Read more >>

How to Use @dataclass in Python

@dataclass in Python

I have been writing Python for over a decade, and I remember the days when creating a simple data container felt like a chore. You had to write the constructor, the string representation, and the equality logic every single time. It was tedious and prone to errors. When Python 3.7 introduced the @dataclass decorator, it … Read more >>

How to Create a Python Tuple with One Element?

python tuple single element

I’ve spent years building applications in Python, and if there is one thing I’ve learned, it’s that the simplest syntax can sometimes be the most deceptive. The other day, I was mentoring a junior developer who was trying to pass a single US State code into a database function as a tuple. He wrote state … Read more >>