
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How it is possible to write obfuscated oneliners in Python?
Yes, it is possible to write obfuscated one-liners in Python using Lambda. Before moving further, let us first understand what are Lambdas in Python.
Python Lambda
The Lambda expressions allow defining anonymous functions. A lambda function is an anonymous function i.e. a function without a name. Let us see the syntax −
lambda arguments: expressions
The keyword lambda defines a lambda function. A lambda expression contains one or more arguments, but it can have only one expression.
Example
Let us see an example −
myStr = "Thisisit!" (lambda myStr : print(myStr))(myStr)
Output
Thisisit!
Merge Elements in a Python List using Lambda
To merge elements using Lambda, we will use the reduce() method. The reduce() is part of the functools module in Python. Let us first learn to install and use the functools module −
Install the functools module;
pip install functools
Use the functools module −
import functools
Example
Following is the code −
import functools # List myList = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] # Display the List print("List = " + str(myList)) # Merge items using Lambda myList[0: 3] = [functools.reduce(lambda i, j: i + j, myList[0: 3])] # Displaying the Result print("Result = " + str(myList))
Output
List = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U'] Result = ['HOW', 'A', 'R', 'E', 'Y', 'O', 'U']
The same functools module will now be used to write obfuscated one-liners in Python. With that, the reduce() method of the functools module is also used −
One-Liner Obfuscated code for Prime Numbers
Example
Let us now see an example of one-liner obfuscated code for prime number using the functools module −
from functools import reduce # Primes < 500 print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0, map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,500)))))
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
One-Liner Obfuscated code for Fibonacci
Example
Let us now see an example of one-liner obfuscated code for first 20 Fibonacci numbers using the functools module −
from functools import reduce # First 20 Fibonacci numbers print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f), range(20))))
Output
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
One-Liner Obfuscated code for Mandelbrot
Example
Let us now see an example of one-liner obfuscated code for Mandelbrot Set using the functools module −
from functools import reduce # Mandelbrot set print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y, Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM, Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro, i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y >=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr( 64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy ))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)) # \___ ___/ \___ ___/ | | |__ lines on screen # V V | |______ columns on screen # | | |__________ maximum of "iterations" # | |_________________ range on y axis # |____________________________ range on x axis
output
BBBBBBBBBBBBBBBCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDCCCCCCCCCCCCCCCBBBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGJJHFFFEEEEEDDDDDCCCCCCCCCCBBBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGHJPJKL_FEEEEEEDDDDDDDCCCCCBBBBBBBBBCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFFGHIJZR_QJIGFFFEEEEEEDDDDDDDCCBBBBBBBBCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFGGGHIKP______SKHGFFFFFEEEEDDDDDDDBBBBBBBCCCDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFHHIXIIIIJKLO______NKJJIHGGGHSGFEEDDDDDBBBBBBCCDDDDDDDDDDDDDDDDDDDEEEEEFFFFFFFGGHIM___R_________________PLN[SQXIFEEDDDDBBBBBCDDDDDDDDDDDDDDDDEEEFFFFFFFFFFFGGGHIKMNS_________________________UIGGFEEDDDBBBBBDDDDDDDDDDEEEEEFFGHOIHHHHHHHHHHHHIJLP____________________________RKJPGFEEDDBBBBDDDDEEEEEEEEEFFFFGGHJM___LNW_ONKJJKNY______________________________SKHGFEEEDBBBBDEEEEEEEEEFFFFFFGHHJLOT__________QPZ_________________________________HFFEEEEBBBDEEEEEEEFGGGGHHHJM_QNS______________________________________________[HGFFEEEEBBB_________________________________________________________________QLIHGFFFEEEEBBBDEEEEEEEFGGGGHHHJM_QNS______________________________________________[HGFFEEEEBBBBDEEEEEEEEEFFFFFFGHHJLOT__________QPZ_________________________________HFFEEEEBBBBDDDDEEEEEEEEEFFFFGGHJM___LNW_ONKJJKNY______________________________SKHGFEEEDBBBBBDDDDDDDDDDEEEEEFFGHOIHHHHHHHHHHHHIJLP____________________________RKJPGFEEDDBBBBBCDDDDDDDDDDDDDDDDEEEFFFFFFFFFFFGGGHIKMNS_________________________UIGGFEEDDDBBBBBBCCDDDDDDDDDDDDDDDDDDDEEEEEFFFFFFFGGHIM___R_________________PLN[SQXIFEEDDDDBBBBBBBCCCDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFHHIXIIIIJKLO______NKJJIHGGGHSGFEEDDDDDBBBBBBBBCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFGGGHIKP______SKHGFFFFFEEEEDDDDDDDBBBBBBBBBCCCCCCDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEEFFFFGHIJZR_QJIGFFFEEEEEEDDDDDDDCCBBBBBBBBBBBCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDEEEEEEEEEFFFGHJPJKL_FEEEEEEDDDDDDDCCCCCBBBBBBBBBBBBBCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDEEEEEEFGJJHFFFEEEEEDDDDDCCCCCCCCCC
- Related Articles
- Is it possible to write to MongoDB console in JavaScript execution?
- How to debug obfuscated JavaScript?
- Is it possible to write data to file using only JavaScript?
- Is it possible to use Python modules in Octave?
- Check if it is possible to survive on Island in Python
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to transform one string to another in Python
- Check if it is possible to serve customer queue with different notes in Python
- Is it possible to cast in a MongoDB Query?
- Is it possible to instantiate Type-parameter in Java?
- Is it possible to resize an array in C#
- Is it possible to create static constructor in java?
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a polygon with given n sidess in Python
