Free Articles and Tutorials: Python
This page contains a listing of free articles and tutorials.
Django
How to Run the Shell for a Django Project within AWS Elastic Beanstalk AMI
I added slugs to several models after I already had data for those models. To get the slugs saved, I had to save each object corresponding to the model, which is easy programmatically, but a pain to do through the admin interface. My project runs on AWS Elastic Beanstalk, so I needed access to the shell. I found the solution here.
Python
Here we provide a list of free articles and tutorials of tips and tricks that will make you more effective with Python.
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.
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.
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?
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.
Bill Clinton's Inaugural Address
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.
Change Default autosave Interval in JupyterLab
In this brief article, we show you how to modify the autosave interval for all your Jupyter notebooks.
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.
This article shows a Python script that demonstrates how the Collatz Conjecture works.
Converting Leading Tabs to Spaces with Python
This is a simple script for converting leading tabs to spaces using Python.
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.
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
.
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.
George Bush's Inaugural Address
Harry S. Truman's Inaugural Address
How Python Finds Imported Modules
The Python interpreter must locate imported modules. Where does it look for them?
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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:
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.
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.
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.
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.
This article explains the different types of clocks in Python.
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.
A simple Python module for holding color constants.
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.
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.
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.
In this short video, we use Python to explain how scientific notation works.
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.
Understanding Python’s __main__ variable
In this brief article and accompanying video, we explain the Python magic __main__ variable.
Using Python to Convert Images to WEBPTutorial
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.
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:
When to use Static Methods in Python? Never
In this article, we explain why you shoul never user static methods in Python.
William Henry Harrison's Inaugural Address