Python: Tips and Tricks

  1. Fixing WebVTT Times with Python

    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.

  2. Using Python to Convert Images to WEBP

    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.

  3. Scientific Notation in Python

    In this short video, we use Python to explain how scientific notation works.

  4. Understanding Python’s __main__ variable

    In this brief article and accompanying video, we explain the Python magic __main__ variable.

  5. Python Color Constants Module

    A simple Python module for holding color constants.

  6. How to find all your Python installations on Windows (and Mac)

    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.

  7. Python Clocks Explained

    This article explains the different types of clocks in Python.

  8. pow(x, y, z) more efficient than x**y % z and other options

    The Python documentation on pow() states that pow(x,y,z) is computed more efficiently than pow(x,y) % z, and our tests show that to be the case. Check out how we tested it.

  9. Converting Leading Tabs to Spaces with Python

    This is a simple script for converting leading tabs to spaces using Python.

  10. Collatz Conjecture in Python

    This article shows a Python script that demonstrates how the Collatz Conjecture works.

  11. Associate Python Files with IDLE

    In this article, we show how to associate all Python files with IDLE in Windows, so that they will open by default in IDLE.

  12. Python: isdigit() vs. isdecimal()

    The difference between str.isdigit() and str.isdecimal() in Python is, for most of us, academic and can be used interchangeably. For the academics out there, this article explains the difference.

  13. Maximum recursion depth exceeded while calling a Python object

    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.

  14. Finally, a use case for finally – Python Exception Handling

    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 for finally.

  15. When to use Static Methods in Python? Never

    In this article, we explain why you shoul never user static methods in Python.

  16. Python Coding Challenge: Two People with the Same Birthday

    In this article, we challenge you to solve the birthday problem and present one possible solution.

  17. Python’s date.strftime() slower than str(), split, unpack, and concatenate?

    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.

  18. Basic Python Programming Exercise: A Penny Doubled Every Day

    Python Coding Challenge: How long does it take you to make a million dollars starting with just a penny?

  19. Change Default autosave Interval in JupyterLab

    In this brief article, we show you how to modify the autosave interval for all your Jupyter notebooks.

  20. A Python Model for Ping Pong Matches

    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.

  21. Creating an Email Decorator with Python and AWS

    In this article, we show you how to create a decorator that will email the output of the decorated function to some email address.

  22. How to Create a Simple Simulation in Python – Numeric Data

    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.

  23. Bulk Convert Python files to IPython Notebook Files (py to ipynb conversion)

    This article describes a Python hack I used to convert Python files to Jupyter Notebook files.

  24. Bi-directional Dictionary in Python

    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.

  25. Simple Python Script for Extracting Text from an SRT File

    In this article, I show how to convert an SRT file to more readable text using Python.

  26. Python Virtual Environments with venv

    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.

  27. Mapping python to Python 3 on Your Mac

    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.

  28. How to Make IDLE the Default Editor for Python Files on Windows

    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.

  29. How to Do Ternary Operator Assignment in Python

    Many programming languages, including Python, have a ternary conditional operator, which is most often used for conditional variable assignment.

  30. How to Convert Seconds to Years with Python

    The time() method of Python's time 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.

  31. How to Create a Python Package

    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.

  32. How to Read a File with Python

    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.

  33. How to Check the Operating System with Python

    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.

  34. How to Use enumerate() to Print a Numbered List in Python

    You have a list of items and want to print it out as a numbered list using Python.

  35. How to Repeatedly Append to a String in 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.

  36. Checking your Sitemap for Broken Links with Python

    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.

  37. How to do Simultaneous Assignment in Python

    A very cool feature of Python is that it allows for simultaneous assignment. In this article, we show how it works.

  38. Visual Studio Code - Opening Files with Python open()

    When running Python files using Run Python File in Terminal, Visual Studio Code runs the Pythn file using an absolute path. For example:

  39. How to Slice Strings in Python

    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:

  40. How Python Finds Imported Modules

    The Python interpreter must locate imported modules. Where does it look for them?

  41. How to Merge Dictionaries in Python

    In Python 3.5, you can merge two or more dictionaries in a single statement by unpacking the new dictionaries into a new dictionary.

  42. How to Index Strings in Python

    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.

  43. How to Create a Tuple in Python

    In this brief article, we explain why you should always use parentheses when you create tuples in Python.