How to Check if a Value Is NaN in Python (isnan and isna)

Check if a value is NaN in Python: VS Code output of math.isnan()

If you want to check if a value is NaN in Python, use math.isnan(value). It returns True for NaN and False for any other number. Comparing with == doesn’t work, because NaN isn’t equal to anything, not even itself. In this tutorial, I’ll show you how to check if a value is NaN in Python … Read more >>

How to Generate Random Numbers Between 0 and 1 in Python

Generate random numbers between 0 and 1 in Python: Command Prompt output of random.random()

By default, every number from Python’s random.random() falls between 0 and 1. To generate one, import the random module and call random.random() with nothing in the parentheses. You can get 0.0, but never exactly 1.0. That matters once you round the result or need 1 as a possible value. In this tutorial, I’ll show you … Read more >>

How to Generate Credit Card Numbers in Python for Testing

Generate credit card numbers in Python for testing: VS Code output of Faker test numbers

As a Python developer working on a checkout form, you’ll get a requirement to test it without a real card. To generate credit card numbers in Python for testing, use Faker’s credit_card_number(), or your payment provider’s own test numbers for sandbox payments. These numbers only look right. They aren’t real accounts, they can’t buy anything, … Read more >>

How to Build Your First AI Agent in Python

Build an AI agent in Python: VS Code run of an agent answering an order and sales tax question

If you want an AI model to look things up instead of guessing, give it tools. When you build an AI agent in Python, the model calls your own functions for facts such as an order’s status or a sales tax amount. The whole trick is a short loop. You’ll save the full version as … Read more >>

FastAPI Project Structure for Beginners

FastAPI project structure for beginners: Swagger UI showing the books and authors routers of a multi-file app

Every FastAPI app starts as one tidy file, then a catalog, some settings and a few tests land in it, and scrolling becomes the main job. Quick answer: a good FastAPI project structure for a small app is one app package with main.py plus routers, schemas, models and core folders, each holding an __init__.py, with … Read more >>

How to Convert Letters to Numbers in Python

Convert letters to numbers in Python: Command Prompt output with words turned into lists of numbers

I always use ord() when I convert letters to numbers in Python, because it needs no list or dictionary. ord(letter.upper()) – 64 gives A=1, B=2, and so on up to Z=26. To try it, save this as one_letter.py and run it: Output: In this tutorial, I’ll show you how to convert letters to numbers in … Read more >>

How to Pad Numbers with Leading Zeros in Python

Pad numbers with leading zeros in Python: VS Code output with five-digit invoice numbers

By default, Python prints the number 345 as 345, with no zeros in front. To pad it with leading zeros, use the f-string f”{345:05d}”, which gives 00345. What trips people up is that the result is text. An integer can’t store leading zeros, so you add them only when you print, save or export the … Read more >>

How to Split a Number into Digits in Python

Split a number into digits in Python: VS Code output with the list of digits of 40827

If you need to split a number into digits in Python, use [int(d) for d in str(number)]. It turns 40827 into the list [4, 0, 8, 2, 7]. You can’t loop over an integer, so make it text first. In this tutorial, I’ll show you how to split a number into digits in Python three … Read more >>

Check if a String Is a Number with a Decimal in Python

Check if a string is a number with a decimal in Python: Command Prompt output for ten price strings

Python developers who read forms or CSV files often have to check if a string represents a number with a decimal, like “19.99”. Convert it with float() inside try/except, then look for a dot in the text. isdecimal() sounds right, but returns False here. In this tutorial, I’ll show you how to check if a … Read more >>

How to Round Numbers in Python (Up, Down, Nearest)

Round numbers in Python: VS Code output of math.ceil() rounding road-trip numbers up

By default, Python’s round() goes to the nearest whole number and sends ties to the even one, so round(2.5) is 2. To round numbers up, use math.ceil(); to round down, math.floor(). If you still round with the old int(n + 0.5) trick, stop: it gives the wrong answer for -12.7. Python’s decimal module does half … Read more >>