Python: Tips and Tricks

- Read Article
Every once in a while, we need to make an edit to a video for which we already have closed captioning. If the edit affects the length of the video; for example, if we removed or added a segment to the video; then we will also need to edit the WebVTT file. Doing so manually is a pain, so I created a short Python function for handling this.
- Read Article
With Google's webp format, you get the same quality image at a much smaller file size, meaning faster downloads for your users. Google offers conversion tools for converting png, gif, and jpeg files to webp, but you can also easily roll your own with Python and PILLOW. In this tutorial, we'll show you how.
- Read Article
In this short video, we use Python to explain how scientific notation works.
- Read Article
In this brief article and accompanying video, we explain the Python magic __main__ variable.
- Read Article
This is a simple script for converting leading tabs to spaces using Python.
- Read Article
The Python documentation on
pow()
states thatpow(x,y,z)
is computed more efficiently thanpow(x,y) % z
, and our tests show that to be the case. Check out how we tested it. - Read Article
Because I’d rather write Python code than practice against my robot, I’ve created a model in Python to try to get an idea of what I should practice most to win more games.
- Read Article
This article describes a Python hack I used to convert Python files to Jupyter Notebook files.
- Read Article
Academic for sure, but I was surprised to find that
date.strftime()
is slower than converting the date to a string, splitting the string into a list, unpacking the list into year, month, and day strings and then concatenating those to format the date. - Read Article
Python Coding Challenge: How long does it take you to make a million dollars starting with just a penny?
- Read Article
A bi-directional dictionary is one that provides mapping in both directions. In other words, if the value for the key "a" is "A", then the value for the key "A" will be "a". This article shows a simple way for creating one.
- Read Article
It's easy to wind up with multiple installations of Python on Windows (and Mac). In this article, we show you how to locate them all.
- Read Article
In this article, we show how to associate all Python files with IDLE in Windows, so that they will open by default in IDLE.
- Read Article
In this brief article, we show you how to modify the autosave interval for all your Jupyter notebooks.
- Read Article
The difference between
str.isdigit()
andstr.isdecimal()
in Python is, for most of us, academic and can be used interchangeably. For the academics out there, this article explains the difference. - Read Article
This article explains the different types of clocks in Python.
- Read Article
A simple Python module for holding color constants.
- Read Article
When working with Python properties, you might find yourself getting a “maximum recursion depth exceeded while calling a Python object” error. This article explains how to prevent it.
- Read Article
In this article, we explain why you shoul never user static methods in Python.
- Read Article
I have never need to use the
finally
keyword in Python to clean up after handling errors, so it took me some time to come up with a real-world use case. With the help a colleague, I did come up with one. This article describes a real-world use case forfinally
. - Read Article
In this article, we show you how to create a decorator that will email the output of the decorated function to some email address.
- Read Article
In this article, we challenge you to solve the birthday problem and present one possible solution.
- Read Article
In this article, I show how to create a simple
Simulation
class in Python. The purpose of the class is to run a simulation many times and then return stats (e.g., mean, median, etc.) on the results of the simulation. - Read Article
This article shows a Python script that demonstrates how the Collatz Conjecture works.
- Read Article
In this article, I show how to convert an SRT file to more readable text using Python.
- Read Article
In this brief tutorial, we’ll show you how to create a virtual environment with Python’s venv module. We will do this using Visual Studio Code.
- Read Article
When you install Python 3 on a Mac, it does not update the python command to use Python 3 instead of Python 2. Instead, to run Python 3, you have to use the python3 command. In this brief article, we show you how to fix this.
- Read Article
Want to be able to open files in IDLE on Windows by double-clicking on them? You need to make IDLE the default editor for Python files. In this short article, you'll learn how.
- Read Article
Many programming languages, including Python, have a ternary conditional operator, which is most often used for conditional variable assignment.
- Read Article
The
time()
method of Python'stime
module returns the seconds since the epoch (1/1/1970 at midnight). To convert the number of seconds to years with Python, divide by seconds in a minute, minutes in an hour, hours in a day, and days in a year. - Read Article
Python packages are very easy to create, but not so easy to design. A Python package is just a group of files (and possibly subfolders) stored in a directory that includes a file named __init__.py.
- Read Article
In Python, you open a file using the built-in
open()
function and passing it the path to the file you want to open. By default, files are opened as read-only. - Read Article
Python is cross-platform and generally runs the same all operating systems, but if you're writing code that is operating-system dependent, you may need to check the OS. Here's a function for doing that.
- Read Article
You have a list of items and want to print it out as a numbered list using Python.
- Read Article
In Python, if you need to repeatedly append to a string, you should convert it to a list, append your items to that list, and then join the list back into a string after you've made all the additions.
- Read Article
In 2016, we launched a new website, and I wrote a Python script that checked our old sitemap to make sure that we had all our 301 redirects properly in place.
Five years later, we are again launching a new site, so I revisited that script.
- Read Article
A very cool feature of Python is that it allows for simultaneous assignment. In this article, we show how it works.
- Read Article
When running Python files using Run Python File in Terminal, Visual Studio Code runs the Pythn file using an absolute path. For example:
- Read Article
In Python, when you need to get a sequence of characters from a string (i.e., a substring), you get a slice of the string using the following syntax:
- Read Article
The Python interpreter must locate imported modules. Where does it look for them?
- Read Article
In Python 3.5, you can merge two or more dictionaries in a single statement by unpacking the new dictionaries into a new dictionary.
- Read Article
Indexing is the process of finding a specific element within a sequence of elements through the element's position. Remember that strings are basically sequences of characters. We can use indexing to find a specific character within the string.
- Read Article
In this brief article, we explain why you should always use parentheses when you create tuples in Python.