diff --git a/README.md b/README.md index a526db9..2715af4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1759 TILs and counting..._ +_1760 TILs and counting..._ See some of the other learning resources I work on: @@ -1047,6 +1047,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Check If Package Is Installed With Pip](python/check-if-package-is-installed-with-pip.md) - [Control Passing Of Time In Tests](python/control-passing-of-time-in-tests.md) - [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md) +- [Create A Range Of Descending Values](python/create-a-range-of-descending-values.md) - [Dunder Methods](python/dunder-methods.md) - [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.md) - [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md) diff --git a/python/create-a-range-of-descending-values.md b/python/create-a-range-of-descending-values.md new file mode 100644 index 0000000..bbc28b1 --- /dev/null +++ b/python/create-a-range-of-descending-values.md @@ -0,0 +1,44 @@ +# Create A Range Of Descending Values + +A typical use of `range` looks something like this: + +```python +>>> list(range(1, 5)) +[1, 2, 3, 4] +``` + +Which is equivalent to this one where we give a `step` value of `1`. + +```python +>>> list(range(1, 5, 1)) +[1, 2, 3, 4] +``` + +If we try to create a _negative range_, that is, a range of values in decreasing +order, we get an empty list. + +```python +>>> list(range(0, -7)) +[] +``` + +That's because the `step` value still defaults to `1`. And there are no positive +steps between `0` and `-7`. So, let's give `range` a `step` value of `-1`. + +```python +>>> list(range(0,-7, -1)) +[0, -1, -2, -3, -4, -5, -6] +``` + +One practical use case of a negative range like this is using a list +comprehension to transform it into a list of the _last seven days_. + +```python +>>> from datetime import datetime, timedelta +>>> [datetime.now().date() + timedelta(days=days) for days in range(0,-7, -1)] +[datetime.date(2026, 3, 19), datetime.date(2026, 3, 18), datetime.date(2026, 3, 17), datetime.date(2026, 3, 16), datetime.date(2026, 3, 15), datetime.date(2026, 3, 14), datetime.date(2026, 3, 13)] +``` + +Of course this could have been written with a positive range and then +subtracting the `timedelta`. I like that I have the option of doing this in +whatever way makes the code most readable.