6 Essential Python Scripts for Beginners

Python is a great language to learn due to its object-oriented approach and minimal syntax. The standard library includes numerous useful modules that can be used to write various programs, big or small.

Here are six essential scripts every beginner should know:

1. Batch File Renamer:
This script renames files in the current directory based on specific text replacement patterns. It’s useful for changing file extensions like “.htm” to “.html.”

“`python
import os, sys
if len(sys.argv) < 3: sys.exit("usage: " + sys.argv[0] + " search replace") for filename in os.listdir("."): new_filename = filename.replace(sys.argv[1], sys.argv[2]) if new_filename != filename: os.rename(os.path.join(".", filename), os.path.join(".", new_filename)) ``` 2. Image Thumbnail Maker: This script uses the Pillow library to create thumbnails from images. ```python from PIL import Image if len(sys.argv) < 4: sys.exit("usage: " + sys.argv[0] + " image width height") image = Image.open(sys.argv[1]) image.thumbnail((int(sys.argv[2]), int(sys.argv[3]))) image.save("thumb.jpg") ``` 3. Simple Web Server: This script creates a basic web server that serves files from the current directory over HTTP. ```python import http.server, socketserver port = 8001 with socketserver.TCPServer(("", port), http.server.SimpleHTTPRequestHandler) as httpd: print(f"Serving at port {port}") httpd.serve_forever() ``` 4. Random Password Generator: This script generates a random password of a specified length. ```python import string, random def main(length: int) -> str:
characters = string.ascii_letters + string.digits + string.punctuation
return “”.join(random.choice(characters) for i in range(length))
print(main(32))
“`

5. Crypto Price Checker:
This script uses the CoinGecko API to fetch and display cryptocurrency prices.

“`python
import urllib.request, json
def get_crypto_prices():
url = “https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,litecoin&vs_currencies=usd”
ufile = urllib.request.urlopen(url)
text = ufile.read()
return json.loads(text)
crypto_prices = get_crypto_prices()
for coin, value in crypto_prices.items():
print(f”{coin.capitalize()}: ${value[‘usd’]}”)
“`

6. ASCII Table Generator:
This script prints a table containing all ASCII characters from 32 to 127.

“`python
for i in range(32, 128):
print(“{:03d}”.format(i) + ” ” + chr(i), end=” “)
if (i – 1) % 10 == 0:
print()
“`

These scripts are perfect for beginners looking to get started with Python programming.

Source: https://www.howtogeek.com/basic-but-useful-python-scripts-to-get-you-started