15 Powerful Python One-Liners for Daily Use

Anup Das
Level Up Coding
Published in
4 min readNov 22, 2022

--

Python is one of my favorite programming languages. With python, we can create almost any program, like many other programming languages. But python has something unique in its sleeves; it’s python one-line. One-liners can be just as powerful as a full flashed program itself.

Here I’m going to talk about the top 10 of my favorite python one-liner which you can use in your daily life.

1. Share your Wi-Fi password By creating QR Code 📶

Before trying this block of code in your system you have to install wifi_qrcode_generator library. You can easily do it by using pip. In your command prompt/ terminal enter the following command pip install wifi-qrcode-generator.

import wifi_qrcode_generator as qr
qr.wifi_qrcode('wifi name ', False, 'WPA', 'password')

Output:

You are going to get a QR code like this.

2. Convert Images to Video 📹

Do you have a bunch of images and want to convert them in a video? Then use this awesome python one liner.

First you need to install moviepy module by running this command in your terminal/cmd —

pip install moviepy
from moviepy.editor import ImageSequenceClip as imageSeq

imageSeq(["img1.png", "img2.png"], fps=1).write_videofile("vid.mp4")

3. Display List of all users on Unix-like systems 👩‍💻

print('\n'.join(line.split(":",1)[0] for line in open("/etc/passwd")))

Output

4. Profile a Python Script 📜

While running your script enter -m cProfile to understand the performance of your code.

python -m cProfile cipher.py

Output:

5. Finding all subsets of a sets in one line

from itertools import combinations
print(list(combinations([1, 2, 3, 4], 2)))

Output

6. CSV to JSON

import csv,json

open("data.json", "w").write(json.dumps(list(csv.reader(open('sample_data/california_housing_test.csv')))))

This code is going to create a json file data.json.

JSON to CSV

import pandas as pd
pd.read_json('data.json').to_csv('data.csv', index = None)

7. Start a Server and share your current directory

import os
python -m SimpleHTTPServer 8000

8. Fetch Tables from a PDF

Want to fetch tables from a PDF file? Then you can use the camelot library. This is one of the best python module to fetch tables from PDF.

First install camelot by running this command in your terminal/cmd —

pip install camelot-py
import camelot as cl
table = cl.read_pdf('table.pdf', pages='all')
print(table)

9. Compress CSS File 🗜

import re

open("compressed.css", "w").write(re.sub("\s*([{};,:])\s*", "\\1", re.sub("/\*.*?\*/", "", re.sub("\s+", " ", open("sample.css").read()))))

10. Download any Webpage

You can download any webpage and see its source code offline using python requests module. Here’s the python one liner to do the same.

import requests as req

r = req.get(input("Enter URL: "), headers={'User-Agent':'Mozilla/5'}).text
print(r)

11. One-line Spelling checker

In our everyday life we make a lot of spelling mistake. If you need a one line script which can auto correct your mistake use this.

First install autocorrect module by running this command in your terminal/cmd —

pip install autocorrect
import autocorrect as autospell

check = autospell.Speller(lang='en').spell('speling')
print(check)

12. Creating Powerset of Set

from functools import reduce
dataset = {1,2,3}

# Creating powerset
f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])

print(f(dataset))

"""
output
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

"""

13. Largest 32-Bytes Number

import pandas as pd
pd.read_json('data.json').to_csv('data.csv', index = None)

Output:

14. Re-implementing Cut

Print every line from an input file but remove the first two fields.

python -c "import sys;[sys.stdout.write(' '.join(line.split(' ')[2:])) for line in sys.stdin]" < input.txt

15. Simple YT Video Downloader

Download any YouTube video to watch it offline just by one simple line of code. For this script we are going to need pytube library.

First install pytube module by running this command in your terminal/cmd —

pip install pytube
import pytube

pytube.YouTube(input("Enter the URL: ")).streams.first().download()

Conclusion

Well, these are my top 15 python one-liners, which every developer must know. Thank you so much for reading the article. I hope you find this article helpful and learned something new. 😇

You can connect with me on Linkedin and never miss any updates from me.

Thanks for reading! if you like this, it will boost me to work hard and feel free to give your feedback and suggestion through comments to improve my work. 😊

Until next time, take care of yourself, your family, your extended family(neighbors), and your friends, stay safe and healthy!

Don’t forget to share this article with your developer friends; who are just starting development.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--

Full Stack Data Scientists who writes Data Science, AI, & Machine Learning blogs. Latest updates -> https://anuptechtips.com