#DailyPythonTip
Python Tip #265 (of 365):

Use next() to get the first line in a file. 🧵

#Python #DailyPythonTip
September 22, 2026 at 5:02 PM
Python Tip #267 (of 365):

Change file extensions with with_suffix. 🧵

A ".rst" file path that needs to turn into a ".md" file path:

>>> from pathlib import Path
>>> old_path = Path("uvrs/readme.rst")
>>> old_path.with_suffix(".md")
PosixPath('uvrs/readme.md')

#Python #DailyPythonTip
September 24, 2026 at 5:02 PM
Python Tip #264 (of 365):

Don't use the file readlines method. 🧵

Do this:
lines = list(file)

Not this:
lines = file.readlines()

Both give the same list of lines. So why prefer list?

#Python #DailyPythonTip
September 21, 2026 at 5:01 PM
Python Tip #266 (of 365):

Reread a file with seek(0). 🧵

Try to .read() a file twice, and you'll get nothing the second time:

>>> f = open("my_file.txt")
>>> f‍.read()
'This is my file 👋\nThis is line 2\n'
>>> f‍.read()
''

#Python #DailyPythonTip
September 23, 2026 at 5:02 PM
Python Tip #78 (of 365):

Don't remove string prefixes and suffixes manually. 🧵

Instead of this:

normalized = hex_value.lower()
if normalized.startswith("0x"):
normalized = normalized[2:]

Do this:

normalized = hex_value.lower().removeprefix("0x")

#Python #DailyPythonTip
March 19, 2026 at 6:04 PM
Python Tip #11 (of 365):

Avoid comparing to True and False in Python. 🧵

#Python #DailyPythonTip
January 11, 2026 at 3:18 PM
Python Tip #102 (of 365):

Use pathlib.Path(directory, filename) to join path strings

To join path strings together, instead of:
joined = Path(directory) / filename

Or:
joined = Path(directory).joinpath(filename)

You can do this:
joined = Path(directory, filename)

#Python #DailyPythonTip
April 12, 2026 at 6:04 PM
Python Tip #161 (of 365):

To flatten an iterable-of-iterables, use chain.from_iterable. 🧵

#Python #DailyPythonTip
June 11, 2026 at 12:21 AM
Python Tip #103 (of 365):

Use underscores to make large numbers more readable.

Compare this:

n = 1000000

To this:

n = 1_000_000

The underscores make it more readable, right?

#Python #DailyPythonTip
April 13, 2026 at 11:43 PM
Python Tip #33 (of 365):

Don't sleep on Python's keyword arguments / named arguments. 🧵

When you have the choice between a positional argument or a keyword argument, choose a keyword argument if it clarifies the argument's purpose.

#Python #DailyPythonTip
February 2, 2026 at 7:04 PM
Python Tip #229 (of 365):

Don't overuse classes. 🧵

Writing Python code does NOT require writing classes.

#Python #DailyPythonTip
August 17, 2026 at 10:47 PM
Python Tip #115 (of 365):

Instead of manually caching computed attribute values, use functools.cached_property. 🧵

#Python #DailyPythonTip
April 25, 2026 at 6:04 PM
Python Tip #18 (of 365):

Merge iterables using asterisks, like this: "[*first, *second]" 🧵

To merge two iterables, first and second, into third don't do this:

third = list(first) + list(second)

Do this:

third = [*first, *second]

Why?...

#Python #DailyPythonTip
January 18, 2026 at 7:04 PM
Python Tip #159 (of 365):

When it comes to modifications, think of lists as stack-like. 🧵

This week's tips are all about lists and data structures.

#Python #DailyPythonTip
June 8, 2026 at 11:38 PM
Python Tip #162 (of 365):

Avoid modifying a list while you're looping over it. 🧵

#Python #DailyPythonTip
June 12, 2026 at 12:36 AM
Python Tip #111 (of 365):

Don't use the __class__ attribute. 🧵

To get the name of any object's class, you can use:

type(my_obj).__name__

You might occasionally see this instead:

my_obj.__class__.__name__

I don't recommend that approach.

#Python #DailyPythonTip
April 21, 2026 at 6:04 PM
Python Tip #262 (of 365):

Parse dates with date.strptime. 🧵

In Python 3.14+, the date class got its own strptime class method:

>>> import datetime as dt
>>> dt.date.strptime("2026-08-19", "%Y-%m-%d")
datetime‍.date(2026, 8, 19)

#Python #DailyPythonTip
September 19, 2026 at 5:00 PM
Python Tip #32 (of 365):

When processing potentially large untrusted files, don't iterate over them. 🧵

#Python #DailyPythonTip
February 1, 2026 at 7:04 PM
Python Tip #144 (of 365):

To suppress specific exceptions, use contextlib.suppress 🧵

You could suppress an exception like this:

try:
...
except ValueError:
pass

But I would recommend this:

from contextlib import suppress

with suppress(ValueError):
...

#Python #DailyPythonTip
May 25, 2026 at 2:34 AM
Python Tip #147 (of 365):

Sort by unbound method objects instead of using "lambda" 🧵

Say you're sorting mixed capitalization strings:

>>> frameworks = ["jQuery", "React", "Alpine", "htmx", "Svelte"]
>>> sorted(frameworks)
['Alpine', 'React', 'Svelte', 'htmx', 'jQuery']

#Python #DailyPythonTip
May 27, 2026 at 6:04 PM
Python Tip #128 (of 365):

Chain method calls for readability's sake 🧵

When an object's methods return the same type of object, prefer to chain calls together instead of juggling extra variables.

#Python #DailyPythonTip
May 8, 2026 at 6:09 PM
Python Tip #222 (of 365):

Be careful with mutable default values in function definitions. 🧵

This class is buggy:

class TodoList:
def __init__(self, tasks=[]):
self.tasks = tasks
def add_task(self, task):
self.tasks.append(task)

#Python #DailyPythonTip
August 11, 2026 at 5:15 AM
Python Tip #12 (of 365):

Use "[]" to create empty lists instead of calling "list()". 🧵

(This week's tips will all be related to lists)

#Python #DailyPythonTip
January 12, 2026 at 7:04 PM
Python Tip #238 (of 365):

Don't write ordering methods by hand. 🧵

Want your objects to support <, >, <=, and >=?

You don't need to write them all!

#Python #DailyPythonTip
August 27, 2026 at 4:46 AM
Python Tip #95 (of 365):

Don't use re.match(): it's confusing 🧵

I'm not sure I've ever seen re.match() used when it wasn't being used by mistake.

If you think you want re.match(), you probably want either re.‍search() or re.fullmatch() instead.

#Python #DailyPythonTip
April 5, 2026 at 6:04 PM