The dos and don’ts of best-practice Python exception handling

Image for post
Image for post
Photo by ConvertKit on Unsplash

No one wants their code throwing errors, but exceptions in Python can have a whole variety of use cases and are critically important to writing good code.

This article describes some examples of what to do and not do when writing exceptions. Hopefully, it’ll instill in you a deeper understanding of exception handling broadly as well provide some useful Python tips.

Do Write Lots of Exceptions

  • AttemptedToAccessCoreDatabasePriorToDatabaseTransactionBeingInitiatedException
  • JavascriptMillisecondTimeFormatBufferOverflowException
  • CannotDeleteUserAccountThatDoesntExistException

Exceptions, exceptions, exceptions everywhere. These names look challenging to read and are long to type, but are they really so scary?

These exceptions provide the most valuable function exceptionally well — they’re specific, informative, and to the point. …

An introduction to f-strings and string formatting

Image for post
Image for post

How do I add my variable into a string in Python? If I have someone’s name, how do I print this back out, with additional information, like a hello message?

Define string formatting:

Noun. The fancy way of saying: “Making strings/text look the way you want them to, whether that’s with variables, new lines, spaces, capital letters, etc.

Example: “The string formatting on the output of this simple command-line app is great, it has colors and everything!

String formatting has come a long way in the last few years. Let’s take a quick trip down memory lane and look at the old ways of formatting strings, scroll to the bottom to see some f-string examples. …

Today I begin my series on coding problems. The first is a simple, yet perhaps somewhat mystifying problem. If given a biased coin, how can one create an unbiased coin toss? This problem is from an interview at Square (reportedly).

Assume you have access to a function toss_biased() which returns 0 or 1 with a probability that's not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin.

Write a function to simulate an unbiased coin toss.

This problem is more mathematical than software related. If you have a background in statistics you’re probably already sensing the solution has something to do with symmetry. …


Henry George

Entrepreneur, Developer, Mathematician. Artificial Intelligence, Startups and Software Development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store