Quick Links
Python is one of the most approachable languages to learn, thanks to its object-oriented-first approach and its minimal syntax. The standard library includes many useful modules that you can use to write all sorts of programs, big and small.
The scripts here are all tiny, with a narrow focus on a useful task. You can use them not only for day-to-day activities, but also to build upon and extend for additional functionality.

1Batch File Renamer
Renaming a set of files can be a challenging task. If you can remember your shell syntax, you can write a command on the fly, or you can rely ona third-party program like rename.
This simple Python program renames all files in your current directory that contain specific text, replacing that text with a given pattern. You could use it, for example, to change a set of extensions from “.htm” to “.html.”

The script imports a couple of useful low-level modules: os and sys. It immediately uses the latter to check the number of arguments (sys.argv) and exits if too few are given.
The main for loop then iterates through all filenames in the current directory (.). The replace method of the string class then attempts to overwrite the first argument with the second.

A final check to see if the filename has changed is then followed by a call to os.rename() which updates the file if necessary.
This script lacks comprehensive error handling, and you should limit your use of it to simple string search and replace. Always be careful with batch operations that may affect many files.
2Image Thumbnail Maker
The following four-line script shows the power of a convenient library, in this case,Pillow. This fork of the Python Imaging Library has many image-processing functions, to support anything from simple scripts to full-blown graphics editors.
You’ll need to install the Pillow library before you can run this script.brew install pillowworked well for me on macOS, but you can also try pip which theinstallation pagerecommends.
The script uses three methods from the Image module to open a file, create a thumbnail, and save it to a second file:
Note how the thumbnail function takes a single argument, which is a tuple. It will create an image with the same ratio as the original, with a maximum width and height according to the dimensions you provide.
On its own, this is of limited use since the filename and dimensions are hardcoded. you may extend its functionality by passing these values as command-line arguments instead, using a second version of the script which is not much bigger:
Note that the final thumbnail filename is still hardcoded. As a further extension, you could generate a filename based on the original, or accept a fourth command-line parameter.
3Simple Web Server
A basic web server can be very useful; I use one to quickly browse and access files on my local computer. It can also be a handy tool for web development.
Few web servers are as simple as this:
The http.server and socketserver modules do most of the work here. A generic TCPServer instance is passed a port and a SimpleHTTPRequestHandler which servers files from the current directory over HTTP. The serve_forever method (which is inherited from a socketserver.BaseServer class) handles requests until the shutdown() method is called.
As Python’s documentation warns, the http.server module lacks proper security and is not for production use.
Although this is just a toy server, it’s great for personal use and its simplicity is unrivaled.
4Random Password Generator
Whether for a new user account, a web form, or your own development work, a random password is often called for. You should probablyuse a password managerto do this, but that’s not always appropriate. With your own script, you can control the exact details and refine it to suit your needs.
This script uses the string and random modules to generate a random password in just five lines of code. The string module has some helpful constants containing sets of individual characters like digits and letters. By combining these and picking a number at random, the script concatenates a series of characters to produce the final password.
5Crypto Price Checker
This script demonstrates the power of a clean, simple API. The API it uses—fromcoingecko—runs over HTTP, so it’s very portable and easy to support.
Before you run this script, check out the API response from the endpoint it uses,/simple/price. You should see that it returns JSON-formatted data with entries for the currencies passed in the ids parameter.
The urllib.request module includes a urlopen() method which fetches the contents of a URL and returns an HTTPResponse object. Reading its text and passing it to the json.loads() method constructs an equivalent dictionary object.
JSON is a very popular formatfor web APIs because its simple syntax and human-readable format make it easy to parse and test.
6ASCII Table Generator
The ASCII character set is a collection of 128 common characters from the English language, used in various computing contexts. Most programming languages support conversion of ASCII codes to individual characters, and vice versa, but it’s sometimes useful to see all ASCII characters at once.
This script prints a table containing all ASCII characters from 32 to 127, each alongside its code point:
The first thing to note is how the built-in Python function range() treats its parameters; it iterates starting from the first, but stops before it reaches the second. In a language like JavaScript, the equivalent for loop would look like this:
Most of the remaining work is in the print() line and includes formatting each code point as a fixed width of three places. The chr() function returns a character given its ASCII code.
One useful property of ASCII, that this table demonstrates, is that the code of each lowercase letter (a-z) is exactly 32 greater than its uppercase equivalent.
The remaining trick is to print a newline character at the appropriate point, so the output is nicely split into rows. This uses the % operator to check the remainder of the range index when divided by 10, ensuring ten characters per line.