K-Nearest Neighbor (KNN) in Python

K nearest neighbor in Python: accuracy versus k for KNN with and without feature scaling

K nearest neighbor in Python is easiest with scikit-learn’s KNeighborsClassifier: scale the features, fit it on labeled data, and it classifies each new point by a vote among its k closest training points: I tested this with Python 3.12.5 and scikit-learn 1.9.1, using the wine dataset that comes with scikit-learn plus a tiny T-shirt size … Read more >>

Python Odd or Even: Check if a Number Is Even or Odd

Python odd or even: Command Prompt output of is_even() and is_odd() on five seat numbers

If you want to split numbers into two groups, such as seat numbers for two doors, check whether each one is odd or even in Python. The check itself is easy. The bugs show up at the edges: input() hands you a string, and a float like 7.5 is neither. Quick answer: if n % … Read more >>

How to Generate Random 4-Digit Numbers in Python

Python random 4-digit number: VS Code output of random.randint(1000, 9999) creating five pickup codes

If you need a short code, such as a pickup number, you can generate random 4-digit numbers in Python with one line: random.randint(1000, 9999). The first version I saw used int(random.random() * 10000), and about one code in ten came out short. Codes like 0427 and login PINs need other tools. Quick answer: random.randint(1000, 9999) … Read more >>

How to Check if a Python String Contains Only Numbers

Python check if string contains only numbers: Command Prompt output of isdigit() on six ZIP code strings

If you want to check if a Python string contains only numbers, such as a ZIP code or a PIN, the short answer is s.isdigit(). It mostly works. The catch is that isdigit() says True for characters most people wouldn’t call numbers, such as ² and full-width digits pasted from a phone keyboard. An empty … Read more >>

How to Find Factors of a Number in Python (4 Ways)

Factors of a number in Python: VS Code output listing the factors of 120, 97, 36 and 1

If you want every number that divides another one with nothing left over, you’re looking for the factors of a number in Python terms. One check finds them: n % i == 0. Keep i when the remainder is zero, and you’ve got your list. In this tutorial, we’ll walk through four ways to get … Read more >>

What Does assert Do in Python? (With Examples)

What does assert do in Python: Command Prompt showing an AssertionError with a custom message

What does assert do in Python? It checks a condition and raises AssertionError if the condition is false; if it’s true, nothing happens. You can add a message after a comma: I ran these checks on Python 3.12.5, including the same script with assertions switched off. We’ll trigger an AssertionError, see why python -O skips … Read more >>

Python list() Function: Syntax and 6 Examples

Python list() function turning dictionary keys, values and items into lists

If you want a list from any iterable in Python, the Python list function is what you need. It takes a string, tuple, set, dict or generator and gives you back a list. In this tutorial, I’ll cover list() with six short examples you’ll actually use. Every output comes from Python 3.12.5 in Command Prompt. … Read more >>

Exploratory Data Analysis in Python (pandas Steps)

Exploratory data analysis in Python: four matplotlib charts of store sales made with pandas

Exploratory data analysis in Python means loading your data with pandas and asking it basic questions before any modeling: how big is it, what’s missing, what do the numbers look like, and which groups stand out. A few calls cover most of it: The dataset is 600 made-up coffee shop orders from 2026, with a … Read more >>

How to Count the Number of Digits in a Number in Python

Number of digits in a number in Python: VS Code output of len(str(abs(n))) on five order numbers

If you need the number of digits in a number in Python, turn it into text and measure it: len(str(abs(n))). The abs() matters, because a minus sign counts as a character. The edge cases trip people up: zero, negatives, floats and huge integers. In this tutorial, I’ll show you how to count the number of … Read more >>