How to Convert Month Name to Number in Python

Convert month name to number in Python: Command Prompt output of strptime turning month names into 1 to 12

To convert month name to number in Python, pass the name to datetime.strptime(name, “%B”).month, which turns “March” into 3. Typed months are rarely that tidy, though. A stray space or “Sept” makes the simple version raise ValueError. In this tutorial, I’ll show you how to convert month name to number in Python four ways, then … Read more >>

How to Check if a String is a Valid Date in Python

Python check if string is date: VS Code output of strptime marking lease dates True or False

If people type dates by hand, you’ll get values like 02/30/2026, 13/01/2026 and N/A. So check if a string is a valid date in Python before you use it. A simple regex, \d{2}/\d{2}/\d{4}, only checked the shape, so February 30 slipped through. Quick answer: pass the string to datetime.strptime(text, “%m/%d/%Y”) inside try. A real date … Read more >>

How to Get the Decimal Part of a Number in Python

Python get decimal part: VS Code output of math.modf splitting prices into integer and decimal parts

To get the decimal part of a float in Python, call math.modf(x). It returns the fractional and integer parts together, both signed, so a -3.75 refund gives -0.75. Below are four tested ways to get the decimal part of a number in Python on Python 3.12.5, plus where x % 1 gets negatives wrong. The … Read more >>

Linear Search in Python (With Examples)

Python linear search: Command Prompt output of a search function counting comparisons

A Python linear search checks each item in a list, one after another, until it finds the target, then returns its index, or -1 if it isn’t there. It works on any list, sorted or not: I measured everything here, including the timings, on Python 3.12.5. We’ll write the function with a comparison counter, look … Read more >>

Python sorted() Function: Syntax and 5 Examples

Python sorted() function returning a new sorted list while the original list stays unchanged

If you have a list in Python and you want it in order, the Python sorted function is the quickest way to get it. It takes any iterable and gives you back a new sorted list, leaving the original untouched. In this tutorial, I’ll show you its syntax and five examples: numbers and strings, descending … Read more >>

How to Make a Python Script Executable

How to make a Python script executable: Command Prompt building and running invoice.exe with PyInstaller

Here’s how to make a Python script executable: on Linux or macOS, add a shebang line and run chmod +x. On Windows, use the py launcher or a .bat wrapper, or build an .exe with PyInstaller: The Windows screenshots come from my Windows 11 machine with Python 3.12.5 and PyInstaller 6.22.3. The Linux and macOS … Read more >>

Python Exponential Function: math.exp() Syntax and 5 Examples

Python exponential function math.exp returning e raised to the power 1, 0, 2, -1 and 0.5

If you want to raise e to a power in Python, the Python exponential function math.exp(x) is what you’re looking for. It takes a number x and gives you back e (about 2.71828) to the power x, from the built-in math module. In this tutorial, I’ll put math.exp() to work on compound interest, a sigmoid, … Read more >>

Counter in Python: Syntax, Methods and 8 Examples

Counter in Python from the collections module counting coffee orders in a list

If you have a list or string and you want to count each item, a Counter in Python is the quickest way to get it. It takes any iterable and gives you back each item with its count, in a dict subclass from collections. In this tutorial, I’ll go through eight Counter examples, from plain … Read more >>

Python mean() Function: Syntax and 6 Examples

Python mean function from the statistics module returning the average of a list of daily orders

If you want the average of a list of numbers in Python, the Python mean function does exactly that. It takes your list and gives you back the average, and you import it from the statistics module. In this tutorial, I’ll walk through mean() with six examples: lists, USD invoices, dictionaries, Decimal, weighted fmean() and … Read more >>