What Is defaultdict in Python? (With Examples)

What is defaultdict in Python: Command Prompt output of a plain dict KeyError next to defaultdict counting

What is defaultdict in Python? It’s a dictionary from the collections module that creates a default value the first time you use a missing key, instead of raising KeyError. You choose the default with a function like int or list: These examples use order and visit data from US states and cities, and they ran … Read more >>

Python Datetime Subtract Days: How to Subtract Days from a Date

Python subtract days from date: Command Prompt output of renewal dates minus 14 days

As a Python developer, you’ll often get a requirement to send a reminder some days before a due date. For that, you need to subtract days from a date. The obvious due – 14 raises a TypeError, and hand counting breaks at month ends. Quick answer: subtract a timedelta. date(2026, 3, 2) – timedelta(days=14) returns … Read more >>

How to Get the Day of the Week from a Date in Python

Python day of week: VS Code output printing weekday names for five 2026 pickup dates

Holiday orders pile up fast, and you first want the day of the week from each pickup date: is it a Saturday? Python gets that in one line. Quick answer: call d.weekday() for a number (Monday is 0, Sunday is 6) or d.strftime(“%A”) for the name. For November 26, 2026 you’ll get 3 and ‘Thursday’. … Read more >>

How to Get the Day of the Year from a Date in Python

Python datetime day of year: Command Prompt output of tm_yday printing the day number for 2026 shipment dates

If you’ve tried d.day to extract the day number of the year from a date in Python, you’ve probably seen 7 for February 7 instead of 38. That’s the day of the month. The Python datetime day of year lives in d.timetuple().tm_yday (an int), or in d.strftime(“%j”) as a zero-padded string. In this tutorial, I’ll … Read more >>

Python floor() Function: math.floor() Syntax and 8 Examples

Python floor function math.floor rounding numbers down in VS Code

If you have a decimal number in Python and you want the whole number below it, the Python floor function, math.floor(), is the quickest way to get it. It takes a number and gives you back the largest integer not above it. In this tutorial, I’ll run Python’s floor() against whole numbers, negative values, budgets, … Read more >>

Python round() Function: Syntax and 6 Examples

Python round() function rounding floats to the nearest integer in VS Code

If you want to round a number to a whole number or to a few decimal places, the Python round function is what you’re looking for. It takes a number and an optional digit count, and gives you back the closest value. In this tutorial, I’ll take round() through whole numbers, decimal places, tens and … Read more >>

What Is a Docstring in Python? (With Examples)

What is a docstring in Python: Command Prompt output of __doc__ and help() showing a function's docstring

What is a docstring in Python? It’s a string literal written as the first statement in a function, class or module. Python stores it in the object’s __doc__ attribute, and help() displays it: Each example ran on Python 3.12.5, and the output you see came straight from those runs. We’ll read docstrings with __doc__ and … Read more >>

Python min() Function: Syntax and 5 Examples

Python min() function returning the smallest value from a list, from separate arguments and from strings

If you want the smallest value in Python, the Python min function does exactly that. It takes an iterable, or two or more values, and gives you back the smallest one. In this tutorial, I’ll take min() through its syntax and five examples: lists and arguments, a key function, dictionaries, empty lists and min() with … Read more >>

pip install –no-cache-dir: What It Does and When to Use It

pip install no cache dir: Command Prompt output of pip install --no-cache-dir and an empty pip cache

pip install –no-cache-dir installs a package without reading from or saving to pip’s cache, so pip downloads it fresh and leaves no copy behind. Use it for smaller Docker images or to rule out a bad cached file: I tested each command in a fresh virtual environment with Python 3.12.5 and pip 26.2.1, using the … Read more >>

How to Get the Week Number from a Date in Python

Python get week number from date: VS Code output of isocalendar printing ISO year and week for 2026 orders

If you need to get the week number from a date in Python, call d.isocalendar().week for the ISO week (Monday start), or d.strftime(“%U”) for a US-style week that starts on Sunday. People get stuck because the two disagree around New Year. January 1, 2027 is ISO week 53 of 2026, yet %U calls it week … Read more >>