Python is a tremendously useful language for running scripts, but it isn't limited just to your computer. There are loads of Python scripts that work perfectly well on mobile devices, too — and while iPhone users are much more limited in what the OS will allow them to actually do, Android users have many more options. That said, all is not lost for iPhone users. Dedicated Python apps like Pyto or Pythonista can work with Apple Shortcuts to create surprisingly robust, serviceable workflows.
Password generators
Get a password that's actually strong
Proper security practices aren't always the easiest to follow, especially when you really start to think about it. And after a few security breaches that caused quite a bit of headache, I've stepped up my game. By keeping a password generator on my phone, I can create a fully custom password that follows set guidelines with just a few taps. I won't recommend one over another — there are a lot of different options — but I'm using Pythonista on my iPhone.
The code is simple:
import secrets
import string
def generate_password(length=16):
alphabet = string.ascii_letters + string.digits + string.punctuation
return ''.join(secrets.choice(alphabet) for _ in range(length))
if __name__ == "__main__":
print(generate_password())
When I run it, it asks how long of a password I want. I can tell it, and it creates one and spits it out. Sure, iPhone has a built-in password generator, but that's still stored in the cloud despite all its encryption. If you want a more hands-on way to generate passwords that doesn't put everything in Apple's clutches, this is a solid option.
YT-DLP
For when I need offline access to video content
If you aren't familiar, yt-dlp is a fantastic tool for downloading video content. It supports a ton of different websites, but in my case, I use it for YouTube. Ever tried maintaining your place in a YouTube video while trying to work inside the engine of a car? It's a pain, especially if you're replaying the same part of the video repeatedly to see what the creator is doing. With yt-dlp, I can quickly download a video to be viewed later. The code is simple, too.
yt-dlp https://www.youtube.com/watch?v=G8sSvZM-pbM
I can also make variations to the code for downloading audio only (great for saving music from YouTube), or for setting videos to download at a specific quality only.
QR code generator
Better security, easier sharing
"What on earth do I need a QR code generator for?" If that was your first thought, you aren't alone. I wasn't entirely sure what the point was, but discovering that I could use it to share my household Wi-Fi with guests was a game-changer. See, my Wi-Fi password is nearly impossible to tell to someone. It's long, convoluted, and generally meant to be a pain to access without credentials. In that respect, it works great — but trying to give it to someone who isn't tech-savvy can be difficult. Generating a QR code that contains the password, though? That's simple.
import qrcode
data = input("What do you want to turn into a QR code? ")
img = qrcode.make(data)
img.save("qrcode.png")
print("Saved as qrcode.png")
Once I enter the information, it saves it as an image file that anyone can scan. That way, I can share my password with guests in a much easier way than handing them a 20-character slip of paper.
I replaced all my bash scripts with Python, and here’s what happened
I replaced all my bash scripts with Python. Here’s what improved, what broke, and why the switch changed my workflow.
Unit conversions
Cooking made easy
I love to cook, and I've gotten pretty good at converting different units of measurement in my head. There are still some I'm not sure about, though (and they're embarrassingly easy). Lately, it's been converting tablespoons to ounces for infant rice cereal, of all things. The solution lies in a simple Python script that I can run on my phone anytime I need to check. Is it overkill for simple things? Yeah, probably. But it's fun. Here's the code I use.
def convert(value, from_unit, to_unit):
to_meters = {
"mm": 0.001, "cm": 0.01, "m": 1, "km": 1000,
"in": 0.0254, "ft": 0.3048, "yd": 0.9144, "mi": 1609.34
}
to_kg = {
"mg": 0.000001, "g": 0.001, "kg": 1,
"oz": 0.0283495, "lb": 0.453592
}
if from_unit in ("c", "f", "k"):
if from_unit == "c" and to_unit == "f": return value * 9/5 + 32
if from_unit == "f" and to_unit == "c": return (value - 32) * 5/9
if from_unit == "c" and to_unit == "k": return value + 273.15
if from_unit == "k" and to_unit == "c": return value - 273.15
if from_unit == "f" and to_unit == "k": return (value - 32) * 5/9 + 273.15
if from_unit == "k" and to_unit == "f": return (value - 273.15) * 9/5 + 32
for table in (to_meters, to_kg):
if from_unit in table and to_unit in table:
return value * table[from_unit] / table[to_unit]
return None
value = float(input("Value: "))
from_unit = input("From (e.g. ft, kg, f): ").lower()
to_unit = input("To (e.g. m, lb, c): ").lower()
result = convert(value, from_unit, to_unit)
if result is not None:
print(f"{value} {from_unit} = {round(result, 4)} {to_unit}")
else:
print("Couldn't convert those units — check your spelling")
Python is a lot more useful on phones than you might think
Python is popular for a reason. It has a huge range of potential applications, but most people don't think about using it on their phones. The best part about it is that the most useful applications can work on both iPhone and Android, despite iPhone's not-insignificant limitations regarding Python.
ython is a programming language that lets you work quicklyand integrate systems more effectively.
