Posted in Python

Python Post #6 Python One Liners ᕕ( ᐛ )ᕗ ✎✐✎✐✎✐✎✐✎✐✎✐


Performing one-liners directly on the Command Prompt can be accomplished by using Python’s -cmd flag (-c for short), and typically requires the import of one or more modules. 


How to Write One-liners in Python?

One-Liners are hard to comprehend, especially in Python. Given a sample one can come up with their own One Liner. Though they are not recommended while actual coding… It is pretty fun to condense normal code to utter gibberish and make the system.. Well I don’t know…Hang maybe

So assuming that you are familiar with the basics of Python and the changes made from Python 2 and Python 3… The only stable versions I am aware of…Let me dive to Pointers on One Liners in Python…

(*•̀ᴗ•́*)و ̑̑

  • So first thing first…As we all know that Python is considered a lazy language…And for a Good Reason too! The blocks of code are distinguished by indentation and not braces,This kinda makes it hard to Create one liners with multiple loops and functions…So let’s stick with a Single never ending for loop 

while 1:…*Gibberish*…

  • And then we have this concept of Recursive function…A Function call inside a function…A pretty convenient thing considering my goal here…Use lambda functions…Since they are preferable than having an actual function definition inside a infinite loop… 
  • And OFC…How can I forget one of my favorite modules…import random…try and read the documentation of this module …since that was where I got most of these one liners 

random.choice(“set of characters to choose from”)


So here are a few… assuming that by now you have installed either a Python2 or Python3 .. Copy these and paste in the Command Prompt 


python -c “while 1:import random;print random.choice(‘|-_+=\\/’), ”

python -c “while 1: locals().setdefault(‘i’,60); import time,random; print(‘ ‘*i+”+’ ‘*(80-i-1)+’|’);time.sleep(.2); i+=random.randint(-2,2)”

python -c “while 1:import random;print “random.randint(0,100)”, map(lambda n:(lambda f:f(f,n))(lambda f,n:{True:lambda:1, False:lambda:n*f(f,n-1)}[n<=1]()),range(0,20))"

python -c “import random,time; p=lambda:random.choice(‘♥♦♣♠’);[print ‘[{}|{}|{}]’.format(p(),p(),p(),t=time.sleep(.2)), for i in range (20)]”


python -c “while 1: locals().setdefault(‘i’,60); import time,random; print(‘ ‘*i+”+’ ‘*(80-i-1)+’|’); time.sleep(.1); i+=random.randint(-2,2)”

python -c “import random,time; p=lambda:random.choice(‘♥♦♣♠’);[print(‘[{}|{}|{}]’ .format(p(),p(),p(),t=time.sleep(.1)),end= ‘\r’) for i in range (20)]”

python -c “import random;n=random.randint(1,99); [(lambda a:print(‘Y’ if a==n else ‘H’ if a>n else ‘L’))(int(input())) for i in range(6)]”

python -c “while 1:import random; print(random.choice(‘|-_+=\\/’), end=”)”



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s