How to Check if a String Is Alphabet in Python

Check if a string is alphabet in Python: VS Code output of isalpha on city, state and name values

To check if a string is alphabet in Python, I reach for isalpha() first. A client’s CRM export of US customers showed me its limits: “New York” and “O’Neil” failed, while “José” passed a system that only takes A to Z. Quick answer: value.isalpha() returns True when the string is not empty and every character … Read more >>

How to Use Abstract Base Classes (ABC) in Python

Abstract base classes in Python: VS Code output of a PaymentMethod ABC raising TypeError on instantiation

Abstract base classes in Python come from the abc module: subclass abc.ABC and put @abstractmethod on every method a child class must supply. Python then refuses to create objects from any class that still has an abstract method left: PaymentMethod() now raises TypeError, while a subclass that writes charge() works like any normal class. In … Read more >>

Python classmethod() Function: How to Use It With Examples

Python classmethod function: VS Code output of from_csv_row and from_dict building three invoices

A billing script I worked on had to build invoices from three places: a CSV export, a JSON API and a web form. Cramming all three into one __init__ got ugly fast, and that’s exactly where Python’s classmethod() function earns its keep. Your first instinct might be a plain helper like make_invoice(row). It works until … Read more >>

Fix: TypeError ‘list’ object is not callable in Python

Python list object is not callable: Command Prompt traceback after shadowing the built-in list

TypeError: ‘list’ object is not callable in Python means you put parentheses after something that’s a list, so Python tried to call it like a function. Usually you named a variable list, or used () where indexing needs []: Every traceback below is real output from Python 3.12.5, with the fix right after it. We’ll … Read more >>

Principal Component Analysis (PCA) in Python with scikit-learn

Principal component analysis in Python: scikit-learn PCA reducing the wine dataset to two components

Principal component analysis in Python takes three lines with scikit-learn: scale your features, create PCA(n_components=2), and call fit_transform(). You get a smaller set of new columns that keep most of the information: I tested this on Python 3.12.5 with scikit-learn 1.9.1, using the wine and iris datasets that ship with scikit-learn, so you won’t need … Read more >>

Python ceil() Function: How to Round Numbers Up

The ceil function in Python: Command Prompt output of math.ceil rounding 2.1, 2.9 and -2.8 and returning an int

The Python ceil() function rounds a number up to the nearest whole number. It lives in the math module, so you call it as math.ceil(x) after import math: math.ceil(2.1) gives 3, and math.ceil(-2.8) gives -2, because up means towards positive infinity. It always returns an int, never a float. This guide covers the ceiling function … Read more >>

How to Check if a String Contains Only Alphanumeric Characters and Underscores in Python?

String Contains Only Alphanumeric Characters and Underscores in Python

In this tutorial, I will explain how to verify that a string only contains letters, numbers, and underscores in Python. As a Python developer working on a project for a US-based company, I recently faced an issue where I needed to ensure that user-provided strings only contained alphanumeric characters and underscores. After researching and testing … Read more >>

How to Return Multiple Values from a Function in Python

Python return multiple values from a function: Command Prompt output of an order summary unpacked into three variables

To return multiple values from a function in Python, list them after return, separated by commas. Python packs them into one tuple, and the caller unpacks it into separate variables: I tested every example here on Python 3.12.5 in the Command Prompt, so the outputs you see are real. Below, I’ll cover unpacking tricks, dictionaries, … Read more >>

What is Regression in Machine Learning?

What is Regression in Machine Learning

Regression is a key technique in machine learning used to predict numerical values. It helps find relationships between variables and estimate outcomes based on input data. Machine learning models use regression to learn patterns from existing information and make predictions for new situations. Regression in machine learning aims to create a mathematical model that can … Read more >>

Python isdigit(): What It Accepts and What It Rejects

Python isdigit: the Command Prompt showing that negative numbers and decimals both return False

isdigit() is a Python string method that returns True when every character in the string is a digit, and False otherwise. Those last two are why you’re probably here. A minus sign isn’t a digit and neither is a full stop, so isdigit rejects most real-world numbers. Everything on this page ran on Python 3.12.5, … Read more >>