How to Check if Input Is a Number in Python

Check if input is a number in Python: Command Prompt run of a loop that keeps asking until a valid quantity is typed

By default, Python’s input() gives you text, so a script that expects a quantity crashes with a ValueError when someone types twelve. To avoid that, check if input is a number before doing any math. The obvious fix, isdigit(), still lets odd text through and rejects -3 and 4.99. Python’s input() always hands you a … Read more >>

Python Currency Format: Format Numbers as Currency in Python

Python currency format: VS Code output of an f-string formatting invoice amounts as US dollars

If you’ve tried f”${x:,.2f}” on a refund in Python, you’ve probably seen $-45.10 instead of -$45.10. The spec formats only the number, so the minus lands after the $. The fix for a Python currency format is a tiny helper: put the sign first, then $, then abs(x) with :,.2f. For other countries, use Babel’s … Read more >>

Simple HTTP Server in Python (http.server Examples)

Simple HTTP server in Python: Command Prompt output of python -m http.server serving files with its log

A simple HTTP server in Python is one command: open a terminal in the folder you want to share and run python -m http.server. It serves the files at http://localhost:8000 until you press Ctrl+C: Every server here ran on my machine with Python 3.12.5, and I made real requests to it so you can see … Read more >>

Python float() Function: Syntax and 6 Examples

Python float() function converting strings with signs, spaces, exponents and underscores to floats

If you want a decimal number in Python, the Python float function does exactly that. It takes “19.99” or 7 and gives you back a float for decimal math. In this tutorial, I’ll show you float() in six real examples: strings, inf and NaN, order totals, integers, safe conversion and custom objects. I ran each … Read more >>

Support Vector Machine in Python (scikit-learn SVC)

Support vector machine in Python: RBF kernel decision boundary with margins and support vectors

To build a support vector machine in Python, use scikit-learn’s SVC class inside a pipeline with StandardScaler, then call fit() and predict(). The SVM finds the boundary with the widest margin between classes: Because the breast cancer dataset ships with scikit-learn, you can run every example offline; I used Python 3.12.5 and scikit-learn 1.9.1. We’ll … Read more >>

Python Dictionary get() With a Default Value

Python dictionary get or default: Command Prompt output of dict.get returning a default instead of KeyError

A Python dictionary get or default lookup uses dict.get(key, default): it returns the value if the key exists and your default if it doesn’t, without raising KeyError. Leave out the default and you get None: All of the examples below ran on Python 3.12.5, using a coffee shop menu priced in dollars. We’ll compare get() … Read more >>

Python str() Function: Syntax and 6 Examples

Python str() function converting integers, floats, booleans and None to strings

If you want to turn any value into text in Python, the Python str function is what you need. It takes a number, list, bytes or object and gives you back a str. In this tutorial, I’ll explain the syntax, then work through six examples: numbers, order messages, lists, bytes, custom classes and str() vs … Read more >>

K-Means Clustering in Python (scikit-learn Example)

K means clustering in Python: scatter plot of customers grouped into three clusters with centers marked

For k means clustering in Python, scale your features, then fit scikit-learn’s KMeans with the number of clusters you want. It assigns every row to the nearest cluster center: I used Python 3.12.5 and scikit-learn 1.9.1 on 300 made-up store customers, each with an annual spend in dollars and a number of visits per year. … Read more >>

Text to Speech in Python (pyttsx3 and gTTS)

Text to speech in Python: waveform of a WAV file created with pyttsx3

For text to speech in Python, install pyttsx3, which works offline with your computer’s built-in voices, then call say() and runAndWait(). For natural-sounding MP3 files, gTTS uses Google’s online service: I tested these on Windows 11 with Python 3.12.5, pyttsx3 2.99 and gTTS 2.5.4. Since you can’t hear a screenshot, I saved the speech to … Read more >>

Python int() Function: Syntax and 6 Examples

Python int() function converting strings with spaces, signs and underscores to integers

If you have a numeric string or a float in Python and you want a whole number, the Python int function is the quickest way to get it. It takes “42” or 7.9 and gives you back an int for math. In this tutorial, I’ll cover the syntax plus six examples: strings, floats, binary and … Read more >>