Python: Tips and Tricks

Python: Tips and Tricks
  1. 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
  2. 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
  3. In this short video, we use Python to explain how scientific notation works.

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

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

    Read Article
  6. 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.

    Read Article
  7. 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
  8. This article describes a Python hack I used to convert Python files to Jupyter Notebook files.

    Read Article
  9. 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
  10. Python Coding Challenge: How long does it take you to make a million dollars starting with just a penny?

    Read Article
  11. 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
  12. 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
  13. 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
  14. In this brief article, we show you how to modify the autosave interval for all your Jupyter notebooks.

    Read Article
  15. 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.

    Read Article
  16. This article explains the different types of clocks in Python.

    Read Article
  17. A simple Python module for holding color constants.

    Read Article
  18. 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
  19. In this article, we explain why you shoul never user static methods in Python.

    Read Article
  20. 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.

    Read Article
  21. 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
  22. In this article, we challenge you to solve the birthday problem and present one possible solution.

    Read Article
  23. 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
  24. This article shows a Python script that demonstrates how the Collatz Conjecture works.

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

    Read Article
  26. 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
  27. 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
  28. 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
  29. Many programming languages, including Python, have a ternary conditional operator, which is most often used for conditional variable assignment.

    Read Article
  30. 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.

    Read Article
  31. 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
  32. 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
  33. 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
  34. You have a list of items and want to print it out as a numbered list using Python.

    Read Article
  35. 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
  36. 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
  37. A very cool feature of Python is that it allows for simultaneous assignment. In this article, we show how it works.

    Read Article
  38. 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
  39. 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
  40. The Python interpreter must locate imported modules. Where does it look for them?

    Read Article
  41. 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
  42. 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
  43. In this brief article, we explain why you should always use parentheses when you create tuples in Python.

    Read Article