Python: Tips and Tricks
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.
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.
In this short video, we use Python to explain how scientific notation works.
In this brief article and accompanying video, we explain the Python magic __main__ variable.
A simple Python module for holding color constants.
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.
This article explains the different types of clocks in Python.
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.This is a simple script for converting leading tabs to spaces using Python.
This article shows a Python script that demonstrates how the Collatz Conjecture works.
In this article, we show how to associate all Python files with IDLE in Windows, so that they will open by default in IDLE.
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.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.
I have never need to use the
finallykeyword 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.In this article, we explain why you shoul never user static methods in Python.
In this article, we challenge you to solve the birthday problem and present one possible solution.
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.Python Coding Challenge: How long does it take you to make a million dollars starting with just a penny?
In this brief article, we show you how to modify the autosave interval for all your Jupyter notebooks.
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.
In this article, we show you how to create a decorator that will email the output of the decorated function to some email address.
In this article, I show how to create a simple
Simulationclass 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.This article describes a Python hack I used to convert Python files to Jupyter Notebook files.
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.
In this article, I show how to convert an SRT file to more readable text using Python.
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.
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.
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.
Many programming languages, including Python, have a ternary conditional operator, which is most often used for conditional variable assignment.
The
time()method of Python'stimemodule 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.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.
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.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.
You have a list of items and want to print it out as a numbered list using Python.
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.
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.
A very cool feature of Python is that it allows for simultaneous assignment. In this article, we show how it works.
When running Python files using Run Python File in Terminal, Visual Studio Code runs the Pythn file using an absolute path. For example:
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:
The Python interpreter must locate imported modules. Where does it look for them?
In Python 3.5, you can merge two or more dictionaries in a single statement by unpacking the new dictionaries into a new dictionary.
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.
In this brief article, we explain why you should always use parentheses when you create tuples in Python.
